a c[ @sddlZddlmZddlmZmZmZmZmZm Z m Z m Z m Z ddlmZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm 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-ddl.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7ddl8m9Z9dd l:m;Z;dd lm?Z?m@Z@mAZAmBZBmCZCm Z mDZDmEZEmFZFmGZGmHZHdd lImJZJdd lKmLZLddlMmNZOddlPmQZQzddl>mRZRWn eSyddl>mFZRYn0dZTdZUdZVdZWdZXdZYeZe e[dfdddZ\e\eOZ]e]dkrdndZ^eZddd d!Z_Gd"d#d#e5Z`Gd$d%d%Zae4e d&d'd(ZbGd)d*d*ZcGd+d,d,ZdGd-d.d.Zeed/d0d1Zfed2d3d1Zged4d5d1Zhed6d7d1Zied8d9d1Zjed8d:d1ZkeZe1edd;dd?d@Zme7eddAdBdCZneZe1eddDdEdFZoe7eddAdGdHZpe1eddAdIdJZqdWe2eZeee ee eeRererddL dMdNZse ee(feZdOdPdQZte ee(feZdOdRdSZueZeeeZefdTdUdVZvdS)XN) ConfigParser) AnyCallableDictListOptionalSetTupleTypeUnion) ErrorCode) ARG_NAMED ARG_NAMED_OPTARG_OPTARG_POS ARG_STAR2MDEFArgumentAssignmentStmtBlockCallExprClassDefContext Decorator EllipsisExprFuncBaseFuncDefJsonDict MemberExprNameExprPassStmtPlaceholderNodeRefExprStrExpr SymbolNodeSymbolTableNodeTempNodeTypeInfo TypeVarExprVar)Options)CheckerPluginInterfaceClassDefContextFunctionContext MethodContextPluginReportConfigContextSemanticAnalyzerPluginInterface) dataclasses)set_callable_name)make_wildcard_trigger) AnyType CallableTypeInstanceNoneType Overloadedr TypeOfAnyTypeType TypeVarType UnionTypeget_proper_type) fill_typevars)get_unique_redefinition_name) __version__)is_valid_field) TypeVarDef)r< pydantic-mypyzpydantic-mypy-metadatazpydantic.main.BaseModelz"pydantic.env_settings.BaseSettingszpydantic.fields.Fieldzpydantic.dataclasses.dataclass.)versionreturncCs$tdd|ddddDS)Ncss|]}t|VqdSN)int).0partrKZ/workspaces/shunt/resources/test-fastapi/venv/lib/python3.9/site-packages/pydantic/mypy.py Tz%parse_mypy_version..+r.)tuplesplitrErKrKrLparse_mypy_versionSsrU)ribuiltins __builtins__zTypingType[Plugin]cCstS)z `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). )PydanticPluginrTrKrKrLplugin[srYcseZdZeddfdd ZeddddZed dd d Zeee e ge fdd d Z eee e gdfdddZeeeefdddZe ddddZeddddZZS)rXNoptionsrFcs&t||_|j|_t|dSrG)PydanticPluginConfig plugin_configto_data _plugin_datasuper__init__)selfr[ __class__rKrLrafs  zPydanticPlugin.__init__z+Optional[Callable[[ClassDefContext], None]])fullnamerFcCs:||}|r6t|jtr6tdd|jjDr6|jSdS)Ncss|]}t|tkVqdSrG) get_fullnameBASEMODEL_FULLNAMErIbaserKrKrLrMorNz5PydanticPlugin.get_base_class_hook..)lookup_fully_qualified isinstancenoder'anymro$_pydantic_model_class_maker_callbackrbresymrKrKrLget_base_class_hookks  z"PydanticPlugin.get_base_class_hookz+Optional[Callable[[FunctionContext], Type]]cCs"||}|r|jtkr|jSdSrG)rjreFIELD_FULLNAME_pydantic_field_callbackrprKrKrLget_function_hookss z PydanticPlugin.get_function_hookcCs|drtSdS)Nz .from_orm)endswithfrom_orm_callbackrbrerKrKrLget_method_hookys zPydanticPlugin.get_method_hookcCs|tkrtjSdSrG)DATACLASS_FULLNAMEr2Zdataclass_class_maker_callbackrxrKrKrLget_class_decorator_hook~sz'PydanticPlugin.get_class_decorator_hookctxrFcCs|jS)zjReturn all plugin config data. Used by mypy to determine if cache needs to be discarded. )r_)rbr}rKrKrLreport_config_datasz!PydanticPlugin.report_config_datacCst||j}|dSrG)PydanticModelTransformerr] transform)rbr}Z transformerrKrKrLros z3PydanticPlugin._pydantic_model_class_maker_callbackr c s |j|jddksJd|jddks2Jd|jd}|jd}|r`|r`t|j|jS|r|jdd}|d}t|ts|Sn|r|jdd}t|t rt dkr|j d}n | d}t|t r|j }t|dd }|rtd d |Drtfd d |D|_|SS) ax 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. rdefaultz0"default" is no longer first argument in Field()rPdefault_factoryz9"default_factory" is no longer second argument in Field())riargsNcss|]}t|tVqdSrG)rkr<)rIargrKrKrLrMrNz:PydanticPlugin._pydantic_field_callback..c3s|] }VqdSrGrK)rI_Zdefault_any_typerKrLrMrN)default_return_typeZcallee_arg_namesr+error_default_and_default_factory_specifiedapicontext arg_typesrkrr9MYPY_VERSION_TUPLEitemsr6ret_typegetattrallrR) rbr}Z default_argsZdefault_factory_argsZ default_typeZ default_argZdefault_factory_typerrrKrrLrts6         z'PydanticPlugin._pydantic_field_callback)__name__ __module__ __qualname__r*rastrrrrurrr.r ryr,r{r0rrr~ror-rt __classcell__rKrKrcrLrXesrXc@sXeZdZUdZeed<eed<eed<eed<edddd Zee e fd d d Z dS) r\)init_forbid_extra init_typedwarn_required_dynamic_aliaseswarn_untyped_fieldsrrrrNrZcCs|jdurdSt|j}|durr|didi}|jD]4}||d}t|tsbtd|t|||q:n:t}| |j|jD] }|j t |dd}t|||qdS)NZtoolrDFz/Configuration value must be a boolean for key: )fallback) config_file parse_tomlget __slots__rkbool ValueErrorsetattrrread getbooleanCONFIGFILE_KEY)rbr[Z toml_configconfigkeyZsettingr]rKrKrLras       zPydanticPluginConfig.__init__rFcsfddjDS)Ncsi|]}|t|qSrK)r)rIrrbrKrL rNz0PydanticPluginConfig.to_data..)rrrKrrLr^szPydanticPluginConfig.to_data) rrrrr__annotations__r*rarrrr^rKrKrKrLr\s r\r|cCst|jtr$t|jjtr$|jj}nDt|jtr8|j}n0d|jd|jjjd}t||j|j |j S|jj t }|dur|j S| di d}|durtt|j|j|j |j S)z3 Raise an error if orm_mode is not enabled z ctx.type: z (of type )Nrorm_modeT)rktyper6rr7rdrerror_unexpected_behaviorrrrmetadatar METADATA_KEYerror_from_ormget_name)r}Z model_typedetailZpydantic_metadatarrKrKrLrws  rwc@sNeZdZUhdZeeed<eeddddZ dddd Z ddd d Z d dd dZ d e ddddZe dd eddddZe dddddZe deddddZeed dddZeeeeedd d!Zeeeeeefd"d#d$Ze deeee ed%d&d'Ze dd ed(d)d*Zee deed+d,d-ZdS).r>allow_mutationextrarfrozenalias_generatorallow_population_by_field_nametracked_config_fieldsN)r}r]rFcCs||_||_dSrG)_ctxr])rbr}r]rKrKrLrasz!PydanticModelTransformer.__init__rcCs|j}|jjj}||}||}|D]&}||jjdur.|jj s.|j q.t dd|j ddD}| ||||||j||jdup|jduddd |D|d |jt<dS) 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 allow_mutation = False or frozen = True * stores the fields, config, and if the class is settings in the mypy metadata for access by subclasses Ncss|]}t|tkVqdSrG)rfBASESETTINGS_FULLNAMErhrKrKrLrMrNz5PydanticModelTransformer.transform..FT)rcSsi|]}|j|qSrK)name serializerIfieldrKrKrLrrNz6PydanticModelTransformer.transform..)fieldsr)rclsinfoadjust_validator_signaturescollect_configcollect_fieldsrrrZfinal_iterationdeferrmrnadd_initializeradd_construct_method set_frozenrrset_values_dictrr)rbr}rrrr is_settingsrKrKrLrs      z"PydanticModelTransformer.transformcCsb|jjjjD]L\}}t|jtr|jjd}t|t rt|j t r|j j dkrd|jj _qdS)a[When we decorate a function `f` with `pydantic.validator(...), 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()` call as a classmethod. rz#pydantic.class_validators.validatorTN)rrrnamesrrkrlrZoriginal_decoratorsrcalleerrefuncis_class)rbrrqZ first_decrKrKrLr#s    z4PydanticModelTransformer.adjust_validator_signaturesModelConfigDatac Cs|j}|j}t}|jjD]d}t|ts*q|jdkr|jjD] }t|tsLq<| | |q<|j r|j s|j jrt|j|q|jjddD]L}t|jvrq|jtt||jtdD]\}}|||qq|S)zz Collects the values of the config attributes that are used by the plugin, accounting for parent classes. ZConfigrPNr)rrrdefsbodyrkrrrupdateget_config_updatehas_alias_generatorrr]rerror_required_dynamic_aliasesrrrnrradd_plugin_dependencyr4rfr setdefault) rbr}rrstmtsubstmtrrvaluerKrKrLr5s2      z'PydanticModelTransformer.collect_configPydanticModelField) model_configrFc s|j}|jj}g}t}|jjD]}t|ts0q |jd}t|tr t |j sPq |j sj|j j rjt|j||jj|j }|durq |j} t| trq t| tsq | jrq ||||} ||\} } | r|js|j jrt|j||t|j | | | |j|jd| |j q |!} |jj"ddD]}t#|j$vr>q*g}|j%t&t'||j$t#d(D]^\}|vrt)||}| ||n(fdd| D\}| *|||qh|| } q*| S)zR Collects the fields for the model, accounting for parent classes rNr is_requiredaliashas_dynamic_aliaslinecolumnrPrc3s|]}|jkr|VqdSrGr)rIarrKrLrMrNz:PydanticModelTransformer.collect_fields..)+rrsetrrrkrlvaluesrrBrZ new_syntaxr]rerror_untyped_fieldsrrrrrlr!r)Z is_classvarget_is_requiredget_alias_inforrrappendrrraddcopyrnrrrr4rfr deserializeremove)rbrr}rrZ known_fieldsrlhsrqrlrrrZ all_fieldsrZsuperclass_fieldsdatarrKrrLrTst               z'PydanticModelTransformer.collect_fields)rrrrFc Cs|j}|jj}|jdu}|p,t|jo*|j }|j||||d}|||sntd} | t | t t j dtd|jjjvrt|d|tdS)z Adds a fields-aware `__init__` method to the class. The added `__init__` will be annotated with types vs. all `Any` depending on the plugin settings. Ttypedforce_all_optional use_aliaskwargsNra)rr]rrrrget_field_argumentsshould_init_forbid_extrar)rrr5r:explicitrrrr add_methodr8) rbrrrr}rrrZinit_argumentsvarrKrKrLrs   z(PydanticModelTransformer.add_initializer)rrFc Cs|j}|jtd|jtdg}t|tg}ttd||dt}|j |dddd}|g|}|jtd}d }|j j d |} t || d g|} t || g|} tt| |j jj|<t| tr| } nt| } t|d || | | dd dS)z Adds a fully typed `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_setNTFrz.objectZ_PydanticBaseModelrQrZ construct) return_type self_typetvar_defis_classmethod)rr named_type BUILTINS_NAMEr=r8rr)rrrrerCr(r%rrrrkr<r) rbrr}Zset_strZoptional_set_strZfields_set_argumentZconstruct_argumentsZobj_typeZself_tvar_nameZ tvar_fullnameZtvdZself_tvar_exprrrKrKrLrs0$  z-PydanticModelTransformer.add_construct_method)rrrFcCs|jjj}|D]z}|j|j}|durD|j}t|ts.FTNrr)rrkr&r>rrrr=rmrrrr"rerszipr arg_namesrdr)rrrexprZ value_typerrrKrKrLrs "z(PydanticModelTransformer.get_is_required)rrFcCs|j}t|trdSt|tr6t|jtr6|jjtks:dSt|j D]:\}}|dkrVqD|j |}t|t rx|j dfSdSqDdS)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. )NFrF)NT) rrkr&rrr"rers enumerater rr#r)rr iZarg_namerrKrKrLr.s$     z'PydanticModelTransformer.get_alias_info)rrrrrFcs&|jjjfdd|D}|S)z Helper function used during the construction of the `__init__` and `construct` method signatures. Returns a list of mypy Argument instances for use in the generated signatures. cs(g|] }r|js|jdqS))rforce_optionalr)r to_argumentrrrrrrKrL Ts z@PydanticModelTransformer.get_field_arguments..)rrr)rbrrrr argumentsrKrrLrKs  z,PydanticModelTransformer.get_field_arguments)rrrFcCs.|js||t|jrdS|jr&dS|jjS)aH 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)ris_dynamic_alias_presentrrrr]r)rbrrrKrKrLr[s z1PydanticModelTransformer.should_init_forbid_extra)rrrFcCs8|D]}|jrdSq|r4|D]}|jdurdSqdS)z Returns whether any fields on the model have a "dynamic alias", i.e., an alias that cannot be determined during static analysis. TNF)rr)rrrrKrKrLris z1PydanticModelTransformer.is_dynamic_alias_present) rrrrrrrr,r\rarrrrrrrrrrrr staticmethodrrrr rrrrrrKrKrKrLrs(  T%  rc@steZdZeeeeeeedddZeee dddZ eeeee ddd Z e d d d Zeee dd ddZdS)rrcCs(||_||_||_||_||_||_dSrGr)rbrrrrrrrKrKrLrazs zPydanticModelField.__init__)rrrFcCs,|j}|r|jdur|j}t|||jjSrG)rrr)r)rbrrrrKrKrLrszPydanticModelField.to_var)rrrrrFcCsR|r"||jjdur"||jj}n ttj}t||||d|sF|jsJtnt dS)N)variabletype_annotationZ initializerkind) rrr5r:rrrrrr )rbrrrrrrKrKrLrs  zPydanticModelField.to_argumentrcCs|jSrG)__dict__rrKrKrLrszPydanticModelField.serialize)rrrFcCs|fi|SrGrK)rrrrKrKrLrszPydanticModelField.deserializeN)rrrrrrrHrar'r)rrrrr classmethodrrKrKrKrLrys  rc@szeZdZdeeeeeeeeeeeedddZeeefdddZ edddd d Z eedd d d Z dS)rNrrrrrrcCs(||_||_||_||_||_||_dSrGr)rbrrrrrrrKrKrLras zModelConfigData.__init__rcCsdd|jDS)NcSsi|]\}}|dur||qSrGrK)rIkvrKrKrLrrNz3ModelConfigData.set_values_dict..)rrrrKrKrLrszModelConfigData.set_values_dict)rrFcCs2|dur dS|D]\}}t|||qdSrG)rrr)rbrrrrKrKrLrszModelConfigData.update)rrrFcCst||durt|||dSrG)rr)rbrrrKrKrLrszModelConfigData.setdefault)NNNNNN) rrrrrrarrrrrrrKrKrKrLrs" rz pydantic-ormzInvalid from_orm callZPydanticzpydantic-configzInvalid config valuezpydantic-aliaszDynamic alias disallowedzpydantic-unexpectedzUnexpected behaviorzpydantic-fieldzUntyped field disallowedzInvalid Field defaults) model_namerrrFcCs|jd|d|tddS)N"z" does not have orm_mode=Truecode)fail ERROR_ORM)rrrrKrKrLrsr)rrrrFcCs|jd|d|tddS)NzInvalid value for "Config.rr )r" ERROR_CONFIG)rrrrKrKrLrsr)rrrFcCs|jd|tddS)Nz#Required dynamic aliases disallowedr )r" ERROR_ALIASrrrKrKrLrsr)rrrrFcCs4d}d|d}|d|d7}|j||tddS)Nz6https://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 )r"ERROR_UNEXPECTED)rrrlinkZ full_messagerKrKrLrs rcCs|jd|tddS)NzUntyped fields disallowedr )r" ERROR_UNTYPEDr&rKrKrLrsrcCs|jd|tddS)Nz>Field default and default_factory cannot be specified togetherr )r"ERROR_FIELD_DEFAULTSr&rKrKrLrsrF) r}rrrrrris_newrFcCs|jj}||jvr@|j|} | jr@t| jtr@|jjj | j|pJt |}|sT|rpt t dt |dtg} n |pzt |}t t d|dtg} | |}ggg} } } |D]:}|jsJd| |j| t|j| |jq|jtd}t| | | ||}|r|g|_t||ttg}||_t|||_||_t|d||_ |j!|_!||jvrt"||j}|j||j|<|rd|_#t ||j}||_|j |_ d|_$t%|t&dg|}|j!|_!t't(|} n t't(|} d| _| |j|<|j)jj|dS) z} Adds a new method to a class. This can be dropped if/when https://github.com/python/mypy/issues/7301 is merged _clsNZ__pydantic_self__z"All arguments must be fully typed.z .functionrQTr)*rrrZplugin_generatedrkrlrrrrr?rr)r;Zmake_normalizedrrrrrrrrrr6 variablesrr r3rrrfrrr@Z is_decoratedrrrr%rZdefn)r}rrrrrrr,rrqfirstrr Z arg_kindsrZ function_type signaturerZr_namerdecrKrKrLrsV            r)xrFcCs|j}t|r|S|Szc Used for compatibility with mypy 0.740; can be dropped once support for 0.740 is dropped. )recallabler2fnrKrKrLrf4srfcCs|j}t|r|S|Sr3)rr4r5rKrKrLr>sr)rrFc Cs|dsdSd}tjdkr&ddl}nZz ddl}WnLty~d}z ddl}Wn(tyxddl}|dYYdS0Yn0t ||}| |WdS1s0YdS)Nz.tomlrb) rrzJNo TOML parser installed, cannot read configuration from `pyproject.toml`.) rvsys version_infoZtomllibZtomli ImportErrortomlwarningswarnopenload)rZ read_modeZtoml_r?rfrKrKrLrHs"         r)NNFF)wr; configparserrtypingrrrrrrr r Z TypingTyper Zmypy.errorcodesr Z mypy.nodesr rrrrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)Z mypy.optionsr*Z mypy.pluginr+r,r-r.r/r0r1Z mypy.pluginsr2Z mypy.semanalr3Zmypy.server.triggerr4Z mypy.typesr5r6r7r8r9r:r;r<r=r>Z mypy.typevarsr?Z mypy.utilr@Z mypy.versionrAZ mypy_versionZpydantic.utilsrBrCr=rrrgrrsrzrrHrUrrrYrXr\rwrrrr#r$r%r(r*r+rrrrrrrrrfrrrKrKrKrLs , | $   4     _%        S