
    df!                       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[ 	 ddlEm\Z\ dZ^dZ_dZ`dZadZbdZcdZdd Zeh d!Zf e[eW      Zgegd"k\  rd#nd$Zhd%ZVdFd&Zi G d' d(e6      Zj G d) d*      ZkdGd+Zl G d, d-      Zm G d. d/      Zn G d0 d1      Zo ed2d3d4      Zp ed5d6d4      Zq ed7d8d4      Zr ed9d:d4      Zs ed;d<d4      Zt ed;d=d4      ZudHd>ZvdId?ZwdJd@Zx	 	 	 	 	 	 	 	 dKdAZydJdBZzdLdCZ{	 	 	 dM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dNdDZ|dOdEZ}y# e]$ r	 ddlEmNZ\ Y w xY w)Pz^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.BaseSettingsz5pydantic._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_validatorrJ   )r   i  builtins__builtins__   c                    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)versions    F/home/api-vastappli/venv/lib/python3.12/site-packages/pydantic/mypy.pypluginrW   q   s
         c                  l     e Zd ZdZd fdZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZ xZS )rT   zThe Pydantic mypy plugin.c                    t        |      | _        | j                  j                         | _        t        |   |       y N)PydanticPluginConfigplugin_configto_data_plugin_datasuper__init__)selfoptions	__class__s     rV   ra   zPydanticPlugin.__init__   s4    1': ..668!rX   c                    | j                  |      }|rLt        |j                  t              r2t	        d |j                  j
                  D              r| j                  S y)zUpdate Pydantic model class.c              3  B   K   | ]  }|j                   t        k(    y wr[   )fullnameBASEMODEL_FULLNAME.0bases     rV   	<genexpr>z5PydanticPlugin.get_base_class_hook.<locals>.<genexpr>   s     P44==$66P   N)lookup_fully_qualified
isinstancenoder(   anymro$_pydantic_model_class_maker_callbackrb   rg   syms      rV   get_base_class_hookz"PydanticPlugin.get_base_class_hook   sG    ))(3:chh1P388<<PP@@@rX   c                .    |t         k(  r| j                  S y)z,Update Pydantic `ModelMetaclass` definition.N)MODEL_METACLASS_FULLNAME)_pydantic_model_metaclass_marker_callbackrb   rg   s     rV   get_metaclass_hookz!PydanticPlugin.get_metaclass_hook   s    //AAArX   c                h    | j                  |      }|r|j                  t        k(  r| j                  S y)z/Adjust the return type of the `Field` function.N)rn   rg   FIELD_FULLNAME_pydantic_field_callbackrt   s      rV   get_function_hookz PydanticPlugin.get_function_hook   s/    ))(33<<>1000rX   c                2    |j                  d      rt        S y)z-Adjust return type of `from_orm` method call.z	.from_ormN)endswithfrom_attributes_callbackrz   s     rV   get_method_hookzPydanticPlugin.get_method_hook   s    [)++rX   c                H    |t         k(  rt        dk  rt        j                  S y)zMark pydantic.dataclasses as dataclass.

        Mypy version 1.1.1 added support for `@dataclass_transform` decorator.
        )   r   N)DATACLASS_FULLNAMEMYPY_VERSION_TUPLEr2   dataclass_class_maker_callbackrz   s     rV   get_class_decorator_hookz'PydanticPlugin.get_class_decorator_hook   s$    
 )).@6.I===rX   c                    | j                   S )zjReturn all plugin config data.

        Used by mypy to determine if cache needs to be discarded.
        )r_   )rb   ctxs     rV   report_config_dataz!PydanticPlugin.report_config_data   s    
    rX   c                    t        |j                  |j                  |j                  | j                        }|j                         S r[   )PydanticModelTransformerclsreasonapir]   	transform)rb   r   transformers      rV   rs   z3PydanticPlugin._pydantic_model_class_maker_callback   s3    .sww

CGGTM_M_`$$&&rX   c                    | j                   j                  ry|j                  j                  j                  }|sJ d       t        |j                  dd      rd|j                  _        yy)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   )rb   r   info_metaclasss      rV   ry   z8PydanticPlugin._pydantic_model_metaclass_marker_callback   s\     7788NNN>&&(BDI;?N8 JrX   c                |  	 |j                   	|j                  d   dk(  sJ d       |j                  d   dk(  sJ d       |j                  d   }|j                  d   }|r$|r"t        |j                  |j
                         	S |r+|j                  d   d   }|d   }t        |t              s|S 	S |r|j                  d   d   }t        |t              r|j                  d   }t        |t              rH|j                  }t        |dd      }|r+t        d	 |D              rt        	f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              3  <   K   | ]  }t        |t                y wr[   )ro   r@   )rj   args     rV   rl   z:PydanticPlugin._pydantic_field_callback.<locals>.<genexpr>   s     HC:c;7Hs   c              3  "   K   | ]  }  y wr[    )rj   _default_any_types     rV   rl   z:PydanticPlugin._pydantic_field_callback.<locals>.<genexpr>   s     -M1.>-Ms   )default_return_typecallee_arg_namesr   +error_default_and_default_factory_specifiedr   context	arg_typesro   r   r<   itemsr9   ret_typer   alltuple)
rb   r   default_argsdefault_factory_argsdefault_typedefault_argdefault_factory_typer   r   r   s
            @rV   r~   z'PydanticPlugin._pydantic_field_callback   sG    22##A&)3g5gg##A&*;;x=xxxx{"xx{07M##==+A.L&q/K k<8##*  ' "#&==#3A#6  .
;';'A'A!'D$.=/88 x6H4HH(--M-M(MrX   rc   r*   returnNone)rg   strr   z(Callable[[ClassDefContext], bool] | None)rg   r   r   z(Callable[[ClassDefContext], None] | None)rg   r   r   z(Callable[[FunctionContext], Type] | None)rg   r   r   z&Callable[[MethodContext], Type] | None)r   r0   r   dict[str, Any])r   r,   r   bool)r   r,   r   r   )r   r-   r   r=   )__name__
__module____qualname____doc__ra   rv   r{   r   r   r   r   rs   ry   r~   __classcell__)rd   s   @rV   rT   rT      s9    #"
!'@. rX   rT   c                  N    e Zd ZU dZdZded<   ded<   ded<   ded<   ddZdd	Zy
)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   c                   |j                   y t        |j                         }|q|j                  di       j                  di       }| j                  D ]?  }|j                  |d      }t	        |t
              st        d|       t        | ||       A y t               }|j                  |j                          | j                  D ]'  }|j                  t        |d      }t        | ||       ) y )NtoolrI   Fz/Configuration value must be a boolean for key: )fallback)config_file
parse_tomlget	__slots__ro   r   
ValueErrorsetattrr   read
getbooleanCONFIGFILE_KEY)rb   rc   toml_configconfigkeysettingr]   s          rV   ra   zPydanticPluginConfig.__init__  s    & !4!45" __VR044_bIF~~ , **S%0!'40$'VWZV[%\]]c7+	, )NMw223~~ ,'22>3QV2Wc7+,rX   c                V    | j                   D ci c]  }|t        | |       c}S c c}w )z/Returns a dict of config names to their values.)r   r   )rb   r   s     rV   r^   zPydanticPluginConfig.to_data  s&    37>>BCWT3''BBBs   &Nr   r   r   )r   r   r   r   r   __annotations__ra   r^   r   rX   rV   r\   r\      s3    I #''##,&CrX   r\   c                   | j                   }t        |t              r|j                  }t        |t              r't        |j
                  t              r|j
                  }n]t        |t              r|}nJd| d|j                  j                   d}t        || j                  | j                         | j                  S |j                   j                  j                  t              }|| j                  S |j                  di       j                  d      }|dur5t!        |j                   j"                  | j                  | j                         | j                  S )z1Raise an error if from_attributes is not enabled.z
ctx.type: 
 (of type )r   from_attributesT)r   ro   r?   itemr9   r   r:   rd   r   error_unexpected_behaviorr   r   r   metadatar   METADATA_KEYerror_from_attributesname)r   ctx_type
model_typedetailpydantic_metadatar   s         rV   r   r   $  s    xxH(H%==(L)j9J9JH.U&&
	Hh	'
hZz(2D2D2M2M1NaP!&#''3;;?&&&"0044\B &&&'++Hb9==>OPOd"joo22CGGS[[I"""rX   c                  r    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZddZddZddZddZe	dd       Z
ddZy	)PydanticModelFieldz5Based on mypy.plugins.dataclasses.DataclassAttribute.c	                t    || _         || _        || _        || _        || _        || _        || _        || _        y r[   )r   aliashas_dynamic_aliashas_defaultlinecolumnr   r   )	rb   r   r   r   r   r   r   r   r   s	            rV   ra   zPydanticModelField.__init__>  s>     	
!2&			rX   c                    t        | j                  ||      |r| j                  |      nt        t        j
                        d|s| j                  rt              S t              S )zABased on mypy.plugins.dataclasses.DataclassAttribute.to_argument.N)variabletype_annotationinitializerkind)	r   to_varr	   r8   r>   explicitr   r   r   )rb   current_infotypedforce_optional	use_aliass        rV   to_argumentzPydanticModelField.to_argumentR  s\    [[y9>CD,,\:QZQcQcId"0D4D4D	
 	
 KT	
 	
rX   c                    | j                   U| j                  j                  ?t        | j                   | j                  j                  j                  t        |      i      S | j                   S )zABased on mypy.plugins.dataclasses.DataclassAttribute.expand_type.)r   r   	self_typer	   idrC   )rb   r   s     rV   r	   zPydanticModelField.expand_type[  sS    99 TYY%8%8%D
 tyy499+>+>+A+A=Q]C^*_``yyrX   c                    |r| j                   | j                   }n| j                  }t        || j                  |            S )z<Based on mypy.plugins.dataclasses.DataclassAttribute.to_var.)r   r   r)   r	   )rb   r   r   r   s       rV   r   zPydanticModelField.to_vare  s8    /::D99D4)),788rX   c                    | j                   sJ | j                  | j                  | j                  | j                  | j
                  | j                  | j                   j                         dS )z?Based on mypy.plugins.dataclasses.DataclassAttribute.serialize.)r   r   r   r   r   r   r   )r   r   r   r   r   r   r   	serialize)rb   s    rV   r   zPydanticModelField.serializen  sT    yyIIZZ!%!7!7++IIkkII'')
 	
rX   c                n    |j                         }t        |j                  d      |      } | d||d|S )zABased on mypy.plugins.dataclasses.DataclassAttribute.deserialize.r   )r   r   r   )copyr3   pop)r   r   datar   typs        rV   deserializezPydanticModelField.deserialize{  s9     yy{(&)93?/$/$//rX   c                j    | j                   't        | j                   || j                        | _         yy)zxExpands type vars in the context of a subtype when an attribute is inherited
        from a generic super type.
        N)r   r7   r   )rb   sub_types     rV   expand_typevar_from_subtypez.PydanticModelField.expand_typevar_from_subtype  s+     99 /		8TYYODI !rX   N)r   r   r   z
str | Noner   r   r   r   r   intr   r  r   Type | Noner   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   r1   r   r   )r  r(   r   r   )r   r   r   r   ra   r   r	   r   r   classmethodr  r  r   rX   rV   r   r   ;  s    ?   	
     (
9
 0 0PrX   r   c                     e Zd ZU dZh dZded<   	 	 	 	 	 	 	 	 	 	 ddZddZddZddZ	dd	Z
dd
ZddZ	 	 	 	 	 	 ddZd dZd!dZ	 	 	 	 	 	 	 	 d!dZd"dZd#dZed$d       Zed%d       Z	 	 	 	 	 	 	 	 	 	 	 	 d&dZd'dZed(d       Zy))r   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_fieldsc                <    || _         || _        || _        || _        y r[   )_cls_reason_apir]   )rb   r   r   r   r]   s        rV   ra   z!PydanticModelTransformer.__init__  s!     		*rX   c                *   | j                   j                  }| j                         }| j                  |      }|y|D ]  }|j                   y t        d |j                  dd D              }| j                  |||       | j                  |||       | j                  ||j                  du        | j                          |D ci c]  }|j                  |j                          c}|j                         d|j                  t         <   yc c}w )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
        NFc              3  B   K   | ]  }|j                   t        k(    y wr[   )rg   BASESETTINGS_FULLNAMEri   s     rV   rl   z5PydanticModelTransformer.transform.<locals>.<genexpr>  s     [T$--+@@[rm   T)r  )fieldsr   )r  r   collect_configcollect_fieldsr   rq   rr   add_initializeradd_model_construct_method
set_frozenr  adjust_decorator_signaturesr   r   get_values_dictr   r   )rb   r   r   r  fieldis_settingss         rV   r   z"PydanticModelTransformer.transform  s    yy~~$$&$$V,> 	Ezz!	 [TXXVYWY][[VV[9''Dv}}'<=((* CIIuzz5??#44I,,.'
l#
 	 Js   "Dc                ,   | j                   j                  j                  j                         D ]  \  }}t	        |j
                  t              s!|j
                  j                  d   t	        t              sKt	        j                  t              sfj                  j                  t        v sj                  j                  t        k(  r(t        fdt        j                         D              rd|j
                  j"                  _         y)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     K   | ]<  \  }}j                   |   d k(  xr! t        |t              xr |j                  dk(   > yw)modeafterN)	arg_namesro   r$   value)rj   ir   	first_decs      rV   rl   zGPydanticModelTransformer.adjust_decorator_signatures.<locals>.<genexpr>  sO        &3 &//2f<rCQXAYr^a^g^gkr^rr s   AATN)r  r   namesr   ro   rp   r   original_decoratorsr   calleer   rg   DECORATOR_FULLNAMESMODEL_VALIDATOR_FULLNAMErq   	enumerater   funcis_class)rb   r   ru   r)  s      @rV   r  z4PydanticModelTransformer.adjust_decorator_signatures  s     --335 	2ID##((I.HH88;	y(3"9#3#3X>!((115HH "((115MM  *3INN*C   .2CHHMM*#	2rX   c                   | j                   }t               }d}d}|j                  j                         D ]-  \  }}| j	                  ||      }|sd}|j                  |       / d}|j                  j                  D ]  }t        |t        t        f      st        |t              r |j                  d   }	t        |	t              r|	j                  dk7  r[t        |j                  t              rbt!        |j                  j"                  |j                  j$                        D ])  \  }
}|
	|j                  | j	                  |
|             + nt        |j                  t&              r|j                  j                  D ]A  \  }}t        |t(              s|j                  | j	                  |j*                  |             C nt        |t              r|j                  dk7  rm|j                  j                  D ]h  }t        |t              s|j                  d   }	t        |	t              s4|j                  | j	                  |	j                  |j                               j |r| j,                  j/                  d|        nd} |s|rF|rD|j0                  r8|j2                  s,| j4                  j6                  rt9        | j,                  |       |j:                  j<                  dd D ]  }t>        |j@                  vr| j,                  jC                  tE        |jF                               |j@                  t>           d	   j                         D ]  \  }}|jI                  ||         |S )
zhCollects the values of the config attributes that are used by the plugin, accounting for parent classes.FTNr   model_configConfigzYSpecifying config in two places is ambiguous, use either Config attribute or class kwargsr   r   )%r  ModelConfigDatakeywordsr   get_config_updateupdatedefsbodyro   r   r   lvaluesr   r   rvaluer   zipr&  r   r   r$   r'  r  failhas_alias_generatorr  r]   r   error_required_dynamic_aliasesr   rr   r   r   add_plugin_dependencyr5   rg   
setdefault)rb   r   r   has_config_kwargshas_config_from_namespacer   exprconfig_datastmtlhsarg_namer   key_expr
value_exprsubstmtr   r'  s                    rV   r  z'PydanticModelTransformer.collect_config  s   ii "!$)! ,,,,. 	+JD$00t<K$(!k*		+ "&HHMM &	-Dd^X$>?$/ll1o!#x0CHH4Ndkk84),T[[-B-BDKKDTDT)U M##+$d&<&<Xs&KLM  X6040A0A Z,*)(G<$d&<&<X^^Z&XYZ
 D(+99(#yy~~ TG%g~> !//!,C%c84 MM$"8"87>>"RST !		o (,%M&	-P  9..//&&DD.tyy$?HHLL$ 	/D4==0 II++,A$--,PQ#}}\:8DJJL /e!!$./	/ rX   c                l   | j                   }i }t        |j                  j                  dd       D ]c  }t        |j
                  vr| j                  j                  t        |j                               |j
                  t           d   j                         D ]  \  }}t        j                  ||| j                        }t        j                  | j                  j                  j                         5  |j#                  |j                         ddd       |||<   |j                  j$                  j'                  |      }|s|j(                  st+        |j(                  t,              r| j                  j/                  d|j(                          f t1               }	| j3                  |j4                        D ]P  }
| j7                  |
|      }||
j8                  d   }|	j;                  |j<                         |||j<                  <   R t?        |jA                               S # 1 sw Y   xY w)zACollects the fields for the model, accounting for parent classes.r   r  r  Nz7BaseModel field may only be overridden by another fieldr   )!r  reversedr   rr   r   r   r  rA  r5   rg   r   r   r  r6   strict_optional_setrc   strict_optionalr  r*  r   rp   ro   r)   r>  set%_get_assignment_statements_from_blockr9  collect_field_from_stmtr;  addr   listvalues)rb   r3  r   found_fieldsr   r   r  r   sym_nodecurrent_field_namesrG  maybe_fieldrH  s                rV   r  z'PydanticModelTransformer.collect_fields/  s   ii 79SXX\\!B/0 	D 4==0 II++,A$--,PQ"mmL9(CIIK 
d*66tT499M
 ..tyy/@/@/P/PQ @55chh?@%*T"88>>--d3jPS6TIINNQ 	8 ),>>sxxH 	5D66t\JK&ll1o#''1)4SXX&	5 L'')**)@ @s   4H))H3c              #    K   |j                   D ](  }|j                  r| j                  |      E d {    * |j                  ;|j                  j                  s$| j                  |j                        E d {    y y y 7 N7 	wr[   )r:  is_unreachablerR  	else_body)rb   rG  r:  s      rV   ,_get_assignment_statements_from_if_statementzEPydanticModelTransformer._get_assignment_statements_from_if_statementc  s{     II 	LD&&EEdKKK	L >>%dnn.K.KAA$..QQQ /L% LQs'   BBBAB:B;BBc              #     K   |j                   D ]A  }t        |t              r| t        |t              s)| j	                  |      E d {    C y 7 wr[   )r:  ro   r   r   r^  )rb   blockrG  s      rV   rR  z>PydanticModelTransformer._get_assignment_statements_from_blockj  sN     JJ 	SD$/
D&)LLTRRR		S Ss   5AAAAc           
     H   | j                   }|j                  d   }t        |t              r.t	        j
                  |j                        r|j                  dk(  ry|j                  st        |j                  t              rt        |j                  j                  t              r_t        |j                  j                  j                  t              r1|j                  j                  j                  j                  t        v ryt        | j                  |       y|j                  d   }t        |t              syt	        j
                  |j                        r|j                  dk(  ry|j                  j                   j#                  |j                        }|y|j$                  }t        |t&              ryt        |t(              r| j                  j+                  d|       yt        |t,              sy|j.                  ryt1        |j2                        }t        |t4              r5|j2                  j                  dk(  r| j                  j+                  d|       | j7                  |      }|j2                  |j8                  rw|j:                  rk| j                  j=                  |j                  d      }	|	r|	|_        n:| j                  j+                  d	|       t?        t@        jB                        |_        | jE                  |      \  }
}|r8|jF                  s,| jH                  jJ                  rtM        | j                  |       | jO                  ||j                  |      }tQ        |j                  |||
|jR                  |jT                  ||j                  
      S )a  Get pydantic model field from statement.

        Args:
            stmt: The statement.
            model_config: Configuration settings for the model.

        Returns:
            A pydantic model field if it could find the field in statement. Otherwise, `None`.
        r   r3  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  r;  ro   r   rF   is_valid_field_namer   
new_syntaxr<  r   r,  rg   r-  error_untyped_fieldsr  r   r*  r   rp   r!   r'   r>  r)   is_classvarrB   r   r:   get_has_defaultrb  is_inferredanalyze_simple_literal_typer8   r>   
from_errorget_alias_infor  r]   r   r@  _infer_dataclass_attr_init_typer   r   r   )rb   rG  r3  r   rH  ru   rp   	node_typer   r  r   r   	init_types                rV   rS  z0PydanticModelTransformer.collect_field_from_stmtq  s    iill1o#x(0K0KCHH0UY\YaYaesYs4;;1t{{118<t{{1188(CKK&&--66:MM
  !D1ll1o#x(**3884N8Rhhnn  *; xxdO,
 dI&IINNX $$
   $DII.	i*y~~/F/FJ_/_IINN7
 **4088$2B2B ))77d7SC			] $I$8$89	#'#6#6t#<  \%B%BtGYGYGwGw*499d;88chhM	!/#;;	
 		
rX   c                8   |j                   }|j                  r|S t        |j                         }t        |t              s|S |j                   j                  d      }|r:t        |j                  t              r|j                   j                  d      }|sJ |j                   r+t        t        |j                   |j                   |            }nt        t        j                        S t        |t              r7|j                  t        t        t        gk(  rt!        |j"                  d   |      S | j$                  j'                  d|j                   j(                   d|       |S | j$                  j'                  d|j                   j(                   d|       |S )zvInfer __init__ argument type for an attribute.

        In particular, possibly use the signature of __set__.
        __set__rR   z(Unsupported signature for "__set__" in ""zUnsupported "__set__" in ")r   implicitrB   ro   r:   r   rp   r   get_containing_type_infor7   r8   r>   unannotatedr9   	arg_kindsr   r
   r   r  r>  r   )	rb   ru   r   r   r   tsetter
super_infosetter_types	            rV   rl  z8PydanticModelTransformer._infer_dataclass_attr_init_type  sE   
 ((<<NCHH% !X&NI&&++w/VV<<YG
!!;;"12I&++WXW]W]_i2j"kK"9#8#899k<8[=R=RW >
 3;3H3H3KQOOIINN%Maffkk]Z[#\^ef  		!;AFFKK=JGTrX   c           
        d| j                   j                  j                  v r.| j                   j                  j                  d   j                  sy| j                  j
                  }|j                  du}t        |j                  xr |j                         }t        j                  | j                  j                  j                        5  | j                  |||||      }|r| j                  j                  t               j"                  }d|j                  v r|j                  d   j"                  }	|	|	j$                  |	j$                  }
t'        |
j(                        D ]}  \  }}|j+                  d      s|j+                  d      s)| j                  j-                  |
j.                  |         }t1        ||      }|j3                  t5        ||dt6                      ddd       | j9                  ||      sCt1        d      }j3                  t5        |t;        t<        j>                        dt@                     tC        | j                  | j                   dtE                      y# 1 sw Y   xY w)	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.
        ra   NTr   requires_dynamic_aliasesr   r!  __r   kwargs)r   return_type)#r  r   r*  plugin_generatedr]   r   r  r   r?  r6   rO  r  rc   rP  get_field_argumentsrn   r  rp   r   r/  r&  
startswith	anal_typer   r)   appendr   r   should_init_forbid_extrar8   r>   r   r   
add_methodr;   )rb   r  r   r!  r   r   r|  r   base_settings_nodebase_settings_init_node	func_typearg_idxrI  analyzed_variable_typer   vars                   rV   r  z(PydanticModelTransformer.add_initializer  s   
 ---diinn6J6J:6V6g6g""--++47	#'(B(B(b6KbKbGb#c &&tyy'8'8'H'HI 	c++)A#' , D %)YY%E%EF[%\%a%a"!3!9!99.@.F.Fz.R.W.W+.:?V?[?[?g$;$@$@	1:9;N;N1O c-GX'2248@S@STW@X (59YY5H5HI\I\]dIe5f2'*85K'LH KK;QSWY`(abc	c* ,,VV<h-CKKgi.@.@&A4ST499dii$HJW3	c 	cs   DI..I7c           	        | j                   j                  t         d| j                   j                  t         d      g      }t        |t	               g      }t        t        d|      |dt              }t        j                  | j                   j                  j                        5  | j                  |ddd|      }ddd       | j                  ||      sCt        d      }j                  t        |t        t         j"                        dt$                     |gz   }t'        | j                   | j(                  d	|t+        | j(                  j,                        d
       y# 1 sw Y   xY w)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_setNTFr{  r~  model_construct)r   r  is_classmethod)r  
named_typeBUILTINS_NAMErA   r;   r   r)   r   r6   rO  rc   rP  r  r  r  r8   r>   r   r   r  r  rC   r   )	rb   r  r   r!  set_stroptional_set_strfields_set_argumentr   r  s	            rV   r  z3PydanticModelTransformer.add_model_construct_method5  s8    ))&&-'=		@T@TXeWffjUk@l?mn$gxz%:;&s=:J'KM]_celm&&tyy'8'8'H'HI 	++dUeal , D	 ,,VV<h-CKKgi.@.@&A4ST#$t+IIII%diinn5	
	 	s   )E&&E/c                   | j                   j                  }|D ]9  }|j                  j                  |j                        }||j
                  }t        |t              r||_        Ot        |t              r1| j                  j                  s| j                  j                          	 t        |      }d| d|j                    d}t#        || j                  | j                          |j%                  |d      }||_        ||_        |j&                  dz   |j                  z   |_        t+        t,        |      |j                  |j                  <   < y# t        $ r t        |      }Y w xY w)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   rp   ro   r)   is_propertyr!   r  final_iterationdeferr   	TypeErrorreprrd   r   r   rg   	_fullnamer%   r   )	rb   r  r  r   r   rX  r  var_strr   s	            rV   r  z#PydanticModelTransformer.set_frozenS  s%   
 yy~~ 	BEzz~~ejj1H#mmc3'&,CO_5dii>W>WIIOO%,"%c(  /wiz#--PQRF-fdiiKll45l9"( $ 3chh >'6tS'A

388$/	B % ,"&s),s   *EE.-E.c                   || j                   vry|dk(  rdt        |t              r|j                  dk(  }n8t        |t              r|j
                  dk(  }nt        || j                  |       yt        |      S |dk(  r/d}t        |t              r|j                  dk(  rd}t        |	      S t        |t              r(|j                  d
v rt        di ||j                  dk(  iS t        || j                  |       y)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)r?  )builtins.Truezbuiltins.Falser  r   )r  ro   r$   r'  r   r   error_invalid_config_valuer  r5  r   rg   )rb   r   r   r  r?  s        rV   r7  z*PydanticModelTransformer.get_config_updater  s    
 t1117?#w'"yyH4C,"xx83*4C@"==$$"&#x(S\\_-L&+#"7JKKc8$9\)\"MdCLLO,K%LMM"4C8rX   c                   | j                   }t        |t              ryt        |t              rt        |j                  t
              r|j                  j                  t        k(  rmt        |j                  |j                        D ]I  \  }}||dk(  r|j                  t        uc S |dk(  s't        |t              xr |j                  dk(   c S  yt        |t               S )zUReturns a boolean indicating whether the field defined in `stmt` is a required field.Fr   r   r  )r<  ro   r&   r   r,  r"   rg   r}   r=  r   r&  rd   r   r   )rG  rE  r   r   s       rV   rg  z(PydanticModelTransformer.get_has_default  s     {{dH%dH%*T[['*Jt{{OcOcguOu
 !DNN; _	T<49#4==<<,, *3 9 ]cllo>]^^_ dL111rX   c                t   | j                   }t        |t              ryt        |t              r7t        |j                  t
              r|j                  j                  t        k(  syt        |j                        D ];  \  }}|dk7  r|j                  |   }t        |t              r|j                  dfc S  y y)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)r<  ro   r&   r   r,  r"   rg   r}   r/  r&  r   r$   r'  )rG  rE  r(  rI  r   s        rV   rk  z'PydanticModelTransformer.get_alias_info  s     {{dH% tX&:dkk7+KPTP[P[PdPdhvPv $T^^4 	"KAx7"))A,C#w'yy%''!	" rX   c           	         | j                   j                  }|D cg c])  }|r|j                  s|j                  |||xs ||      + }}|S c c}w )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.
        )r   r   r   )r  r   r   r   )	rb   r  r   r   r|  r!  r   r   	argumentss	            rV   r  z,PydanticModelTransformer.get_field_arguments  sj     yy~~
  	
 %"9"9	 E2J2Yken  
	 
 
s   .Ac                    |j                   s&| j                  |t        |j                              ry|j                  ry| 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   r?  r  r]   r   )rb   r  r   s      rV   r  z1PydanticModelTransformer.should_init_forbid_extra  sI     &&,,VT&:T:T5UV!!333rX   c                \    | D ]  }|j                   s y |r| D ]  }|j                   y y)zReturns whether any fields on the model have a "dynamic alias", i.e., an alias that cannot be
        determined during static analysis.
        TF)r   r   )r  r?  r   s      rV   r  z1PydanticModelTransformer.is_dynamic_alias_present  sE    
  	E&&	   ;;&  rX   N)
r   r   r   zExpression | Statementr   r1   r]   r\   r   r   )r   r   )r   r   )r   r5  )r3  r5  r   zlist[PydanticModelField] | None)rG  r   r   Iterator[AssignmentStmt])r`  r   r   r  )rG  r   r3  r5  r   zPydanticModelField | None)ru   r%   r   r   r   r   r   r  )r  list[PydanticModelField]r   r5  r!  r   r   r   )r  r  r  r   r   r   )r   r   r   r   r   ModelConfigData | None)rG  r   r   r   )rG  r   r   ztuple[str | None, bool])r  r  r   r   r   r   r|  r   r!  r   r   list[Argument])r  r  r   r5  r   r   )r  r  r?  r   r   r   )r   r   r   r   r  r   ra   r   r  r  r  r^  rR  rS  rl  r  r  r  r7  staticmethodrg  rk  r  r  r  r   rX   rV   r   r     sN   '8 ++ '+ -	+
 ,+ 
+ D26JX2+hRSv
"v
2Av
	"v
p$L$XL
.
8G
VZ
	
<B>4 2 2*  6(  	
 #'  
,4  rX   r   c                  N    e Zd ZdZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZd	dZd
dZddZy)r5  z(Pydantic mypy plugin model config class.Nc                J    || _         || _        || _        || _        || _        y r[   )r  r  r   r  r?  )rb   r  r  r   r  r?  s         rV   ra   zModelConfigData.__init__  s+     ). 0#6 rX   c                p    | j                   j                         D ci c]  \  }}|	|| c}}S c c}}w )zReturns a dict of Pydantic model config names to their values.

        It includes the config if config value is not `None`.
        )__dict__r   )rb   kvs      rV   r  zModelConfigData.get_values_dict  s1    
 "&!4!4!6HA!-1HHHs   
22c                p    |y|j                         j                         D ]  \  }}t        | ||        y)z$Update Pydantic model config values.N)r  r   r   )rb   r   r  r  s       rV   r8  zModelConfigData.update  s;    >**,224 	 DAqD!Q	 rX   c                8    t        | |      t        | ||       yy)zFSet default value for Pydantic model config if config value is `None`.N)r   r   )rb   r   r'  s      rV   rB  zModelConfigData.setdefault  s     4%D#u% &rX   )NNNNN)
r  bool | Noner  r  r   r  r  r  r?  r  r   )r   r  r   r   )r   r   r'  r   r   r   )r   r   r   r   ra   r  r8  rB  r   rX   rV   r5  r5    s[    2 %)"'+(,+/7!7 7 %	7
 &7 )7I &rX   r5  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 defaultsc                <    |j                  d|  d|t               y)zCEmits an error when the model does not have `from_attributes=True`.rq  z$" does not have from_attributes=TruecodeN)r>  	ERROR_ORM)
model_namer   r   s      rV   r   r     s    HHq@A7QZH[rX   c                <    |j                  d|  d|t               y)z0Emits an error when the config value is invalid.zInvalid value for "Config.rq  r  N)r>  ERROR_CONFIG)r   r   r   s      rV   r  r  "  s    HH)$q17HNrX   c                4    | j                  d|t               y)znEmits required dynamic aliases error.

    This will be called when `warn_required_dynamic_aliases=True`.
    z#Required dynamic aliases disallowedr  N)r>  ERROR_ALIASr   r   s     rV   r@  r@  '  s    
 HH2G+HNrX   c                V    d}d|  d}|d| dz  }|j                  ||t               y)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)r>  ERROR_UNEXPECTED)r   r   r   linkfull_messages        rV   r   r   /  sA    
 DDLVHTVWL<TFB[\\LHH\7)9H:rX   c                4    | j                  d|t               y)z;Emits an error when there is an untyped field in the model.zUntyped fields disallowedr  N)r>  ERROR_UNTYPEDr  s     rV   re  re  :  s    HH('HFrX   c                4    | j                  d|t               y)zNEmits an error when `Field` has both `default` and `default_factory` together.z>Field default and default_factory cannot be specified togetherr  N)r>  ERROR_FIELD_DEFAULTSr  s     rV   r   r   ?  s    HHMw]qHrrX   c                D   |j                   }||j                  v rd|j                  |   }	|	j                  rIt        |	j                  t
              r/|j                  j                  j                  |	j                         t        | t              r| j                  d      }
n| j                  dg       }
|r6|xs t        t        |            }t        t        d      |dt         d      g}n+|xs t        |      }t        t        d      |dt               g}||z   }g g g }}}|D ]p  }|j"                  sJ d       |j%                  |j"                         |j%                  |j&                  j(                         |j%                  |j*                         r t-        |||||
      }|r|g|_        t        ||t1        t3               g            }||_         t5        ||      |_        ||_        |j:                  dz   |z   |_        |j>                  |_        ||j                  v r2tA        ||j                        }|j                  |   |j                  |<   |rud|_!        t        ||j6                        }||_         |j<                  |_        d|_"        tG        |tI        d      g|      }|j>                  |_        tK        tL        |      }	ntK        tL        |      }	d|	_        |	|j                  |<   |jN                  j                  j                  j%                  |       y)	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  ro   rp   r   r9  r:  remover1   r  named_generic_typer?   rC   r   r)   r   r   r  r   r   r   r9   	variablesr   r    r4   r   r1  rg   r  r   rD   is_decoratedr  r   r   r%   r   defn)r   r   r   r   r  r   tvar_defr  r   ru   function_typefirstr   r&  ru  r   	signaturer0  r_namer  decs                        rV   r  r  D  s    88D tzzjjJsxx$AHHMM  *#67':;../BBG>-*=!>	#f+y$FG4t!4	#12ItWMN4<D&("b)yI #""H$HH,,-**+"	# Y	9k=YI'j	4uhj\23DDI!)T2DI"DM]]S(4/DN		DI tzz-dDJJ?!ZZ-

6
  dii nnx67;99dC(dD)CDJJtIINNt$rX   c                   | j                  d      syt        j                  dk\  rddl}n	 ddl}t        | d      5 }|j                  |      cddd       S # t
        $ r ddl}|j                  d       Y yw xY w# 1 sw Y   yxY w)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  rfs       rV   r   r     s    
 (
7"	! 
k4	  Bzz"~   	MMfg		 s   A A;A87A8;B)rU   r   r   ztype[Plugin])r   r.   r   r=   )r  r   r   r+   r   r   r   r   )r   r   r   r1   r   r   r   r   )r   r1   r   r   r   r   )r   r   r   z8CheckerPluginInterface | SemanticAnalyzerPluginInterfacer   r   r   r   )r   r+   r   r   r   r   )NNF)r   z8SemanticAnalyzerPluginInterface | CheckerPluginInterfacer   r   r   r   r   r  r  r=   r   r  r  zTypeVarDef | Noner  r   r   r   )r   r   r   zdict[str, Any] | None)~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/   r0   r1   mypy.pluginsr2   mypy.plugins.commonr3   mypy.semanalr4   mypy.server.triggerr5   
mypy.stater6   mypy.typeopsr7   
mypy.typesr8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   mypy.typevarsrC   	mypy.utilrD   mypy.versionrE   mypy_versionpydantic._internalrF   pydantic.versionrG   rH   r  r   r   rh   r  rx   r}   r   r.  r-  r   r  rW   rT   r\   r   r   r   r5  r  r  r  r  r  r  r   r  r@  r   re  r   r  r   r   rX   rV   <module>r     s{   d " 
 % * * % @                 B !   % + 5  0    ( 2 4 & /5%
 !'. = R (5 K   (5 0H<
. s V s l+C +C\#.LP LP^c	 c	L"& "&J n&DjQ	*,BJO(*DjQ24I:V *,F
S !13KZX \
O
O;;N;Y`;	;G
s ""& K%	AK%	K% K% 	K%
 K% K%  K% K% 
K%\}!  545s   "G GG