o
    %if                     @  s^  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. dd	l/m0Z0 dd
l1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZP ddlQmRZR ddlSmTZT ddlUmVZW ddlXmYZY ddlZm[Z[ zddlEm\Z\ W n e]y   ddlEmNZ\ Y nw dZ^dZ_dZ`dZadZbdZcdZdd Zed!Zfh d"Zge[eWZhehd#krd$nd%Zid&ZVdvd+d,ZjG d-d. d.e6ZkG d/d0 d0Zldwd4d5ZmG d6d7 d7ZnG d8d9 d9ZoG d:d; d;ZpG d<d= d=Zqed>d?d@ZredAdBd@ZsedCdDd@ZtedEdFd@ZuedGdHd@ZvedGdId@ZwedGdJd@ZxdxdQdRZydydUdVZzdzdWdXZ{d{d[d\Z|dzd]d^Z}d|d_d`Z~d|dadbZ			cd}d~dpdqZddtduZdS )z^This module includes classes and functions designed specifically for use with the mypy plugin.    )annotationsN)ConfigParser)AnyCallableIterator)	ErrorCode)expand_typeexpand_type_by_instance)	ARG_NAMEDARG_NAMED_OPTARG_OPTARG_POS	ARG_STAR2MDEFArgumentAssignmentStmtBlockCallExprClassDefContext	DecoratorDictExprEllipsisExpr
ExpressionFuncDefIfStmtJsonDict
MemberExprNameExprPassStmtPlaceholderNodeRefExpr	StatementStrExprSymbolTableNodeTempNode	TypeAliasTypeInfoVar)Options)CheckerPluginInterfaceClassDefContextFunctionContextMethodContextPluginReportConfigContextSemanticAnalyzerPluginInterface)dataclasses)deserialize_and_fixup_type)set_callable_name)make_wildcard_trigger)state)map_type_from_supertype)AnyTypeCallableTypeInstanceNoneType
OverloadedType	TypeOfAnyTypeTypeTypeVarType	UnionTypeget_proper_type)fill_typevars)get_unique_redefinition_name)__version__)_fields)parse_mypy_version)
TypeVarDef)r?   pydantic-mypyzpydantic-mypy-metadatazpydantic.main.BaseModelz#pydantic_settings.main.BaseSettingszpydantic.root_model.RootModelz5pydantic._internal._model_construction.ModelMetaclasszpydantic.fields.Fieldzpydantic.dataclasses.dataclass.pydantic.functional_validators.model_validator>   *pydantic.functional_serializers.serializer.pydantic.deprecated.class_validators.validator.pydantic.functional_validators.field_validator0pydantic.functional_serializers.model_serializer3pydantic.deprecated.class_validators.root_validatorrI   )r   i  builtins__builtins__   versionstrreturntype[Plugin]c                 C  s   t S )a!  `version` is the mypy version string.

    We might want to use this to print a warning if the mypy version being used is
    newer, or especially older, than we expect (or need).

    Args:
        version: The mypy version string.

    Return:
        The Pydantic mypy plugin type.
    )PydanticPlugin)rR    rW   F/var/www/html/corbot_env/lib/python3.10/site-packages/pydantic/mypy.pypluginr   s   rY   c                      s|   e Zd ZdZd' fddZd(ddZd)ddZd*ddZd+ddZd)ddZ	d,ddZ
d-dd Zd.d!d"Zd/d%d&Z  ZS )0rV   zThe Pydantic mypy plugin.optionsr)   rT   Nonec                   s&   t || _| j | _t | d S N)PydanticPluginConfigplugin_configto_data_plugin_datasuper__init__)selfrZ   	__class__rW   rX   rb      s   
zPydanticPlugin.__init__fullnamerS   (Callable[[ClassDefContext], bool] | Nonec                 C  s:   |  |}|rt|jtrtdd |jjD r| jS dS )zUpdate Pydantic model class.c                 s      | ]}|j tkV  qd S r\   )rf   BASEMODEL_FULLNAME.0baserW   rW   rX   	<genexpr>       z5PydanticPlugin.get_base_class_hook.<locals>.<genexpr>N)lookup_fully_qualified
isinstancenoder'   anymro$_pydantic_model_class_maker_callbackrc   rf   symrW   rW   rX   get_base_class_hook   s
   
z"PydanticPlugin.get_base_class_hook(Callable[[ClassDefContext], None] | Nonec                 C  s   |t kr| jS dS )z,Update Pydantic `ModelMetaclass` definition.N)MODEL_METACLASS_FULLNAME)_pydantic_model_metaclass_marker_callbackrc   rf   rW   rW   rX   get_metaclass_hook   s   z!PydanticPlugin.get_metaclass_hook(Callable[[FunctionContext], Type] | Nonec                 C  s"   |  |}|r|jtkr| jS dS )z/Adjust the return type of the `Field` function.N)ro   rf   FIELD_FULLNAME_pydantic_field_callbackru   rW   rW   rX   get_function_hook   s   
z PydanticPlugin.get_function_hook&Callable[[MethodContext], Type] | Nonec                 C  s   | drtS dS )z-Adjust return type of `from_orm` method call.z	.from_ormN)endswithfrom_attributes_callbackr{   rW   rW   rX   get_method_hook   s   
zPydanticPlugin.get_method_hookc                 C  s   |t krtdk rtjS dS )zMark pydantic.dataclasses as dataclass.

        Mypy version 1.1.1 added support for `@dataclass_transform` decorator.
        )   r   N)DATACLASS_FULLNAMEMYPY_VERSION_TUPLEr1   dataclass_class_maker_callbackr{   rW   rW   rX   get_class_decorator_hook   s   z'PydanticPlugin.get_class_decorator_hookctxr/   dict[str, Any]c                 C  s   | j S )zjReturn all plugin config data.

        Used by mypy to determine if cache needs to be discarded.
        )r`   )rc   r   rW   rW   rX   report_config_data   s   z!PydanticPlugin.report_config_datar+   boolc                 C  s   t |j|j|j| j}| S r\   )PydanticModelTransformerclsreasonapir^   	transform)rc   r   transformerrW   rW   rX   rt      s   z3PydanticPlugin._pydantic_model_class_maker_callbackc                 C  s@   | j jrdS |jjj}|sJ dt|jddrd|j_dS dS )zReset dataclass_transform_spec attribute of ModelMetaclass.

        Let the plugin handle it. This behavior can be disabled
        if 'debug_dataclass_transform' is set to True', for testing purposes.
        Nz-callback not passed from 'get_metaclass_hook'dataclass_transform_spec)r^   debug_dataclass_transformr   infodeclared_metaclassgetattrtyper   )rc   r   info_metaclassrW   rW   rX   rz      s   
z8PydanticPlugin._pydantic_model_metaclass_marker_callbackr,   r<   c           	        s  |j  |jd dksJ d|jd dksJ d|jd }|jd }|r0|r0t|j|j  S |rF|jd d }|d }t|tsD|S  S |r|jd d }t|t	rY|j
d }t|tr|j}t|dd}|r~td	d
 |D r~t fdd
|D |_|S  S )ao  Extract the type of the `default` argument from the Field function, and use it as the return type.

        In particular:
        * Check whether the default and default_factory argument is specified.
        * Output an error if both are specified.
        * Retrieve the type of the argument which is specified, and use it as return type for the function.
        r   defaultz0"default" is no longer first argument in Field()r   default_factoryz9"default_factory" is no longer second argument in Field()argsNc                 s  s    | ]}t |tV  qd S r\   )rp   r?   )rk   argrW   rW   rX   rm      rn   z:PydanticPlugin._pydantic_field_callback.<locals>.<genexpr>c                 3  s    | ]} V  qd S r\   rW   )rk   _default_any_typerW   rX   rm      s    )default_return_typecallee_arg_namesr   +error_default_and_default_factory_specifiedr   context	arg_typesrp   r   r;   itemsr8   ret_typer   alltuple)	rc   r   default_argsdefault_factory_argsdefault_typedefault_argdefault_factory_typer   r   rW   r   rX   r      s4   





z'PydanticPlugin._pydantic_field_callbackrZ   r)   rT   r[   )rf   rS   rT   rg   )rf   rS   rT   rx   )rf   rS   rT   r}   )rf   rS   rT   r   )r   r/   rT   r   )r   r+   rT   r   )r   r+   rT   r[   )r   r,   rT   r<   )__name__
__module____qualname____doc__rb   rw   r|   r   r   r   r   rt   rz   r   __classcell__rW   rW   rd   rX   rV      s    

	



	

rV   c                   @  sJ   e Zd ZU dZdZded< ded< ded< ded< dddZdddZdS )r]   a  A Pydantic mypy plugin config holder.

    Attributes:
        init_forbid_extra: Whether to add a `**kwargs` at the end of the generated `__init__` signature.
        init_typed: Whether to annotate fields in the generated `__init__`.
        warn_required_dynamic_aliases: Whether to raise required dynamic aliases error.
        debug_dataclass_transform: Whether to not reset `dataclass_transform_spec` attribute
            of `ModelMetaclass` for testing purposes.
    )init_forbid_extra
init_typedwarn_required_dynamic_aliasesr   r   r   r   r   r   rZ   r)   rT   r[   c                 C  s   |j d u rd S t|j }|d ur:|di di }| jD ]}||d}t|ts1td| t| || qd S t }|	|j  | jD ]}|j
t|dd}t| || qFd S )NtoolrH   Fz/Configuration value must be a boolean for key: )fallback)config_file
parse_tomlget	__slots__rp   r   
ValueErrorsetattrr   read
getbooleanCONFIGFILE_KEY)rc   rZ   toml_configconfigkeysettingr^   rW   rW   rX   rb     s"   




zPydanticPluginConfig.__init__r   c                   s    fdd j D S )z/Returns a dict of config names to their values.c                   s   i | ]}|t  |qS rW   )r   )rk   r   rc   rW   rX   
<dictcomp>"      z0PydanticPluginConfig.to_data.<locals>.<dictcomp>)r   r   rW   r   rX   r_         zPydanticPluginConfig.to_dataNr   rT   r   )r   r   r   r   r   __annotations__rb   r_   rW   rW   rW   rX   r]      s   
 

r]   r   r-   r<   c                 C  s   | j }t|tr|j}t|trt|jtr|j}nt|tr"|}nd| d|jj d}t	|| j
| j | jS |j jt}|du rF| jS |di d}|dur]t|j j| j
| j | jS )z1Raise an error if from_attributes is not enabled.z
ctx.type: 
 (of type )Nr   from_attributesT)r   rp   r>   itemr8   r   r9   re   r   error_unexpected_behaviorr   r   r   metadatar   METADATA_KEYerror_from_attributesname)r   ctx_type
model_typedetailpydantic_metadatar   rW   rW   rX   r   %  s"   

r   c                   @  sZ   e Zd ZdZd,ddZd-ddZd.ddZd/dd Zd0d"d#Ze	d1d%d&Z
d2d)d*Zd+S )3PydanticModelFieldz5Based on mypy.plugins.dataclasses.DataclassAttribute.r   rS   alias
str | Nonehas_dynamic_aliasr   has_defaultlineintcolumnr   Type | Noner   r'   c	           	      C  s4   || _ || _|| _|| _|| _|| _|| _|| _d S r\   )r   r   r   r   r   r   r   r   )	rc   r   r   r   r   r   r   r   r   rW   rW   rX   rb   ?  s   
zPydanticModelField.__init__current_infotypedforce_optional	use_aliasr   r0   rT   r   c                 C  sJ   |  |||}|r| ||nttj}t||d|s| jr!tdS tdS )zABased on mypy.plugins.dataclasses.DataclassAttribute.to_argument.N)variabletype_annotationinitializerkind)	to_varr   r7   r=   explicitr   r   r   r
   )rc   r   r   r   r   r   r   r   rW   rW   rX   to_argumentS  s   	zPydanticModelField.to_argumentc                 C  sj   | j dur2t| jdddur2t|jj t| j | jjj	t
|iW  d   S 1 s-w   Y  | j S )zABased on mypy.plugins.dataclasses.DataclassAttribute.expand_type.N	self_type)r   r   r   r5   strict_optional_setrZ   strict_optionalr   r   idrB   )rc   r   r   rW   rW   rX   r   e  s
    zPydanticModelField.expand_typer(   c                 C  s.   |r| j dur| j }n| j}t|| ||S )z<Based on mypy.plugins.dataclasses.DataclassAttribute.to_var.N)r   r   r(   r   )rc   r   r   r   r   rW   rW   rX   r   q  s   zPydanticModelField.to_varr   c                 C  s0   | j sJ | j| j| j| j| j| j| j  dS )?Based on mypy.plugins.dataclasses.DataclassAttribute.serialize.)r   r   r   r   r   r   r   )r   r   r   r   r   r   r   	serializer   rW   rW   rX   r   z  s   
zPydanticModelField.serializedatac                 C  s,   |  }t|d|}| d||d|S )ABased on mypy.plugins.dataclasses.DataclassAttribute.deserialize.r   )r   r   NrW   )copyr2   pop)r   r   r   r   typrW   rW   rX   deserialize  s   zPydanticModelField.deserializesub_typer[   c                 C  sT   | j dur(t|jj t| j || j| _ W d   dS 1 s!w   Y  dS dS )zxExpands type vars in the context of a subtype when an attribute is inherited
        from a generic super type.
        N)r   r5   r   rZ   r   r6   r   )rc   r   r   rW   rW   rX   expand_typevar_from_subtype  s
   
"z.PydanticModelField.expand_typevar_from_subtypeN)r   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   )r   r'   r   r   r   r   r   r   r   r0   rT   r   )r   r'   r   r0   rT   r   )r   r'   r   r0   r   r   rT   r(   rT   r   )r   r'   r   r   r   r0   rT   r   )r   r'   r   r0   rT   r[   )r   r   r   r   rb   r   r   r   r   classmethodr   r   rW   rW   rW   rX   r   <  s    




	r   c                   @  s0   e Zd ZdZdd ZedddZdd	d
ZdS )PydanticModelClassVarzBased on mypy.plugins.dataclasses.DataclassAttribute.

    ClassVars are ignored by subclasses.

    Attributes:
        name: the ClassVar name
    c                 C  s
   || _ d S r\   r   )rc   r   rW   rW   rX   rb     s   
zPydanticModelClassVar.__init__r   r   rT   c                 C  s   |  }| di |S )r   NrW   )r   )r   r   rW   rW   rX   r     s   z!PydanticModelClassVar.deserializec                 C  s
   d| j iS )r   r   r   r   rW   rW   rX   r     s   zPydanticModelClassVar.serializeN)r   r   rT   r   r   )r   r   r   r   rb   r   r   r   rW   rW   rW   rX   r     s    r   c                   @  s   e Zd ZU dZh dZded< dYddZdZddZd[ddZd\ddZ	d]ddZ
d^d!d"Zd_d%d&Zd`d+d,Zdad4d5Zdbd:d;Zdcd<d=Zddd?d@ZdedfdFdGZedgdHdIZedhdKdLZdidQdRZdjdSdTZedkdVdWZdXS )lr   zTransform the BaseModel subclass according to the plugin settings.

    Attributes:
        tracked_config_fields: A set of field configs that the plugin has to track their value.
    >   extrafrozenalias_generatorr   populate_by_namezset[str]tracked_config_fieldsr   r   r   Expression | Statementr   r0   r^   r]   rT   r[   c                 C  s   || _ || _|| _|| _d S r\   )_cls_reason_apir^   )rc   r   r   r   r^   rW   rW   rX   rb     s   
z!PydanticModelTransformer.__init__r   c                 C  s   | j j}tdd |jdd D }|  }| ||\}}|du s&|du r(dS |D ]
}|jdu r4 dS q*tdd |jdd D }| |||| | ||| | j	|| j
|jdu d |   d	d
 |D dd
 |D | d|jt< dS )a  Configures the BaseModel subclass according to the plugin settings.

        In particular:

        * determines the model config and fields,
        * adds a fields-aware signature for the initializer and construct methods
        * freezes the class if frozen = True
        * stores the fields, config, and if the class is settings in the mypy metadata for access by subclasses
        c                 s  s    | ]}t |jv V  qd S r\   )ROOT_MODEL_FULLNAMErf   rj   rW   rW   rX   rm     rn   z5PydanticModelTransformer.transform.<locals>.<genexpr>NFc                 s  rh   r\   )rf   BASESETTINGS_FULLNAMErj   rW   rW   rX   rm     rn   T)r  c                 S     i | ]}|j | qS rW   r   r   rk   fieldrW   rW   rX   r     r   z6PydanticModelTransformer.transform.<locals>.<dictcomp>c                 S  r  rW   r  )rk   	class_varrW   rW   rX   r     r   )fields
class_varsr   )r  r   rr   rs   collect_configcollect_fields_and_class_varsr   add_initializeradd_model_construct_method
set_frozenr	  r  adjust_decorator_signaturesget_values_dictr   r   )rc   r   is_root_modelr   r  r  r  is_settingsrW   rW   rX   r     s(   

z"PydanticModelTransformer.transformc                   s   | j jj D ]:\}}t|jtrA|jjd  t trAt j	t
rA j	jtv rA j	jtkr<t fddt jD sAd|jj_qdS )a  When we decorate a function `f` with `pydantic.validator(...)`, `pydantic.field_validator`
        or `pydantic.serializer(...)`, mypy sees `f` as a regular method taking a `self` instance,
        even though pydantic internally wraps `f` with `classmethod` if necessary.

        Teach mypy this by marking any function whose outermost decorator is a `validator()`,
        `field_validator()` or `serializer()` call as a `classmethod`.
        r   c                 3  s6    | ]\}} j | d kot|to|jdkV  qdS )modeafterN)	arg_namesrp   r#   value)rk   ir   	first_decrW   rX   rm     s
     
zGPydanticModelTransformer.adjust_decorator_signatures.<locals>.<genexpr>TN)r  r   namesr   rp   rq   r   original_decoratorsr   calleer   rf   DECORATOR_FULLNAMESMODEL_VALIDATOR_FULLNAMErr   	enumerater   funcis_class)rc   r   rv   rW   r"  rX   r    s    

z4PydanticModelTransformer.adjust_decorator_signaturesModelConfigDatac              	   C  s   | j }t }d}d}|j D ]\}}| ||}|r"d}|| qd}|jjD ]}t|t	t
fs3q)t|t	r|jd }	t|	trG|	jdkrHq)t|jtrmt|jj|jjD ]\}
}|
du r`qW|| j|
|dd qWnNt|jtr|jjD ]\}}t|tsqw|| |j| qwn.t|t
r|jdkrq)|jjD ]}t|t	sq|jd }	t|	tsq|| |	j|j q|r| jd|  nd}q)|s|r|r|jr|js| jjrt| j| |jjd	d D ]&}t|j vrq| j!t"|j# |j t d
  D ]\}}|$|| qq|S )zhCollects the values of the config attributes that are used by the plugin, accounting for parent classes.FTNr   model_config)	lax_extraConfigzYSpecifying config in two places is ambiguous, use either Config attribute or class kwargsr   r   )%r  r,  keywordsr   get_config_updateupdatedefsbodyrp   r   r   lvaluesr   r   rvaluer   zipr  r   r   r#   r   r	  failhas_alias_generatorr  r^   r   error_required_dynamic_aliasesr   rs   r   r   add_plugin_dependencyr4   rf   
setdefault)rc   r   r   has_config_kwargshas_config_from_namespacer   exprconfig_datastmtlhsarg_namer   key_expr
value_exprsubstmtr   r   rW   rW   rX   r    s   









z'PydanticModelTransformer.collect_configr-  r  Jtuple[list[PydanticModelField] | None, list[PydanticModelClassVar] | None]c                 C  s  | j }i }i }t|jjdd D ]b}t|jvrq| jt|j	 |jt d 
 D ]2\}}t||| j}	|	|j| j |	||< |jj|}
|
r]|
jr]t|
jts]| jd|
j q+|jt d 
 D ]\}}t|||< qgqt }t }| |jD ]B}| |||}t|tr|jd }|r|jdkrt| j| q||j |||j< qt|tr|jd }||j |||j< qt| t| fS )zACollects the fields for the model, accounting for parent classes.r   r  r  z7BaseModel field may only be overridden by another fieldr  r   root)r  reversedr   rs   r   r   r	  r;  r4   rf   r   r   r   r   r$  r   rq   rp   r(   r8  r   set%_get_assignment_statements_from_blockr3  $collect_field_or_class_var_from_stmtr5  r    error_extra_fields_on_root_modeladdlistvalues)rc   r-  r  r   found_fieldsfound_class_varsr   r   r   r  sym_nodecurrent_field_namescurrent_class_vars_namesrA  maybe_fieldrB  rW   rW   rX   r  W  sJ   





z6PydanticModelTransformer.collect_fields_and_class_varsrA  r   Iterator[AssignmentStmt]c                 c  sT    |j D ]}|js| |E d H  q|jd ur&|jjs(| |jE d H  d S d S d S r\   )r4  is_unreachablerK  	else_body)rc   rA  r4  rW   rW   rX   ,_get_assignment_statements_from_if_statement  s   
zEPydanticModelTransformer._get_assignment_statements_from_if_statementblockr   c                 c  s>    |j D ]}t|tr|V  qt|tr| |E d H  qd S r\   )r4  rp   r   r   rZ  )rc   r[  rA  rW   rW   rX   rK    s   


z>PydanticModelTransformer._get_assignment_statements_from_blockr   r   dict[str, PydanticModelClassVar]1PydanticModelField | PydanticModelClassVar | Nonec              
   C  s  | j }|jd }t|trt|jr|jdkrdS |jsKt|jt	r<t|jj
t	r<t|jj
j
tr<|jj
j
jtv r<dS |j|v rCdS t| j| dS |jd }t|tsWdS t|jrb|jdkrddS |jj|j}|du rrdS |j}t|tr|dS t|tr| jd| dS t|tsdS |jrt|jS t|j}t|tr|jjdkr| jd| | |}	|jdu r|jr|jr| jj|jdd}
|
r|
|_n| jd	| t t!j"|_| #|\}}|r|j$s| j%j&rt'| j| | (||j|}t)|j||	||j*|j+||jd
S )a^  Get pydantic model field from statement.

        Args:
            stmt: The statement.
            model_config: Configuration settings for the model.
            class_vars: ClassVars already known to be defined on the model.

        Returns:
            A pydantic model field if it could find the field in statement. Otherwise, `None`.
        r   r-  NzFType aliases inside BaseModel definitions are not supported at runtimezdataclasses.InitVarz%InitVar is not supported in BaseModelT)is_finalzGNeed type argument for Final[...] with non-literal default in BaseModel)r   r   r   r   r   r   r   r   ),r  r5  rp   r   rE   is_valid_field_namer   
new_syntaxr6  r   r&  rf   r'  error_untyped_fieldsr	  r   r$  r   rq   r    r&   r8  r(   is_classvarr   rA   r   r9   get_has_defaultr^  is_inferredanalyze_simple_literal_typer7   r=   
from_errorget_alias_infor  r^   r   r:  _infer_dataclass_attr_init_typer   r   r   )rc   rA  r-  r  r   rB  rv   rq   	node_typer   r   r   r   	init_typerW   rW   rX   rL    s   
 









z=PydanticModelTransformer.collect_field_or_class_var_from_stmtrv   r$   r   rS   r   r   r   c           	      C  s   |j }|jr|S t|j }t|ts|S |j d}|rpt|jtrc|j d}|s,J |j r:tt	|j |j |}nt
tjS t|trT|jtttgkrTt|jd |S | jd|j j d| |S | jd|j j d| |S )zvInfer __init__ argument type for an attribute.

        In particular, possibly use the signature of __set__.
        __set__rQ   z(Unsupported signature for "__set__" in ""zUnsupported "__set__" in ")r   implicitrA   rp   r9   r   rq   r   get_containing_type_infor6   r7   r=   unannotatedr8   	arg_kindsr   r	   r   r	  r8  r   )	rc   rv   r   r   r   tsetter
super_infosetter_typerW   rW   rX   rh  #  s0   


z8PydanticModelTransformer._infer_dataclass_attr_init_typer  list[PydanticModelField]r   r  c                 C  sf  d| j jjv r| j jjd jsdS | jj}|jdu}t|jo!|j }| j	|||||d}|rAt
dkrA|d jtkr<tnt|d _|r| jtj}	d|	jv r|	jd j}
|
dur|
jdur|
j}t|jD ]'\}}|dst|dsuqf| j|j| }t||}|t||dt qf| ||std	}|t|ttjdt  t!| j| j d|t" d
 dS )zAdds a fields-aware `__init__` method to the class.

        The added `__init__` will be annotated with types vs. all `Any` depending on the plugin settings.
        rb   NTr   requires_dynamic_aliasesr   r  )r   r   r   r   __r   kwargs)r   return_type)#r  r   r$  plugin_generatedr^   r   r  r   r9  get_field_argumentsr   r   r
   r   r   r	  ro   r  rq   r   r)  r  
startswith	anal_typer   r(   appendr   should_init_forbid_extrar7   r=   r   r   
add_methodr:   )rc   r  r   r  r  r   r   rw  r   base_settings_nodebase_settings_init_node	func_typearg_idxrC  analyzed_variable_typer   varrW   rW   rX   r  I  s<   


z(PydanticModelTransformer.add_initializerc           	      C  s   | j t d| j t dg}t|t g}ttd||dt}t	| j j
j | j|ddd|d}W d   n1 s@w   Y  | ||s\td}|t|ttjdt |g| }t| j | jd	|t| jjdd
 dS )zAdds a fully typed `model_construct` classmethod to the class.

        Similar to the fields-aware __init__ method, but always uses the field names (not aliases),
        and does not treat settings fields as optional.
        z.setz.str_fields_setNTFrv  ry  model_construct)r   rz  is_classmethod)r	  
named_typeBUILTINS_NAMEr@   r:   r   r(   r   r5   r   rZ   r   r|  r  r  r7   r=   r   r   r  r  rB   r   )	rc   r  r   r  set_stroptional_set_strfields_set_argumentr   r  rW   rW   rX   r  v  s(   $



z3PydanticModelTransformer.add_model_construct_methodr  c           
   	   C  s   | j j}|D ]o}|j|j}|durU|j}t|tr||_qt|t	r.| j
js.| j
  qzt|}W n tyA   t|}Y nw d| d|j d}	t|	| j
| j  q|j||dd}||_||_|jd |j |_tt||j|j< qdS )zMarks all fields as properties so that attempts to set them trigger mypy errors.

        This is the same approach used by the attrs and dataclasses plugins.
        Nzsym_node.node: r   r   F)r   .)r  r   r$  r   r   rq   rp   r(   is_propertyr    r	  final_iterationdeferrS   	TypeErrorreprre   r   r   rf   	_fullnamer$   r   )
rc   r  r   r  r   r  rS  r  var_strr   rW   rW   rX   r    s,   
z#PydanticModelTransformer.set_frozenFr   r   r.  ModelConfigData | Nonec                 C  s   || j vrdS |dkr1t|tr|jdk}nt|tr!|jdk}n|s*t|| j| dS t|dS |dkrHd}t|t	rC|j
dkrCd}t|d	S t|t	r^|j
d
v r^tdi ||j
dkiS t|| j| dS )zDetermines the config update due to a single kwarg in the ConfigDict definition.

        Warns if a tracked config attribute is set to a value the plugin doesn't know how to interpret (e.g., an int)
        Nr  forbid)forbid_extrar  Tbuiltins.NoneF)r9  )builtins.Truezbuiltins.Falser  rW   )r  rp   r#   r   r   r   error_invalid_config_valuer	  r,  r   rf   )rc   r   r   r.  r  r9  rW   rW   rX   r1    s(   


	

z*PydanticModelTransformer.get_config_updatec                 C  s   | j }t|tr
dS t|trIt|jtrI|jjtkrIt|j	|j
D ]$\}}|du s.|dkr5|jtu  S |dkrFt|toB|jdk   S q"dS t|t S )zUReturns a boolean indicating whether the field defined in `stmt` is a required field.FNr   r   r  )r6  rp   r%   r   r&  r!   rf   r~   r7  r   r  re   r   r   )rA  r?  r   r   rW   rW   rX   rc    s   
"z(PydanticModelTransformer.get_has_defaulttuple[str | None, bool]c                 C  s   | j }t|tr
dS t|trt|jtr|jjtksdS t|j	D ]\}}|dkr+q"|j
| }t|tr<|jdf  S  dS dS )a  Returns a pair (alias, has_dynamic_alias), extracted from the declaration of the field defined in `stmt`.

        `has_dynamic_alias` is True if and only if an alias is provided, but not as a string literal.
        If `has_dynamic_alias` is True, `alias` will be None.
        )NFr   F)NT)r6  rp   r%   r   r&  r!   rf   r~   r)  r  r   r#   r   )rA  r?  r!  rC  r   rW   rW   rX   rg    s"   



z'PydanticModelTransformer.get_alias_infor   r   rw  list[Argument]c                   s(   j j  fdd|D }|S )zHelper function used during the construction of the `__init__` and `model_construct` method signatures.

        Returns a list of mypy Argument instances for use in the generated signatures.
        c              	     s0   g | ]}r	|j s|j pjd qS ))r   r   r   r   )r   r   r	  r  r   r  rw  rc   r   r   rW   rX   
<listcomp>  s    	z@PydanticModelTransformer.get_field_arguments.<locals>.<listcomp>)r  r   )rc   r  r   r   rw  r  	argumentsrW   r  rX   r|    s
   z,PydanticModelTransformer.get_field_argumentsc                 C  s.   |j s| |t|jrdS |jrdS | jjS )a@  Indicates whether the generated `__init__` should get a `**kwargs` at the end of its signature.

        We disallow arbitrary kwargs if the extra config setting is "forbid", or if the plugin config says to,
        *unless* a required dynamic alias is present (since then we can't determine a valid signature).
        FT)r  is_dynamic_alias_presentr   r9  r  r^   r   )rc   r  r   rW   rW   rX   r  "  s   z1PydanticModelTransformer.should_init_forbid_extrar9  c                 C  s8   | D ]}|j r
 dS q|r| D ]
}|jdu r dS qdS )zReturns whether any fields on the model have a "dynamic alias", i.e., an alias that cannot be
        determined during static analysis.
        TNF)r   r   )r  r9  r  rW   rW   rX   r  /  s   
z1PydanticModelTransformer.is_dynamic_alias_presentN)
r   r   r   r  r   r0   r^   r]   rT   r[   )rT   r   )rT   r[   )rT   r,  )r-  r,  r  r   rT   rG  )rA  r   rT   rW  )r[  r   rT   rW  )rA  r   r-  r,  r  r\  rT   r]  )rv   r$   r   rS   r   r   rT   r   )
r  ru  r   r,  r  r   r  r   rT   r[   )r  ru  r   r,  r  r   rT   r[   )r  ru  r   r0   r  r   rT   r[   )F)r   rS   r   r   r.  r   rT   r  )rA  r   rT   r   )rA  r   rT   r  )r  ru  r   r   r   r   rw  r   r  r   rT   r  )r  ru  r   r,  rT   r   )r  ru  r9  r   rT   r   )r   r   r   r   r  r   rb   r   r  r  r  rZ  rK  rL  rh  r  r  r  r1  staticmethodrc  rg  r|  r  r  rW   rW   rW   rX   r     s0   
 


$

L
A


}
&
-
#

r   c                   @  sD   e Zd ZdZ					ddd	d
ZdddZdddZdddZdS )r,  z(Pydantic mypy plugin model config class.Nr  bool | Noner  r   r  r9  c                 C  s"   || _ || _|| _|| _|| _d S r\   )r  r  r   r  r9  )rc   r  r  r   r  r9  rW   rW   rX   rb   A  s
   
zModelConfigData.__init__rT   r   c                 C  s   dd | j  D S )zReturns a dict of Pydantic model config names to their values.

        It includes the config if config value is not `None`.
        c                 S  s   i | ]\}}|d ur||qS r\   rW   )rk   kvrW   rW   rX   r   T  s    z3ModelConfigData.get_values_dict.<locals>.<dictcomp>)__dict__r   r   rW   rW   rX   r  O     zModelConfigData.get_values_dictr   r  r[   c                 C  s2   |du rdS |   D ]
\}}t| || qdS )z$Update Pydantic model config values.N)r  r   r   )rc   r   r  r  rW   rW   rX   r2  V  s
   zModelConfigData.updater   rS   r   r   c                 C  s"   t | |du rt| || dS dS )zFSet default value for Pydantic model config if config value is `None`.N)r   r   )rc   r   r   rW   rW   rX   r<  ]  s   zModelConfigData.setdefault)NNNNN)
r  r  r  r  r   r  r  r  r9  r  r   )r   r  rT   r[   )r   rS   r   r   rT   r[   )r   r   r   r   rb   r  r2  r<  rW   rW   rW   rX   r,  >  s    

r,  zpydantic-ormzInvalid from_attributes callPydanticzpydantic-configzInvalid config valuezpydantic-aliaszDynamic alias disallowedzpydantic-unexpectedzUnexpected behaviorzpydantic-fieldzUntyped field disallowedzInvalid Field defaultsz!Extra field on RootModel subclass
model_namer   r*   r   r   r[   c                 C     |j d|  d|td dS )zCEmits an error when the model does not have `from_attributes=True`.rl  z$" does not have from_attributes=TruecodeN)r8  	ERROR_ORM)r  r   r   rW   rW   rX   r   l     r   r   r0   c                 C  r  )z0Emits an error when the config value is invalid.zInvalid value for "Config.rl  r  N)r8  ERROR_CONFIG)r   r   r   rW   rW   rX   r  q  r  r  c                 C     | j d|td dS )znEmits required dynamic aliases error.

    This will be called when `warn_required_dynamic_aliases=True`.
    z#Required dynamic aliases disallowedr  N)r8  ERROR_ALIASr   r   rW   rW   rX   r:  v  r  r:  r   8CheckerPluginInterface | SemanticAnalyzerPluginInterfacec                 C  s4   d}d|  d}|d| d7 }|j ||td dS )z Emits unexpected behavior error.z6https://github.com/pydantic/pydantic/issues/new/choosez7The pydantic mypy plugin ran into unexpected behavior: 
z&Please consider reporting this bug at z so we can try to fix it!r  N)r8  ERROR_UNEXPECTED)r   r   r   linkfull_messagerW   rW   rX   r   ~  s   r   c                 C  r  )z;Emits an error when there is an untyped field in the model.zUntyped fields disallowedr  N)r8  ERROR_UNTYPEDr  rW   rW   rX   ra    r   ra  c                 C  r  )z]Emits an error when there is more than just a root field defined for a subclass of RootModel.z2Only `root` is allowed as a field of a `RootModel`r  N)r8  ERROR_EXTRA_FIELD_ROOT_MODELr  rW   rW   rX   rM    r   rM  c                 C  r  )zNEmits an error when `Field` has both `default` and `default_factory` together.z>Field default and default_factory cannot be specified togetherr  N)r8  ERROR_FIELD_DEFAULTSr  rW   rW   rX   r     r   r   F8SemanticAnalyzerPluginInterface | CheckerPluginInterfacer   r   r   r  rz  r   r   tvar_defTypeVarDef | Noner  r   c                 C  s  |j }||jv r|j| }	|	jrt|	jtr|jj|	j t| t	r)| 
d}
n| dg }
|rE|p8tt|}ttd|dtdg}n|pJt|}ttd|dtg}|| }g g g }}}|D ]}|jslJ d||j ||jj ||j qct|||||
}|r|g|_t||tt g}||_ t|||_||_|jd | |_|j|_||jv rt ||j}|j| |j|< |rd|_!t||j}||_ |j|_d|_"t#|t$dg|}|j|_t%t&|}	nt%t&|}	d|	_|	|j|< |j'jj| dS )	zhVery closely related to `mypy.plugins.common.add_method_to_class`, with a few pydantic-specific changes.zbuiltins.functionr  NT__pydantic_self__z"All arguments must be fully typed.r  r   )(r   r$  r{  rp   rq   r   r3  r4  remover0   r  named_generic_typer>   rB   r   r(   r   r   r  r   r   r   r8   	variablesr   r   r3   r   r+  rf   r  r   rC   is_decoratedr  r   r   r$   r   defn)r   r   r   r   rz  r   r  r  r   rv   function_typefirstr   r  rp  r   	signaturer*  r_namer  decrW   rW   rX   r    sZ   





r  r   dict[str, Any] | Nonec                 C  s   |  dsdS tjdkrddl}nzddl}W n ty*   ddl}|d Y dS w t| d}|	|W  d   S 1 s@w   Y  dS )zReturns a dict of config keys to values.

    It reads configs from toml file and returns `None` if the file is not a toml file.
    z.tomlN)      r   zJNo TOML parser installed, cannot read configuration from `pyproject.toml`.rb)
r   sysversion_infotomllibtomliImportErrorwarningswarnopenload)r   toml_r  rfrW   rW   rX   r     s   



$r   )rR   rS   rT   rU   )r   r-   rT   r<   )r  rS   r   r*   r   r   rT   r[   )r   rS   r   r0   r   r   rT   r[   )r   r0   r   r   rT   r[   )r   rS   r   r  r   r   rT   r[   )r   r*   r   r   rT   r[   )NNF)r   r  r   r   r   rS   r   r  rz  r<   r   r   r  r  r  r   rT   r[   )r   rS   rT   r  )r   
__future__r   r  configparserr   typingr   r   r   mypy.errorcodesr   mypy.expandtyper   r	   
mypy.nodesr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   mypy.optionsr)   mypy.pluginr*   r+   r,   r-   r.   r/   r0   mypy.pluginsr1   mypy.plugins.commonr2   mypy.semanalr3   mypy.server.triggerr4   
mypy.stater5   mypy.typeopsr6   
mypy.typesr7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   mypy.typevarsrB   	mypy.utilrC   mypy.versionrD   mypy_versionpydantic._internalrE   pydantic.versionrF   rG   r  r   r   ri   r  r
  ry   r~   r   r(  r'  r   r  rY   rV   r]   r   r   r   r   r,  r  r  r  r  r  r  r  r   r  r:  r   ra  rM  r   r  r   rW   rW   rW   rX   <module>   s    !$	4

v
.[     %






X