
    Ѯh                       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.m/Z/ dd	l0m1Z1 dd
l2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQ ddlRmSZS ddlTmUZU ddlVmWZX ddlYmZZZ ddl[m\Z\ 	 ddlFm]Z] dZ_dZ`dZadZbdZcdZddZed Zfd!Zgh d"Zh e\eX      Zieid#k\  rd$nd%Zjd&ZWdKd'Zk G d( d)e7      Zl G d* d+      ZmdLd,Zn G d- d.      Zo G d/ d0      Zp G d1 d2      Zq G d3 d4      Zr ed5d6d7      Zs ed8d9d7      Zt ed:d;d7      Zu ed<d=d7      Zv ed>d?d7      Zw ed>d@d7      Zx ed>dAd7      ZydMdBZzdNdCZ{dOdDZ|	 	 	 	 	 	 	 	 dPdEZ}dOdFZ~dQdGZdQdHZ	 	 	 dR	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dSdIZdTdJZy# e^$ r
 ddlFmOZ] Y w xY w)Uz^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	INVARIANT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)rA   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_validatorrK   )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    _/home/www/academy-backend.kofcorporation.com/venv/lib/python3.12/site-packages/pydantic/mypy.pypluginrX   s   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 )rU   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     rW   rb   zPydanticPlugin.__init__   s4    1': ..668!rY   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     rW   	<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rc   rh   syms      rW   get_base_class_hookz"PydanticPlugin.get_base_class_hook   sG    ))(3:chh1P388<<PP@@@rY   c                .    |t         k(  r| j                  S y)z,Update Pydantic `ModelMetaclass` definition.N)MODEL_METACLASS_FULLNAME)_pydantic_model_metaclass_marker_callbackrc   rh   s     rW   get_metaclass_hookz!PydanticPlugin.get_metaclass_hook   s    //AAArY   c                h    | j                  |      }|r|j                  t        k(  r| j                  S y)z/Adjust the return type of the `Field` function.N)ro   rh   FIELD_FULLNAME_pydantic_field_callbackru   s      rW   get_function_hookz PydanticPlugin.get_function_hook   s/    ))(33<<>1000rY   c                2    |j                  d      rt        S y)z-Adjust return type of `from_orm` method call.z	.from_ormN)endswithfrom_attributes_callbackr{   s     rW   get_method_hookzPydanticPlugin.get_method_hook   s    [)++rY   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_TUPLEr3   dataclass_class_maker_callbackr{   s     rW   get_class_decorator_hookz'PydanticPlugin.get_class_decorator_hook   s$    
 )).@6.I===rY   c                    | j                   S )zjReturn all plugin config data.

        Used by mypy to determine if cache needs to be discarded.
        )r`   )rc   ctxs     rW   report_config_dataz!PydanticPlugin.report_config_data   s    
    rY   c                    t        |j                  |j                  |j                  | j                        }|j                         S r\   )PydanticModelTransformerclsreasonapir^   	transform)rc   r   transformers      rW   rt   z3PydanticPlugin._pydantic_model_class_maker_callback   s3    .sww

CGGTM_M_`$$&&rY   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   )rc   r   info_metaclasss      rW   rz   z8PydanticPlugin._pydantic_model_metaclass_marker_callback   s^     7788NNN~>&&(BDI;?N8 JrY   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\   )rp   rA   )rk   args     rW   rm   z:PydanticPlugin._pydantic_field_callback.<locals>.<genexpr>   s     HC:c;7Hs   c              3  "   K   | ]  }  y wr\    )rk   _default_any_types     rW   rm   z:PydanticPlugin._pydantic_field_callback.<locals>.<genexpr>   s     -M1.>-Ms   )default_return_typecallee_arg_namesr   +error_default_and_default_factory_specifiedr   context	arg_typesrp   r   r=   itemsr:   ret_typer   alltuple)
rc   r   default_argsdefault_factory_argsdefault_typedefault_argdefault_factory_typer   r   r   s
            @rW   r   z'PydanticPlugin._pydantic_field_callback   sM    22##A&)3g5gg3##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rY   rd   r+   returnNone)rh   strr   z(Callable[[ClassDefContext], bool] | None)rh   r   r   z(Callable[[ClassDefContext], None] | None)rh   r   r   z(Callable[[FunctionContext], Type] | None)rh   r   r   z&Callable[[MethodContext], Type] | None)r   r1   r   dict[str, Any])r   r-   r   bool)r   r-   r   r   )r   r.   r   r>   )__name__
__module____qualname____doc__rb   rw   r|   r   r   r   r   rt   rz   r   __classcell__)re   s   @rW   rU   rU      s9    #"
!'@. rY   rU   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toolrJ   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   rd   toml_configconfigkeysettingr^   s          rW   rb   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+,rY   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   )rc   r   s     rW   r_   zPydanticPluginConfig.to_data!  s&    37>>BCWT3''BBBs   &Nr   r   r   )r   r   r   r   r   __annotations__rb   r_   r   rY   rW   r]   r]      s3    I #''##,&CrY   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   rp   r@   itemr:   r   r;   re   r   error_unexpected_behaviorr   r   r   metadatar   METADATA_KEYerror_from_attributesname)r   ctx_type
model_typedetailpydantic_metadatar   s         rW   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"""rY   c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 	 	 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   )	rc   r   r   r   r   r   r   r   r   s	            rW   rb   zPydanticModelField.__init__@  s>     	
!2&			rY   c                    | j                  ||||      }|r| j                  ||      nt        t        j                        }t        ||d|s| j                  rt              S t              S )zABased on mypy.plugins.dataclasses.DataclassAttribute.to_argument.N)variabletype_annotationinitializerkind)	to_varr	   r9   r?   explicitr   r   r   r   )	rc   current_infotypedforce_optional	use_aliasr   force_typevars_invariantr   r   s	            rW   to_argumentzPydanticModelField.to_argumentT  so     ;;|S)=UVAF$**<=GT]TfTfLg+"0D4D4D	
 	
 KT	
 	
rY   c                d   |rFt        | j                  t              r,| j                  j                         }t        |_        || _        | j                  t        | j                  dd      t        j                  |j                  j                        5  t        |      }|r-|j                  D ]  }t        |t              st        |_          t        | j                  | j                  j                  j                   |i      cddd       S | j                  S # 1 sw Y   | j                  S xY w)zABased on mypy.plugins.dataclasses.DataclassAttribute.expand_type.N	self_type)rp   r   rA   copy_modifiedr   variancer   r   r7   strict_optional_setrd   strict_optionalrD   r   r	   r   id)rc   r   r   r   modified_typefilled_with_typevarsr   s          rW   r	   zPydanticModelField.expand_typeg  s    
 $
 $))[1 $		 7 7 9)2&)	99 WTYYT%J%V
 **3;;+F+FG ^'4\'B$+388 5%c;7+4CL5 #499tyy/B/B/E/EG[.\]^ ^ yy^ yys   -DADD/c                    |r| j                   | j                   }n| j                  }t        || j                  |||            S )z<Based on mypy.plugins.dataclasses.DataclassAttribute.to_var.)r   r   r*   r	   )rc   r   r   r   r   r   s         rW   r   zPydanticModelField.to_var  s?     /::D99D4)),=UVWWrY   c                    | j                   sJ | 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   	serializerc   s    rW   r  zPydanticModelField.serialize  sV    yyyIIZZ!%!7!7++IIkkII'')
 	
rY   c                n    |j                         }t        |j                  d      |      } | d||d|S )ABased on mypy.plugins.dataclasses.DataclassAttribute.deserialize.r   )r   r   r   )copyr4   pop)r   r   datar   typs        rW   deserializezPydanticModelField.deserialize  s9     yy{(&)93?/$/$//rY   c                    | j                   Yt        j                  |j                  j                        5  t        | j                   || j                        | _         ddd       yy# 1 sw Y   yxY w)zxExpands type vars in the context of a subtype when an attribute is inherited
        from a generic super type.
        N)r   r7   r   rd   r   r8   r   )rc   sub_typer   s      rW   expand_typevar_from_subtypez.PydanticModelField.expand_typevar_from_subtype  sb     99 **3;;+F+FG T3DIIxS	T T !T Ts   'A''A0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   r2   r   r   r   r   F)r   r)   r   r2   r   r   r   r  )
r   r)   r   r2   r   r   r   r   r   r*   r   r   )r   r)   r  r   r   r2   r   r   )r  r)   r   r2   r   r   )r   r   r   r   rb   r   r	   r   r  classmethodr  r  r   rY   rW   r   r   =  s-   ?   	
     (

 
 	

 
 -
 #'
 

( ns$+Jfj	D */XX -X 	X
 #'X 
X
 0 0TrY   r   c                  0    e Zd ZdZd Zedd       ZddZy)PydanticModelClassVarzBased on mypy.plugins.dataclasses.DataclassAttribute.

    ClassVars are ignored by subclasses.

    Attributes:
        name: the ClassVar name
    c                    || _         y r\   r   )rc   r   s     rW   rb   zPydanticModelClassVar.__init__  s	    	rY   c                2    |j                         } | di |S )r	  r   )r
  )r   r  s     rW   r  z!PydanticModelClassVar.deserialize  s     yy{{T{rY   c                    d| j                   iS )r  r   r  r  s    rW   r  zPydanticModelClassVar.serialize  s     DII
 	
rY   N)r  r   r   r  r  )r   r   r   r   rb   r  r  r  r   rY   rW   r  r    s%      

rY   r  c                  F   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%dZed&d       Zed'd       Z	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 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^   )rc   r   r   r   r^   s        rW   rb   z!PydanticModelTransformer.__init__  s!     		*rY   c                   | j                   j                  }t        d |j                  dd D              }| j	                         }| j                  ||      \  }}||y|D ]  }|j                   y t        d |j                  dd D              }| j                  ||||       |s| j                  |||       | j                  || j                  |j                  du        | j                          |D ci c]  }|j                  |j                          c}|D ci c]  }|j                  |j                          c}|j                         d|j                   t"        <   yc c}w 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
        c              3  @   K   | ]  }t         |j                  v   y wr\   )ROOT_MODEL_FULLNAMErh   rj   s     rW   rm   z5PydanticModelTransformer.transform.<locals>.<genexpr>  s     [T/4==@[s   NFc              3  B   K   | ]  }|j                   t        k(    y wr\   )rh   BASESETTINGS_FULLNAMErj   s     rW   rm   z5PydanticModelTransformer.transform.<locals>.<genexpr>  s     [T$--+@@[rn   T)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_signaturesr   r  get_values_dictr   r   )	rc   r   is_root_modelr   r-  r.  fieldis_settings	class_vars	            rW   r   z"PydanticModelTransformer.transform  sY    yy~~[TXXVYWY][[$$&!??V
>Z/ 	Ezz!	 [TXXVYWY][[VV[-H++FFKH		&--42GH((* CIIuzz5??#44IR\]Y9>>9+>+>+@@],,.'
l#  J]s   ;"E+#"E0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_namesrp   r%   value)rk   ir   	first_decs      rW   rm   zGPydanticModelTransformer.adjust_decorator_signatures.<locals>.<genexpr>  sO        &3 &//2f<rCQXAYr^a^g^gkr^rr s   AATN)r$  r   namesr   rp   rq   r   original_decoratorsr   calleer    rh   DECORATOR_FULLNAMESMODEL_VALIDATOR_FULLNAMErr   	enumerater   funcis_class)rc   r   rv   rA  s      @rW   r4  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*#	2rY   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              rdt!        |j                  j"                  |j                  j$                        D ]+  \  }
}|
	|j                  | j	                  |
|d             - nt        |j                  t&              r|j                  j                  D ]A  \  }}t        |t(              s|j                  | j	                  |j*                  |             C nt        |t              r|j                  dk7  ro|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)	lax_extraConfigzYSpecifying config in two places is ambiguous, use either Config attribute or class kwargsr   r   )%r$  ModelConfigData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_dependencyr6   rh   
setdefault)rc   r   r   has_config_kwargshas_config_from_namespacer   exprconfig_datastmtlhsarg_namer   key_expr
value_exprsubstmtr   r?  s                    rW   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 ]##+$d&<&<XsVZ&<&[\]  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!!$./	/ rY   c                   | j                   }i }i }t        |j                  j                  dd       D ]v  }t        |j
                  vr| j                  j                  t        |j                               |j
                  t           d   j                         D ]  \  }}t        j                  ||| j                        }	|	j                  |j                  | j                         |	||<   |j                  j                  j                  |      }
|
sz|
j                   st#        |
j                   t$              r| j                  j'                  d|
j                           |j
                  t           d   j                         D ]  \  }}t(        j                  |      ||<    y t+               }t+               }| j-                  |j.                        D ]  }| j1                  |||      }t#        |t              rb|j2                  d   }|r&|j4                  dk7  rt7        | j                  |       ]|j9                  |j4                         |||j4                  <   t#        |t(              s|j2                  d   }|j9                  |j4                         |||j4                  <    t;        |j=                               t;        |j=                               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&  rZ  r6   rh   r   r   r  r  rB  r   rq   rp   r*   rW  r  set%_get_assignment_statements_from_blockrR  $collect_field_or_class_var_from_stmtrT  r    error_extra_fields_on_root_modeladdlistvalues)rc   rK  r6  r   found_fieldsfound_class_varsr   r   r  r7  sym_nodecurrent_field_namescurrent_class_vars_namesr`  maybe_fieldra  s                   rW   r0  z6PydanticModelTransformer.collect_fields_and_class_varsq  s]    ii 79=?SXX\\!B/0 	QD 4==0 II++,A$--,PQ"mmL9(CIIK 
d*66tT499M
 11#((DIIF%*T"88>>--d3jPS6TIINNQ   #mmL9,GMMO Q
d)>)J)J4)P &Q5	Q< ),-0U >>sxxH 	9DCCD,XhiK+'9:ll1o SXX%74TYYE'++CHH5-8L*K)>?ll1o(,,SXX6-8 *	9 L'')*D1A1H1H1J,KKKrY   c              #    K   |j                   D ](  }|j                  r| j                  |      E d {    * |j                  ;|j                  j                  s$| j                  |j                        E d {    y y y 7 N7 	wr\   )rS  is_unreachablerj  	else_body)rc   r`  rS  s      rW   ,_get_assignment_statements_from_if_statementzEPydanticModelTransformer._get_assignment_statements_from_if_statement  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\   )rS  rp   r   r   ry  )rc   blockr`  s      rW   rj  z>PydanticModelTransformer._get_assignment_statements_from_block  sN     JJ 	SD$/
D&)LLTRRR		S Ss   5AAAAc           
        | 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|j                  |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t1        |j                        S t3        |j4                        }t        |t6              r5|j4                  j                  dk(  r| j                  j+                  d|       | j9                  |      }	|j4                  |j:                  rw|j<                  rk| j                  j?                  |j                  d      }
|
r|
|_        n:| j                  j+                  d	|       tA        tB        jD                        |_        | jG                  |      \  }}|r8|jH                  s,| jJ                  jL                  rtO        | j                  |       | jQ                  ||j                  |      }tS        |j                  ||	||jT                  |jV                  ||j                  
      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   rK  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$  rT  rp   r    rG   is_valid_field_namer   
new_syntaxrU  r   rD  rh   rE  error_untyped_fieldsr&  r   rB  r   rq   r"   r(   rW  r*   is_classvarr  rC   r   r;   get_has_defaultr}  is_inferredanalyze_simple_literal_typer9   r?   
from_errorget_alias_infor!  r^   r   rY  _infer_dataclass_attr_init_typer   r   r   )rc   r`  rK  r.  r   ra  rv   rq   	node_typer   r  r   r   	init_types                 rW   rk  z=PydanticModelTransformer.collect_field_or_class_var_from_stmt  s    iill1o#x(0K0KCHH0UY\YaYaesYs4;;1t{{118<t{{1188(CKK&&--66:MM
 xx:% !D1ll1o#x(**3884N8Rhhnn  *; xxdO,
 dI&IINNX $$
  (22 $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	!/#;;	
 		
rY   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__rS   z(Unsupported signature for "__set__" in ""zUnsupported "__set__" in ")r   implicitrC   rp   r;   r   rq   r   get_containing_type_infor8   r9   r?   unannotatedr:   	arg_kindsr   r
   r   r&  rW  r   )	rc   rv   r   r   r   tsetter
super_infosetter_types	            rW   r  z8PydanticModelTransformer._infer_dataclass_attr_init_type=  sG   
 ((<<NCHH% !X&NI&&++w/VV<<YG
!!z;;"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rY   c           	        d| j                   j                  j                  v r.| j                   j                  j                  d   j                  sy| j                  j
                  }|j                  du}t        |j                  xr |j                         }| j                  |||||d      }|r3t        dk  r*|d   j                  t        k(  rt        nt        |d   _        |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/                  |j0                  |         }t3        ||      }|j5                  t7        ||dt                      | j9                  ||      sCt3        d	      }|j5                  t7        |t;        t<        j>                        dt@                     tC        | j                  | j                   d|tE               
       y)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   r8  r   )r   r   r   r   __r   kwargs)r   return_type)#r$  r   rB  plugin_generatedr^   r   r!  r   rX  get_field_argumentsr   r   r   r   r   r&  ro   r,  rq   r   rG  r>  
startswith	anal_typer   r*   appendr   should_init_forbid_extrar9   r?   r   r   
add_methodr<   )rc   r-  r   r8  r6  r   r   r  r   base_settings_nodebase_settings_init_node	func_typearg_idxrb  analyzed_variable_typer   vars                    rW   r1  z(PydanticModelTransformer.add_initializerc  s
    ---diinn6J6J:6V6g6g""--++47	#'(B(B(b6KbKbGb#c ''%=#%) ( 
 /9< '+1glli&?7WDGL!%!A!ABW!X!]!]/555*<*B*B:*N*S*S'*6;R;W;W;c 7 < <I-6y7J7J-K _)#..t4H<O<OPS<T$151D1DYEXEXY`Ea1b.#&x1G#HHX7MtU\$]^_ ,,VV<h-CKKgi.@.@&A4ST499dii$HJWrY   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_setNTF)r   r  r   r8  r  model_construct)r   r  is_classmethod)r&  
named_typeBUILTINS_NAMErB   r<   r   r*   r   r7   r   rd   r   r  r  r  r9   r?   r   r   r  r$  rD   r   )	rc   r-  r   r8  set_stroptional_set_strfields_set_argumentr   r  s	            rW   r2  z3PydanticModelTransformer.add_model_construct_method  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 ]:  }|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   rB  r   r   rq   rp   r*   is_propertyr"   r&  final_iterationdeferr   	TypeErrorreprre   r   r   rh   	_fullnamer&   r   )
rc   r-  r   r  r   r7  rr  r  var_strr   s
             rW   r3  z#PydanticModelTransformer.set_frozen  s'   
 yy~~ 	BEzz~~ejj1H#mmc3'&,CO_5dii>W>WIIOO%,"%c(  /wiz#--PQRF-fdiiKll4l>"( $ 3chh >'6tS'A

388$/	B % ,"&s),s   *EE/.E/c                   || j                   vry|dk(  rft        |t              r|j                  dk(  }n:t        |t              r|j
                  dk(  }n|s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)rX  )builtins.Truezbuiltins.Falser  r   )r"  rp   r%   r?  r   r   error_invalid_config_valuer&  rN  r    rh   )rc   r   r   rL  r  rX  s         rW   rP  z*PydanticModelTransformer.get_config_update  s    
 t1117?#w'"yyH4C,"xx83  /tTYYD"==$$"&#x(S\\_-L&+#"7JKKc8$9\)\"MdCLLO,K%LMM"4C8rY   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  )rU  rp   r'   r   rD  r#   rh   r~   rV  r   r>  re   r   r    )r`  r^  r   r   s       rW   r  z(PydanticModelTransformer.get_has_default  s     {{dH%dH%*T[['*Jt{{OcOcguOu
 !DNN; _	T<49#4==<<,, *3 9 ]cllo>]^^_ dL111rY   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)rU  rp   r'   r   rD  r#   rh   r~   rG  r>  r   r%   r?  )r`  r^  r@  rb  r   s        rW   r  z'PydanticModelTransformer.get_alias_info  s     {{dH% tX&:dkk7+KPTP[P[PdPdhvPv $T^^4 	"KAx7"))A,C#w'yy%''!	" rY   c                    | j                   j                  }|D cg c]5  }|r|j                  s%|j                  |||xs ||| j                  |      7 }	}|	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   r   r   r&  )
rc   r-  r   r   r  r8  r   r   r7  	argumentss
             rW   r  z,PydanticModelTransformer.get_field_arguments#  su     yy~~  
 %"9"9 7F;#II)A  
	 
 
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   rX  r  r^   r   )rc   r-  r   s      rW   r  z1PydanticModelTransformer.should_init_forbid_extra?  sI     &&,,VT&:T:T5UV!!333rY   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-  rX  r7  s      rW   r  z1PydanticModelTransformer.is_dynamic_alias_presentL  sE    
  	E&&	   ;;&  rY   N)
r   r   r   zExpression | Statementr   r2   r^   r]   r   r   )r   r   )r   r   )r   rN  )rK  rN  r6  r   r   zJtuple[list[PydanticModelField] | None, list[PydanticModelClassVar] | None])r`  r   r   Iterator[AssignmentStmt])r{  r   r   r  )r`  r   rK  rN  r.  z dict[str, PydanticModelClassVar]r   z1PydanticModelField | PydanticModelClassVar | None)rv   r&   r   r   r   r   r   r  )
r-  list[PydanticModelField]r   rN  r8  r   r6  r   r   r   )r-  r  r   rN  r8  r   r   r   )r-  r  r   r2   r  r   r   r   r  )r   r   r   r   rL  r   r   ModelConfigData | None)r`  r   r   r   )r`  r   r   ztuple[str | None, bool])r-  r  r   r   r   r   r  r   r8  r   r   r   r   list[Argument])r-  r  r   rN  r   r   )r-  r  rX  r   r   r   )r   r   r   r   r"  r   rb   r   r4  r/  r0  ry  rj  rk  r  r1  r2  r3  rP  staticmethodr  r  r  r  r  r   rY   rW   r   r     s   '8 ++ '+ -	+
 ,+ 
+#J26JX?L+?L<@?L	S?LBRS{
"{
2A{
Oo{
	:{
z$L,X.,X8G,XVZ,Xko,X	,X\
.
8G
VZ
	
<B>!F 2 2*  D */(  	
 #'  #' 
84  rY   r   c                  N    e Zd ZdZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZd	dZd
dZddZy)rN  z(Pydantic mypy plugin model config class.Nc                J    || _         || _        || _        || _        || _        y r\   )r  r  r   r!  rX  )rc   r  r  r   r!  rX  s         rW   rb   zModelConfigData.__init__^  s+     ). 0#6 rY   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   )rc   kvs      rW   r5  zModelConfigData.get_values_dictl  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)r5  r   r   )rc   r   r  r  s       rW   rQ  zModelConfigData.updates  s;    >**,224 	 DAqD!Q	 rY   c                8    t        | |      t        | ||       yy)zFSet default value for Pydantic model config if config value is `None`.N)r   r   )rc   r   r?  s      rW   r[  zModelConfigData.setdefaultz  s     4%D#u% &rY   )NNNNN)
r  bool | Noner  r  r   r  r!  r  rX  r  r   )r   r  r   r   )r   r   r?  r   r   r   )r   r   r   r   rb   r5  rQ  r[  r   rY   rW   rN  rN  [  s[    2 %)"'+(,+/7!7 7 %	7
 &7 )7I &rY   rN  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 subclassc                <    |j                  d|  d|t               y)zCEmits an error when the model does not have `from_attributes=True`.r  z$" does not have from_attributes=TruecodeN)rW  	ERROR_ORM)
model_namer   r   s      rW   r   r     s    HHq@A7QZH[rY   c                <    |j                  d|  d|t               y)z0Emits an error when the config value is invalid.zInvalid value for "Config.r  r  N)rW  ERROR_CONFIG)r   r   r   s      rW   r  r    s    HH)$q17HNrY   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)rW  ERROR_ALIASr   r   s     rW   rY  rY    s    
 HH2G+HNrY   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)rW  ERROR_UNEXPECTED)r   r   r   linkfull_messages        rW   r   r     sA    
 DDLVHTVWL<TFB[\\LHH\7)9H:rY   c                4    | j                  d|t               y)z;Emits an error when there is an untyped field in the model.zUntyped fields disallowedr  N)rW  ERROR_UNTYPEDr  s     rW   r  r    s    HH('HFrY   c                4    | j                  d|t               y)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)rW  ERROR_EXTRA_FIELD_ROOT_MODELr  s     rW   rl  rl    s    HHA7QmHnrY   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)rW  ERROR_FIELD_DEFAULTSr  s     rW   r   r     s    HHMw]qHrrY   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   rB  r  rp   rq   r   rR  rS  remover2   r  named_generic_typer@   rD   r   r*   r   r   r  r   r   r   r:   	variablesr   r!   r5   r   rI  rh   r  r   rE   is_decoratedr  r   r    r&   r   defn)r   r   r   r   r  r   tvar_defr  r   rv   function_typefirstr   r>  r  r   	signaturerH  r_namer  decs                        rW   r  r    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$rY   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       rW   r   r     s    
 (
7"	! 
k4	  Bzz"~   	MMfg		 s   A A;A87A8;B)rV   r   r   ztype[Plugin])r   r/   r   r>   )r  r   r   r,   r   r   r   r   )r   r   r   r2   r   r   r   r   )r   r2   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)   r*   mypy.optionsr+   mypy.pluginr,   r-   r.   r/   r0   r1   r2   mypy.pluginsr3   mypy.plugins.commonr4   mypy.semanalr5   mypy.server.triggerr6   
mypy.stater7   mypy.typeopsr8   
mypy.typesr9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   mypy.typevarsrD   	mypy.utilrE   mypy.versionrF   mypy_versionpydantic._internalrG   pydantic.versionrH   rI   r  r   r   ri   r,  r)  ry   r~   r   rF  rE  r   r  rX   rU   r]   r   r   r  r   rN  r  r  r  r  r  r  r  r   r  rY  r   r  rl  r   r  r   r   rY   rW   <module>r     s   d " 
 % * * % @! ! ! ! ! ! ! ! !D !   % + 5  0    ( 2 4 & /5%
 !'. = 5 R (5 K   (5 0H<
. s V s l+C +C\#.pT pTf
 
2O
 O
d"& "&J n&DjQ	*,BJO(*DjQ24I:V *,F
S !13KZX ()9;^`jk \
O
O;;N;Y`;	;G
o
s ""& U%	AU%	U% U% 	U%
 U% U%  U% U% 
U%pq%  5445s   $G   G/.G/