o
    $6d!                     @  s6  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. dd	l/m0Z0 dd
l1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZP ddlQmRZR ddlSmTZT ddlUmVZW ddlXmYZY ddlZm[Z[ zddlEm\Z\ W n e]y   ddlEmNZ\ Y nw dZ^dZ_dZ`dZadZbdZcdZdd Zeh d!Zfe[eWZgegd"krd#nd$Zhd%ZVdpd*d+ZiG d,d- d-e6ZjG d.d/ d/Zkdqd3d4ZlG d5d6 d6ZmG d7d8 d8ZnG d9d: d:Zoed;d<d=Zped>d?d=Zqed@dAd=ZredBdCd=ZsedDdEd=ZtedDdFd=ZudrdMdNZvdsdQdRZwdtdSdTZxdudWdXZydtdYdZZzdvd[d\Z{			]dwdxdjdkZ|dydndoZ}dS )zz^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>   z0pydantic.functional_serializers.model_serializerrI   z*pydantic.functional_serializers.serializerz.pydantic.deprecated.class_validators.validatorz.pydantic.functional_validators.field_validatorz3pydantic.deprecated.class_validators.root_validator)r   i  builtins__builtins__   versionstrreturntype[Plugin]c                 C  s   t S )a!  `version` is the mypy version string.

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

    Args:
        version: The mypy version string.

    Return:
        The Pydantic mypy plugin type.
    )PydanticPlugin)rM    rR   dC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\pydantic/mypy.pypluginq   s   rT   c                      s|   e Zd ZdZd' fddZd(ddZd)ddZd*ddZd+ddZd)ddZ	d,ddZ
d-dd Zd.d!d"Zd/d%d&Z  ZS )0rQ   zThe Pydantic mypy plugin.optionsr)   rO   Nonec                   s&   t || _| j | _t | d S N)PydanticPluginConfigplugin_configto_data_plugin_datasuper__init__)selfrU   	__class__rR   rS   r]      s   
zPydanticPlugin.__init__fullnamerN   (Callable[[ClassDefContext], bool] | Nonec                 C  s:   |  |}|rt|jtrtdd |jjD r| jS dS )zUpdate Pydantic model class.c                 s      | ]}|j tkV  qd S rW   )ra   BASEMODEL_FULLNAME.0baserR   rR   rS   	<genexpr>       z5PydanticPlugin.get_base_class_hook.<locals>.<genexpr>N)lookup_fully_qualified
isinstancenoder'   anymro$_pydantic_model_class_maker_callbackr^   ra   symrR   rR   rS   get_base_class_hook   s
   
z"PydanticPlugin.get_base_class_hook(Callable[[ClassDefContext], None] | Nonec                 C  s   |t kr| jS dS )z,Update Pydantic `ModelMetaclass` definition.N)MODEL_METACLASS_FULLNAME)_pydantic_model_metaclass_marker_callbackr^   ra   rR   rR   rS   get_metaclass_hook   s   z!PydanticPlugin.get_metaclass_hook(Callable[[FunctionContext], Type] | Nonec                 C  s"   |  |}|r|jtkr| jS dS )z/Adjust the return type of the `Field` function.N)rj   ra   FIELD_FULLNAME_pydantic_field_callbackrp   rR   rR   rS   get_function_hook   s   
z PydanticPlugin.get_function_hook&Callable[[MethodContext], Type] | Nonec                 C  s   | drtS dS )z-Adjust return type of `from_orm` method call.z	.from_ormN)endswithfrom_attributes_callbackrv   rR   rR   rS   get_method_hook   s   
zPydanticPlugin.get_method_hookc                 C  s   |t krtdk rtjS dS )zMark pydantic.dataclasses as dataclass.

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

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

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

        In particular:
        * Check whether the default and default_factory argument is specified.
        * Output an error if both are specified.
        * Retrieve the type of the argument which is specified, and use it as return type for the function.
        r   defaultz0"default" is no longer first argument in Field()r   default_factoryz9"default_factory" is no longer second argument in Field()argsNc                 s  s    | ]}t |tV  qd S rW   )rk   r?   )rf   argrR   rR   rS   rh      ri   z:PydanticPlugin._pydantic_field_callback.<locals>.<genexpr>c                 3  s    | ]} V  qd S rW   rR   )rf   _Zdefault_any_typerR   rS   rh      s    )default_return_typeZcallee_arg_namesr   +error_default_and_default_factory_specifiedr   context	arg_typesrk   r   r;   itemsr8   ret_typer   alltuple)	r^   r   Zdefault_argsZdefault_factory_argsZdefault_typeZdefault_argZdefault_factory_typer   r   rR   r   rS   rz      s4   





z'PydanticPlugin._pydantic_field_callbackrU   r)   rO   rV   )ra   rN   rO   rb   )ra   rN   rO   rs   )ra   rN   rO   rx   )ra   rN   rO   r|   )r   r/   rO   r   )r   r+   rO   r   )r   r+   rO   rV   )r   r,   rO   r<   )__name__
__module____qualname____doc__r]   rr   rw   r{   r   r   r   ro   ru   rz   __classcell__rR   rR   r_   rS   rQ      s    

	



	

rQ   c                   @  sJ   e Zd ZU dZdZded< ded< ded< ded< dddZdddZdS )rX   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   rU   r)   rO   rV   c                 C  s   |j d u rd S t|j }|d ur:|di di }| jD ]}||d}t|ts1td| t| || qd S t }|	|j  | jD ]}|j
t|dd}t| || qFd S )NZtoolrH   Fz/Configuration value must be a boolean for key: )fallback)config_file
parse_tomlget	__slots__rk   r   
ValueErrorsetattrr   read
getbooleanCONFIGFILE_KEY)r^   rU   Ztoml_configconfigkeyZsettingrY   rR   rR   rS   r]     s"   




zPydanticPluginConfig.__init__r   c                   s    fdd j D S )z/Returns a dict of config names to their values.c                   s   i | ]}|t  |qS rR   )r   )rf   r   r^   rR   rS   
<dictcomp>!      z0PydanticPluginConfig.to_data.<locals>.<dictcomp>)r   r   rR   r   rS   rZ        zPydanticPluginConfig.to_dataNr   rO   r   )r   r   r   r   r   __annotations__r]   rZ   rR   rR   rR   rS   rX      s   
 

rX   r   r-   r<   c                 C  s   | j }t|tr|j}t|trt|jtr|j}nt|tr"|}nd| d|jj d}t	|| j
| j | jS |j jt}|du rF| jS |di d}|dur]t|j j| j
| j | jS )z1Raise an error if from_attributes is not enabled.z
ctx.type: 
 (of type )Nr   from_attributesT)r   rk   r>   itemr8   r   r9   r`   r   error_unexpected_behaviorr   r   r   metadatar   METADATA_KEYerror_from_attributesname)r   Zctx_typeZ
model_typedetailZpydantic_metadatar   rR   rR   rS   r~   $  s"   

r~   c                   @  sZ   e Zd ZdZd,ddZd-ddZd.ddZd/ddZd0d d!Ze	d1d%d&Z
d2d)d*Zd+S )3PydanticModelFieldz5Based on mypy.plugins.dataclasses.DataclassAttribute.r   rN   alias
str | Nonehas_dynamic_aliasr   has_defaultlineintcolumnr   Type | Noner   r'   c	           	      C  s4   || _ || _|| _|| _|| _|| _|| _|| _d S rW   )r   r   r   r   r   r   r   r   )	r^   r   r   r   r   r   r   r   r   rR   rR   rS   r]   >  s   
zPydanticModelField.__init__current_infotypedforce_optional	use_aliasrO   r   c                 C  s>   t | |||r| |nttjd|s| jrtdS tdS )zABased on mypy.plugins.dataclasses.DataclassAttribute.to_argument.N)variabletype_annotationZinitializerkind)	r   to_varr   r7   r=   explicitr   r   r
   )r^   r   r   r   r   rR   rR   rS   to_argumentR  s   
zPydanticModelField.to_argumentc                 C  s6   | j dur| jjdurt| j | jjjt|iS | j S )zABased on mypy.plugins.dataclasses.DataclassAttribute.expand_type.N)r   r   	self_typer   idrB   )r^   r   rR   rR   rS   r   [  s   zPydanticModelField.expand_typer(   c                 C  s,   |r| j dur| j }n| j}t|| |S )z<Based on mypy.plugins.dataclasses.DataclassAttribute.to_var.N)r   r   r(   r   )r^   r   r   r   rR   rR   rS   r   e  s   zPydanticModelField.to_varr   c                 C  s0   | j s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   	serializer   rR   rR   rS   r   n  s   
zPydanticModelField.serializedatar   r0   c                 C  s,   |  }t|d|}| d||d|S )zABased on mypy.plugins.dataclasses.DataclassAttribute.deserialize.r   )r   r   NrR   )copyr2   pop)r   r   r   r   typrR   rR   rS   deserialize{  s   zPydanticModelField.deserializesub_typerV   c                 C  s$   | j durt| j || j| _ dS dS )zxExpands type vars in the context of a subtype when an attribute is inherited
        from a generic super type.
        N)r   r6   r   )r^   r   rR   rR   rS   expand_typevar_from_subtype  s   
z.PydanticModelField.expand_typevar_from_subtypeN)r   rN   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   )
r   r'   r   r   r   r   r   r   rO   r   )r   r'   rO   r   )r   r'   r   r   rO   r(   )rO   r   )r   r'   r   r   r   r0   rO   r   )r   r'   rO   rV   )r   r   r   r   r]   r   r   r   r   classmethodr   r   rR   rR   rR   rS   r   ;  s    


	


	r   c                   @  s   e Zd ZU dZh dZded< dTddZdUddZdVddZdWddZ	dXddZ
dYd d!ZdZd$d%Zd[d(d)Zd\d1d2Zd]d7d8Zd]d9d:Zd^d<d=Zd_dAdBZed`dCdDZedadFdGZdbdLdMZdcdNdOZedddQdRZdSS )er   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.
    >   populate_by_namer   frozenalias_generatorextrazset[str]tracked_config_fieldsr   r   r   Expression | Statementr   r0   rY   rX   rO   rV   c                 C  s   || _ || _|| _|| _d S rW   )_clsZ_reason_apirY   )r^   r   r   r   rY   rR   rR   rS   r]     s   
z!PydanticModelTransformer.__init__r   c                 C  s   | j j}|  }| |}|du rdS |D ]
}|jdu r dS qtdd |jdd D }| ||| | ||| | j	||j
du d |   dd	 |D | d
|jt< dS )a  Configures the BaseModel subclass according to the plugin settings.

        In particular:

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

        Teach mypy this by marking any function whose outermost decorator is a `validator()`,
        `field_validator()` or `serializer()` call as a `classmethod`.
        r   c                 3  s6    | ]\}} j | d kot|to|jdkV  qdS )modeafterN)	arg_namesrk   r#   value)rf   ir   Z	first_decrR   rS   rh     s
     
zGPydanticModelTransformer.adjust_decorator_signatures.<locals>.<genexpr>TN)r   r   namesr   rk   rl   r   Zoriginal_decoratorsr   calleer   ra   DECORATOR_FULLNAMESMODEL_VALIDATOR_FULLNAMErm   	enumerater   funcis_class)r^   r   rq   rR   r   rS   r     s    

z4PydanticModelTransformer.adjust_decorator_signaturesModelConfigDatac                 C  s  | j }t }d}d}|j D ]\}}| ||}|r"d}|| qd}|jjD ]}t|t	t
fs3q)t|t	r|jd }	t|	trG|	jdkrHq)t|jtrkt|jj|jjD ]\}
}|
du r`qW|| |
| qWnNt|jtr|jjD ]\}}t|tsqu|| |j| qun.t|t
r|jdkrq)|jjD ]}t|t	sq|jd }	t|	tsq|| |	j|j q|r| jd|  nd}q)|s|r|r|jr|js| jjrt| j| |jjdd D ]%}t|j vrq| j!t"|j# |j t d	  D ]
\}}|$|| qq|S )
zhCollects the values of the config attributes that are used by the plugin, accounting for parent classes.FTNr   model_configZConfigzYSpecifying config in two places is ambiguous, use either Config attribute or class kwargsr   r   )%r   r  keywordsr   get_config_updateupdatedefsbodyrk   r   r   lvaluesr   r   rvaluer   zipr   r   r   r#   r   r   failhas_alias_generatorr   rY   r   error_required_dynamic_aliasesr   rn   r   r   add_plugin_dependencyr4   ra   
setdefault)r^   r   r   Zhas_config_kwargsZhas_config_from_namespacer   exprZconfig_datastmtlhsarg_namer   Zkey_exprZ
value_exprZsubstmtr   r   rR   rR   rS   r     s   









z'PydanticModelTransformer.collect_configr  list[PydanticModelField] | Nonec              
   C  s>  | j }i }t|jjdd D ]c}t|jvrq| jt|j	 |jt d 
 D ]H\}}t||| j}t| jjj ||j W d   n1 sNw   Y  |||< |jj|}|rq|jrqt|jtsq| jd|j q)qt }	| |jD ]}
| |
|}|dur|
jd }|	|j |||j< q|t|  S )zACollects the fields for the model, accounting for parent classes.r   r   r   Nz7BaseModel field may only be overridden by another fieldr   )!r   reversedr   rn   r   r   r   r  r4   ra   r   r   r   r5   strict_optional_setrU   strict_optionalr   r   r   rl   rk   r(   r  set%_get_assignment_statements_from_blockr
  collect_field_from_stmtr  addr   listvalues)r^   r  r   Zfound_fieldsr   r   r   r   sym_nodeZcurrent_field_namesr  Zmaybe_fieldr  rR   rR   rS   r   /  s:   


z'PydanticModelTransformer.collect_fieldsr  r   Iterator[AssignmentStmt]c                 c  sT    |j D ]}|js| |E d H  q|jd ur&|jjs(| |jE d H  d S d S d S rW   )r  Zis_unreachabler  Z	else_body)r^   r  r  rR   rR   rS   ,_get_assignment_statements_from_if_statementc  s   
zEPydanticModelTransformer._get_assignment_statements_from_if_statementblockr   c                 c  s>    |j D ]}t|tr|V  qt|tr| |E d H  qd S rW   )r  rk   r   r   r$  )r^   r%  r  rR   rR   rS   r  j  s   


z>PydanticModelTransformer._get_assignment_statements_from_blockr   PydanticModelField | Nonec              
   C  s   | j }|jd }t|trt|jr|jdkrdS |jsDt|jt	r<t|jj
t	r<t|jj
j
tr<|jj
j
jtv r<dS t| j| dS |jd }t|tsPdS t|jr[|jdkr]dS |jj|j}|du rkdS |j}t|trudS t|tr| jd| dS t|tsdS |jrdS t|j}t|tr|jjdkr| jd| | |}|jdu r|jr|jr| jj|jdd}	|	r|	|_n| jd	| tt j!|_| "|\}
}|r|j#s| j$j%rt&| j| | '||j|}t(|j|||
|j)|j*||jd
S )a  Get pydantic model field from statement.

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

        Returns:
            A pydantic model field if it could find the field in statement. Otherwise, `None`.
        r   r  NzFType aliases inside BaseModel definitions are not supported at runtimezdataclasses.InitVarz%InitVar is not supported in BaseModelT)is_finalzGNeed type argument for Final[...] with non-literal default in BaseModel)r   r   r   r   r   r   r   r   )+r   r  rk   r   rE   Zis_valid_field_namer   Z
new_syntaxr  r   r   ra   r   error_untyped_fieldsr   r   r   r   rl   r    r&   r  r(   Zis_classvarrA   r   r9   get_has_defaultr'  Zis_inferredZanalyze_simple_literal_typer7   r=   Z
from_errorget_alias_infor   rY   r   r  _infer_dataclass_attr_init_typer   r   r   )r^   r  r  r   r  rq   rl   Z	node_typer   r   r   r   Z	init_typerR   rR   rS   r  q  s   
 







z0PydanticModelTransformer.collect_field_from_stmtrq   r$   r   rN   r   r   r   c           	      C  s   |j }|jr|S t|j }t|ts|S |j d}|rpt|jtrc|j d}|s,J |j r:tt	|j |j |}nt
tjS t|trT|jtttgkrTt|jd |S | jd|j j d| |S | jd|j j d| |S )zvInfer __init__ argument type for an attribute.

        In particular, possibly use the signature of __set__.
        __set__rL   z(Unsupported signature for "__set__" in ""zUnsupported "__set__" in ")r   ZimplicitrA   rk   r9   r   rl   r   Zget_containing_type_infor6   r7   r=   Zunannotatedr8   	arg_kindsr   r	   r   r   r  r   )	r^   rq   r   r   r   tsetterZ
super_infoZsetter_typerR   rR   rS   r+    s0   


z8PydanticModelTransformer._infer_dataclass_attr_init_typer   list[PydanticModelField]r   r   c              	   C  sn  d| j jjv r| j jjd jsdS | jj}|jdu}t|jo!|j }t	
| jjj_ | j|||||d}|r| jtj}d|jv r|jd j}	|	dur|	jdur|	j}
t|
jD ]'\}}|dsi|dsjq[| j|
j| }t||}|t||dt q[W d   n1 sw   Y  | ||std}|t|ttjdt  t!| j| j d|t" d dS )	zAdds a fields-aware `__init__` method to the class.

        The added `__init__` will be annotated with types vs. all `Any` depending on the plugin settings.
        r]   NTr   requires_dynamic_aliasesr   r   __r   kwargs)r   return_type)#r   r   r   plugin_generatedrY   r   r   r   r  r5   r  r   rU   r  get_field_argumentsrj   r   rl   r   r  r   
startswithZ	anal_typer   r(   appendr   r   should_init_forbid_extrar7   r=   r   r   
add_methodr:   )r^   r   r   r   r   r   r3  r   Zbase_settings_nodeZbase_settings_init_node	func_typeZarg_idxr  Zanalyzed_variable_typer   varrR   rR   rS   r     s>   


z(PydanticModelTransformer.add_initializerc           	      C  s   | j t d| j t dg}t|t g}ttd||dt}t	| j j
j | j|ddd|d}W d   n1 s@w   Y  | ||s\td}|t|ttjdt |g| }t| j | jd	|t| jjdd
 dS )zAdds a fully typed `model_construct` classmethod to the class.

        Similar to the fields-aware __init__ method, but always uses the field names (not aliases),
        and does not treat settings fields as optional.
        z.setz.strZ_fields_setNTFr2  r5  Zmodel_construct)r   r6  is_classmethod)r   
named_typeBUILTINS_NAMEr@   r:   r   r(   r   r5   r  rU   r  r8  r;  r:  r7   r=   r   r   r<  r   rB   r   )	r^   r   r   r   Zset_strZoptional_set_strZfields_set_argumentr   r>  rR   rR   rS   r   5  s(   $



z3PydanticModelTransformer.add_model_construct_methodr   c           	   	   C  s   | j j}|D ]n}|j|j}|durU|j}t|tr||_qt|t	r.| j
js.| j
  qzt|}W n tyA   t|}Y nw d| d|j d}t|| j
| j  q|j|dd}||_||_|jd |j |_tt||j|j< qdS )zMarks all fields as properties so that attempts to set them trigger mypy errors.

        This is the same approach used by the attrs and dataclasses plugins.
        Nzsym_node.node: r   r   F)r   .)r   r   r   r   r   rl   rk   r(   Zis_propertyr    r   Zfinal_iterationZdeferrN   	TypeErrorreprr`   r   r   ra   	_fullnamer$   r   )	r^   r   r   r   r   r"  r>  Zvar_strr   rR   rR   rS   r   S  s,   
z#PydanticModelTransformer.set_frozenr   r   ModelConfigData | Nonec                 C  s   || j vrdS |dkr/t|tr|jdk}nt|tr!|jdk}n	t|| j| dS t|dS |dkrFd}t|t	rA|j
dkrAd}t|d	S t|t	r\|j
d
v r\tdi ||j
dkiS t|| j| dS )zDetermines the config update due to a single kwarg in the ConfigDict definition.

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

        `has_dynamic_alias` is True if and only if an alias is provided, but not as a string literal.
        If `has_dynamic_alias` is True, `alias` will be None.
        )NFr   F)NT)r  rk   r%   r   r   r!   ra   ry   r  r   r   r#   r   )r  r  r   r  r   rR   rR   rS   r*    s"   



z'PydanticModelTransformer.get_alias_infor   r   r3  list[Argument]c                   s&   | j j  fdd|D }|S )zHelper function used during the construction of the `__init__` and `model_construct` method signatures.

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

        We disallow arbitrary kwargs if the extra config setting is "forbid", or if the plugin config says to,
        *unless* a required dynamic alias is present (since then we can't determine a valid signature).
        FT)r   is_dynamic_alias_presentr   r  rG  rY   r   )r^   r   r   rR   rR   rS   r;    s   z1PydanticModelTransformer.should_init_forbid_extrar  c                 C  s8   | D ]}|j r
 dS q|r| D ]
}|jdu r dS qdS )zReturns whether any fields on the model have a "dynamic alias", i.e., an alias that cannot be
        determined during static analysis.
        TNF)r   r   )r   r  r   rR   rR   rS   rP    s   
z1PydanticModelTransformer.is_dynamic_alias_presentN)
r   r   r   r   r   r0   rY   rX   rO   rV   )rO   r   )rO   rV   )rO   r  )r  r  rO   r  )r  r   rO   r#  )r%  r   rO   r#  )r  r   r  r  rO   r&  )rq   r$   r   rN   r   r   rO   r   )r   r1  r   r  r   r   rO   rV   )r   r1  r   r   rO   rV   )r   rN   r   r   rO   rF  )r  r   rO   r   )r  r   rO   rK  )r   r1  r   r   r   r   r3  r   r   r   rO   rL  )r   r1  r   r  rO   r   )r   r1  r  r   rO   r   )r   r   r   r   r   r   r]   r   r   r   r   r$  r  r  r+  r   r   r   r  staticmethodr)  r*  r8  r;  rP  rR   rR   rR   rS   r     s0   
 


"

L
4


x
&
&



r   c                   @  sD   e Zd ZdZ					ddd	d
ZdddZdddZdddZdS )r  z(Pydantic mypy plugin model config class.NrG  bool | Noner   r   r   r  c                 C  s"   || _ || _|| _|| _|| _d S rW   )rG  r   r   r   r  )r^   rG  r   r   r   r  rR   rR   rS   r]     s
   
zModelConfigData.__init__rO   r   c                 C  s   dd | j  D S )zReturns a dict of Pydantic model config names to their values.

        It includes the config if config value is not `None`.
        c                 S  s   i | ]\}}|d ur||qS rW   rR   )rf   kvrR   rR   rS   r     s    z3ModelConfigData.get_values_dict.<locals>.<dictcomp>)__dict__r   r   rR   rR   rS   r        zModelConfigData.get_values_dictr   rF  rV   c                 C  s2   |du rdS |   D ]
\}}t| || qdS )z$Update Pydantic model config values.N)r   r   r   )r^   r   rS  rT  rR   rR   rS   r	    s
   zModelConfigData.updater   rN   r   r   c                 C  s"   t | |du rt| || dS dS )zFSet default value for Pydantic model config if config value is `None`.N)r   r   )r^   r   r   rR   rR   rS   r    s   zModelConfigData.setdefault)NNNNN)
rG  rR  r   rR  r   rR  r   rR  r  rR  r   )r   rF  rO   rV   )r   rN   r   r   rO   rV   )r   r   r   r   r]   r   r	  r  rR   rR   rR   rS   r    s    

r  zpydantic-ormzInvalid from_attributes callZPydanticzpydantic-configzInvalid config valuezpydantic-aliaszDynamic alias disallowedzpydantic-unexpectedzUnexpected behaviorzpydantic-fieldzUntyped field disallowedzInvalid Field defaults
model_namer   r*   r   r   rV   c                 C     |j d|  d|td dS )zCEmits an error when the model does not have `from_attributes=True`.r-  z$" does not have from_attributes=TruecodeN)r  	ERROR_ORM)rW  r   r   rR   rR   rS   r        r   r   r0   c                 C  rX  )z0Emits an error when the config value is invalid.zInvalid value for "Config.r-  rY  N)r  ERROR_CONFIG)r   r   r   rR   rR   rS   rJ  "  r\  rJ  c                 C     | j d|td dS )znEmits required dynamic aliases error.

    This will be called when `warn_required_dynamic_aliases=True`.
    z#Required dynamic aliases disallowedrY  N)r  ERROR_ALIASr   r   rR   rR   rS   r  '  rV  r  r   8CheckerPluginInterface | SemanticAnalyzerPluginInterfacec                 C  s4   d}d|  d}|d| d7 }|j ||td dS )z Emits unexpected behavior error.z6https://github.com/pydantic/pydantic/issues/new/choosez7The pydantic mypy plugin ran into unexpected behavior: 
z&Please consider reporting this bug at z so we can try to fix it!rY  N)r  ERROR_UNEXPECTED)r   r   r   linkZfull_messagerR   rR   rS   r   /  s   r   c                 C  r^  )z;Emits an error when there is an untyped field in the model.zUntyped fields disallowedrY  N)r  ERROR_UNTYPEDr`  rR   rR   rS   r(  :  r   r(  c                 C  r^  )zNEmits an error when `Field` has both `default` and `default_factory` together.z>Field default and default_factory cannot be specified togetherrY  N)r  ERROR_FIELD_DEFAULTSr`  rR   rR   rS   r   ?  r   r   F8SemanticAnalyzerPluginInterface | CheckerPluginInterfacer   r   r   rL  r6  r   r   tvar_defTypeVarDef | Noner?  r   c                 C  s  |j }||jv r|j| }	|	jrt|	jtr|jj|	j t| t	r)| 
d}
n| dg }
|rE|p8tt|}ttd|dtdg}n|pJt|}ttd|dtg}|| }g g g }}}|D ]}|jslJ d||j ||jj ||j qct|||||
}|r|g|_t||tt g}||_ t|||_||_|jd | |_|j|_||jv rt ||j}|j| |j|< |rd|_!t||j}||_ |j|_d|_"t#|t$dg|}|j|_t%t&|}	nt%t&|}	d|	_|	|j|< |j'jj| dS )	zhVery closely related to `mypy.plugins.common.add_method_to_class`, with a few pydantic-specific changes.zbuiltins.functionr   NTZ__pydantic_self__z"All arguments must be fully typed.rB  r   )(r   r   r7  rk   rl   r   r
  r  remover0   r@  Znamed_generic_typer>   rB   r   r(   r   r   r:  r   r   r   r8   	variablesr   r   r3   r   r  ra   rE  r   rC   Zis_decoratedr?  r   r   r$   r   Zdefn)r   r   r   r   r6  r   rh  r?  r   rq   Zfunction_typefirstr   r   r.  r   	signaturer  Zr_namerT  decrR   rR   rS   r<  D  sZ   





r<  r   dict[str, Any] | Nonec                 C  s   |  dsdS tjdkrddl}nzddl}W n ty*   ddl}|d Y dS w t| d}|	|W  d   S 1 s@w   Y  dS )zReturns a dict of config keys to values.

    It reads configs from toml file and returns `None` if the file is not a toml file.
    z.tomlN)      r   zJNo TOML parser installed, cannot read configuration from `pyproject.toml`.rb)
r}   sysversion_infoZtomllibtomliImportErrorwarningswarnopenload)r   Ztoml_rw  rfrR   rR   rS   r     s   



$r   )rM   rN   rO   rP   )r   r-   rO   r<   )rW  rN   r   r*   r   r   rO   rV   )r   rN   r   r0   r   r   rO   rV   )r   r0   r   r   rO   rV   )r   rN   r   ra  r   r   rO   rV   )r   r*   r   r   rO   rV   )NNF)r   rg  r   r   r   rN   r   rL  r6  r<   r   r   rh  ri  r?  r   rO   rV   )r   rN   rO   ro  )~r   
__future__r   rs  configparserr   typingr   r   r   Zmypy.errorcodesr   Zmypy.expandtyper   r	   Z
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(   Zmypy.optionsr)   Zmypy.pluginr*   r+   r,   r-   r.   r/   r0   Zmypy.pluginsr1   Zmypy.plugins.commonr2   Zmypy.semanalr3   Zmypy.server.triggerr4   Z
mypy.stater5   Zmypy.typeopsr6   Z
mypy.typesr7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   Zmypy.typevarsrB   Z	mypy.utilrC   Zmypy.versionrD   Zmypy_versionZpydantic._internalrE   Zpydantic.versionrF   rG   rv  r   r   rd   r   rt   ry   r   r  r   r   rA  rT   rQ   rX   r~   r   r   r  r[  r]  r_  rc  re  rf  r   rJ  r  r   r(  r   r<  r   rR   rR   rR   rS   <module>   s    !$	4

v
.O    j%





N