U nfv @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@mAZAmBZBmCZCmDZDm Z mEZEmFZFmGZGmHZHmIZImJZJdd lKmLZLdd lMmNZNddlOmPZQddlRmSZSzddl>mTZTWn"eUk rddl>mHZTYnXdZVdZWeXddZYeYdZZeYdZ[eYdZ\eYdZ]eYdZ^e_e e`dfdddZaeaeQZbebdkr0dnd Zcd!ZPe_d"dd#d$ZdGd%d&d&e5ZeGd'd(d(Zfe4e d)d*d+ZgGd,d-d-ZhGd.d/d/ZiGd0d1d1Zjed2d3d4Zked5d6d4Zled7d8d4Zmed9d:d4Zned;dd?d@Zqe_e7eddAdBdCZre7eddDdEdFZse_e e1e7feddGdHdIZte7eddDdJdKZue1eddDdLdMZvdZe2e_eee ee eeTewewddO dPdQZxe ee(fe_dRdSdTZye ee(fe_dRdUdVZze_eee_efdWdXdYZ{dS)[N) 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 Overloaded ProperTyper TypeOfAnyTypeType TypeVarId TypeVarType UnionTypeget_proper_type) fill_typevars)get_unique_redefinition_name) __version__)is_valid_field) TypeVarDef)r> pydantic-mypyzpydantic-mypy-metadataz.main.BaseModelz.env_settings.BaseSettingsz.main.ModelMetaclassz .fields.Fieldz.dataclasses.dataclass.)versionreturncCsttt|dddS)N+r.)tuplemapint partitionsplitrHrR>/opt/hc_python/lib/python3.8/site-packages/pydantic/v1/mypy.pyparse_mypy_versionWsrT)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). )PydanticPluginrQrRrRrSpluginbsrYcseZdZeddfdd ZeddddZeeee gdfdd d Z ed dd d Z eeee ge fdddZeeee gdfdddZeeeefdddZe ddddZe ddddZeddddZZS)rXNoptionsrIcs&t||_|j|_t|dSN)PydanticPluginConfig plugin_configto_data _plugin_datasuper__init__)selfr[ __class__rRrSrbms  zPydanticPlugin.__init__z+Optional[Callable[[ClassDefContext], None]])fullnamerIcCs:||}|r6t|jtr6tdd|jjDr6|jSdS)Ncss|]}t|tkVqdSr\) get_fullnameBASEMODEL_FULLNAME.0baserRrRrS vsz5PydanticPlugin.get_base_class_hook..)lookup_fully_qualified isinstancenoder'anymro$_pydantic_model_class_maker_callbackrcrfsymrRrRrSget_base_class_hookrs  z"PydanticPlugin.get_base_class_hookcCs|tkr|jSdSr\)MODEL_METACLASS_FULLNAME)_pydantic_model_metaclass_marker_callbackrcrfrRrRrSget_metaclass_hookzsz!PydanticPlugin.get_metaclass_hookz+Optional[Callable[[FunctionContext], Type]]cCs"||}|r|jtkr|jSdSr\)rmrfFIELD_FULLNAME_pydantic_field_callbackrsrRrRrSget_function_hooks z PydanticPlugin.get_function_hookcCs|drtSdS)Nz .from_orm)endswithfrom_orm_callbackrxrRrRrSget_method_hooks zPydanticPlugin.get_method_hookcCs|tkrtdkrtjSdS)zMark pydantic.dataclasses as dataclass. Mypy version 1.1.1 added support for `@dataclass_transform` decorator. )rN)DATACLASS_FULLNAMEMYPY_VERSION_TUPLEr2Zdataclass_class_maker_callbackrxrRrRrSget_class_decorator_hooksz'PydanticPlugin.get_class_decorator_hookctxrIcCs|jS)zjReturn all plugin config data. Used by mypy to determine if cache needs to be discarded. )r`)rcrrRrRrSreport_config_datasz!PydanticPlugin.report_config_datacCst||j}|dSr\)PydanticModelTransformerr^ transform)rcrZ transformerrRrRrSrrs z3PydanticPlugin._pydantic_model_class_maker_callbackcCs<|jjr dS|jjj}|s"tdt|jddr8d|j_dS)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_transformclsinfoZdeclared_metaclassAssertionErrorgetattrtyper)rcrZinfo_metaclassrRrRrSrws   z8PydanticPlugin._pydantic_model_metaclass_marker_callbackr c s |j|jddkstd|jddks2td|jd}|jd}|r`|r`t|j|jS|r|jdd}|d}t|t s|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()rdefault_factoryz9"default_factory" is no longer second argument in Field())riargsNcss|]}t|tVqdSr\)rnr>)rjargrRrRrSrlsz:PydanticPlugin._pydantic_field_callback..c3s|] }VqdSr\rR)rj_Zdefault_any_typerRrSrls)default_return_typeZcallee_arg_namesrr+error_default_and_default_factory_specifiedapicontext arg_typesrnrr9ritemsr6ret_typerallrL) rcrZ default_argsZdefault_factory_argsZ default_typeZ default_argZdefault_factory_typerrrRrrSr{s6         z'PydanticPlugin._pydantic_field_callback)__name__ __module__ __qualname__r*rbstrrurrr,ryr|r.r rrr0rrrrrrwr-r{ __classcell__rRrRrdrSrXls  rXc@s`eZdZUdZeed<eed<eed<eed<eed<eddd d Zee e fd d d Z dS)r])init_forbid_extra init_typedwarn_required_dynamic_aliaseswarn_untyped_fieldsrrrrrrNrZcCs|jdkrdSt|j}|dk rr|didi}|jD]4}||d}t|tsbtd|t|||q:n:t}| |j|jD] }|j t |dd}t|||qdS)NZtoolrFFz/Configuration value must be a boolean for key: )fallback) config_file parse_tomlget __slots__rnbool ValueErrorsetattrrread getbooleanCONFIGFILE_KEY)rcr[Z toml_configconfigkeyZsettingr^rRrRrSrbs       zPydanticPluginConfig.__init__rIcsfddjDS)Ncsi|]}|t|qSrR)r)rjrrcrRrS sz0PydanticPluginConfig.to_data..)rrrRrrSr_szPydanticPluginConfig.to_data) rrrrr__annotations__r*rbrrrr_rRrRrRrSr]s r]rcCs|j}t|tr|j}t|tr4t|jtr4|j}n.NFT)rcSsi|]}|j|qSrR)name serializerjfieldrRrRrSrBsz6PydanticModelTransformer.transform..)fieldsr)rrradjust_validator_signaturescollect_configcollect_fieldsrprqadd_initializeradd_construct_method set_frozenrrset_values_dictrr)rcrrrr is_settingsrRrRrSr-s    z"PydanticModelTransformer.transformcCsh|jjjjD]R\}}t|jtr|jjd}t|t rt|j t r|j j t 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.class_validators.validatorTN)rrrnamesrrnrorZoriginal_decoratorsrcalleerrf _NAMESPACEfuncis_class)rcrrtZ first_decrRrRrSrFs   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|jkrq|jtt||jtdD]\}}|||qq|S)zz Collects the values of the config attributes that are used by the plugin, accounting for parent classes. ZConfigrNr)rrrdefsbodyrnrrrupdateget_config_updatehas_alias_generatorrr^rerror_required_dynamic_aliasesrrrqrradd_plugin_dependencyr4rgr setdefault) rcrrrstmtsubstmtrrvaluerRrRrSrXs2      z'PydanticModelTransformer.collect_configPydanticModelField) model_configrIc s|j}|jj}g}t}|jjD]}t|ts0q |jd}t|tr t |j sPq |j sj|j j rjt|j||jj|j }|dkrq |j} t| trq t| tsq | jrq ||||} ||\} } | r|js|j jrt|j||t|j | | | |j|jd| |j q |!} |jj"ddD]}t#|j$kr>q*g}|j%t&t'||j$t#d(D]^\}|krt)||}| ||n(fdd| D\}| *|||qh|| } q*| S)zR Collects the fields for the model, accounting for parent classes rNr is_requiredaliashas_dynamic_aliaslinecolumnrrc3s|]}|jkr|VqdSr\r)rjarrRrSrls z:PydanticModelTransformer.collect_fields..)+rrsetrrrnrlvaluesrrDrZ new_syntaxr^rerror_untyped_fieldsrrrrror!r)Z is_classvarget_is_requiredget_alias_inforrrappendrrraddcopyrqrrrr4rgr deserializeremove)rcrrrrZ known_fieldsrlhsrtrorrrZ all_fieldsrZsuperclass_fieldsdatarrRrrSrwst               z'PydanticModelTransformer.collect_fields)rrrrIc Cs|j}|jj}|jdk }|p,t|jo*|j }|j||||d}|||sntd} | t | t t j dtd|jjjkrt|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_aliaskwargsNrb)rr^rrrrget_field_argumentsshould_init_forbid_extrar)rrr5r;explicitrrrr add_methodr8) rcrrrrrrrZinit_argumentsvarrRrRrSrs"   z(PydanticModelTransformer.add_initializer)rrIc Cs0|j}|jtd|jtdg}t|tg}ttd||dt}|j |dddd}|g|}|jtd}d }|j j d |} t d krt || td g|ttj} t|| g|ttj} nt|| d g|} t|| g|} tt| |j jj|<t| t r| } nt | } t|d || | | dddS)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_PydanticBaseModelrK)rrZ construct) return_type self_typetvar_defis_classmethod)rr named_type BUILTINS_NAMEr?r8rr)rrrrfrr>r=r5r;Zfrom_omitted_genericsr(rEr%rrrrnr) rcrrZset_strZoptional_set_strZfields_set_argumentZconstruct_argumentsobj_typeZself_tvar_nameZ tvar_fullnameZtvdZself_tvar_exprr rRrRrSrsP$  z-PydanticModelTransformer.add_construct_method)rrrIc Cs|j}|jj}|D]}|j|j}|dk r|j}t|trD||_ qt|t rb|j j sb|j qz t|}Wntk rt|}YnXd|d|jd} t| |j |jq|j|dd}||_||_ t|dt||_tt||jt|<qdS)z Marks 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. Nzsym_node.node: rrF)rrK)rrrrrrrornr)Z is_propertyr!rZfinal_iterationZdeferr TypeErrorreprrerto_varrgr _fullnamer%r) rcrrrrrZsym_noderZvar_strrrRrRrSrs*   z#PydanticModelTransformer.set_frozen)rrIcCs|jd}t|tr |j|jks$dS|jdkrt|jtrH|jjdk}n0t|jtrb|jjdk}nt |j|j j |dSt |dS|jdkrd}t|jtr|jj dkrd }t |d St|jtr|jj d krt f|j|jj d kiSt |j|j j |dS) z Determines the config update due to a single statement in the Config class definition. Warns if a tracked config attribute is set to a value the plugin doesn't know how to interpret (e.g., an int) rNrZforbid) forbid_extrarTz builtins.NoneF)r) builtins.Truezbuiltins.Falser)rrnrrrrvaluer#rrerror_invalid_config_valuerrrrf)rcrrrrrRrRrSr8s(       z*PydanticModelTransformer.get_config_update)rrrrIcCs|j}t|tr.t|j|jj}t| St|t rt|j t r|j j t krt|j|jD]4\}}|dksv|dkr|jtkS|dkr^dSq^t|j|jj}t| St|tS)zg Returns a boolean indicating whether the field defined in `stmt` is a required field. NrrF)rrnr&r@rrrrtype_has_implicit_defaultrrr"rfrzzipr arg_namesrer)rrrexprZ value_typerrrRrRrSrTs  " z(PydanticModelTransformer.get_is_required)type_rIcCs4t|trdSt|tr0tdd|jDr0dSdS)z Returns True if the passed type will be given an implicit default value. In pydantic v1, this is the case for Optional types and Any (with default value None). Tcss"|]}t|tpt|tVqdSr\)rnr8r5)rjrrRrRrSrlxszEPydanticModelTransformer.type_has_implicit_default..F)rnr5r?rpr)rrRrRrSrns z2PydanticModelTransformer.type_has_implicit_default)rrIcCs|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) rrnr&rrr"rfrz enumeraterrr#r)rriZarg_namerrRrRrSrs$     z'PydanticModelTransformer.get_alias_info)rrrrrIcs&|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_argumentrrrrrrRrS sz@PydanticModelTransformer.get_field_arguments..)rrr)rcrrrr argumentsrRr"rSrs  z,PydanticModelTransformer.get_field_arguments)rrrIcCs.|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)rcrrrRrRrSrs z1PydanticModelTransformer.should_init_forbid_extra)rrrIcCs8|D]}|jrdSq|r4|D]}|jdkrdSqdS)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)rrrrRrRrSr%s z1PydanticModelTransformer.is_dynamic_alias_present)"rrrrrrrr,r]rbrrrrrrrrrrrr staticmethodrrrr:rr rrrrr%rRrRrRrSrs>  T6! rc@steZdZeeeeeeedddZeee dddZ eeeee ddd Z e d d d Zeee dd ddZdS)rrcCs(||_||_||_||_||_||_dSr\r)rcrrrrrrrRrRrSrbs zPydanticModelField.__init__)rrrIcCs,|j}|r|jdk r|j}t|||jjSr\)rrr)r)rcrrrrRrRrSrszPydanticModelField.to_var)rrr rrIcCsR|r"||jjdk r"||jj}n ttj}t||||d|sF|jsJtnt dS)N)variabletype_annotationZ initializerkind) rrr5r;rrrrrr )rcrrr rr(rRrRrSr!s  zPydanticModelField.to_argumentrcCs|jSr\)__dict__rrRrRrSrszPydanticModelField.serialize)rrrIcCs |f|Sr\rR)rrrrRrRrSrszPydanticModelField.deserializeN)rrrrrrrNrbr'r)rrr!rr classmethodrrRrRrRrSrs  rc@szeZdZdeeeeeeeeeeeedddZeeefdddZ edddd d Z eedd d d Z dS)rNrrrrrrcCs(||_||_||_||_||_||_dSr\r,)rcrrrrrrrRrRrSrbs zModelConfigData.__init__rcCsdd|jDS)NcSsi|]\}}|dk r||qSr\rR)rjkvrRrRrSrsz3ModelConfigData.set_values_dict..)r*rrrRrRrSrszModelConfigData.set_values_dict)rrIcCs2|dkr dS|D]\}}t|||qdSr\)rrr)rcrr-r.rRrRrSrszModelConfigData.update)rrrIcCst||dkrt|||dSr\)rr)rcrrrRrRrSr szModelConfigData.setdefault)NNNNNN) rrrrrrbrrrrrrrRrRrRrSrs" rz pydantic-ormzInvalid from_orm callZPydanticzpydantic-configzInvalid config valuezpydantic-aliaszDynamic alias disallowedzpydantic-unexpectedzUnexpected behaviorzpydantic-fieldzUntyped field disallowedzInvalid Field defaults) model_namerrrIcCs|jd|d|tddS)N"z" does not have orm_mode=Truecode)fail ERROR_ORM)r/rrrRrRrSrsr)rrrrIcCs|jd|d|tddS)NzInvalid value for "Config.r0r1)r3 ERROR_CONFIG)rrrrRrRrSrsr)rrrIcCs|jd|tddS)Nz#Required dynamic aliases disallowedr1)r3 ERROR_ALIASrrrRrRrSrsr)rrrrIcCs4d}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!r1)r3ERROR_UNEXPECTED)rrrlinkZ full_messagerRrRrSr"s rcCs|jd|tddS)NzUntyped fields disallowedr1)r3 ERROR_UNTYPEDr7rRrRrSr,srcCs|jd|tddS)Nz>Field default and default_factory cannot be specified togetherr1)r3ERROR_FIELD_DEFAULTSr7rRrRrSr0srF) rrrr r r r is_newrIcCs|jj}||jkr@|j|} | jr@t| jtr@|jjj | j|pJt |}|sT|rpt t dt |dtg} n |pzt |}t t d|dtg} | |}ggg} } } |D]:}|jstd| |j| t|j| |jq|jtd}t| | | ||}|r|g|_t||ttg}||_t|||_||_t |d||_!|j"|_"||jkrt#||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 .functionrKTr+)+rrrZplugin_generatedrnrorrrrrArr)r<Zmake_normalizedrr(rrrr'r)rrrr6 variablesrr r3rrrgrrrBZ is_decoratedr rrr%rZdefn)rrrr r r r r=rrtfirstrrZ arg_kindsrZ function_type signaturerZr_namer.decrRrRrSr4sV            r)xrIcCs|j}t|r|S|Szc Used for compatibility with mypy 0.740; can be dropped once support for 0.740 is dropped. )rfcallablerCfnrRrRrSrgsrgcCs|j}t|r|S|SrD)rrErFrRrRrSrsr)rrIc Cs|dsdSd}tjdkr&ddl}n^z ddl}WnPtk rd}z ddl}Wn*tk r|ddl}|dYYdSXYnXt ||}| |W5QRSQRXdS)Nz.tomlrb) rrzJNo TOML parser installed, cannot read configuration from `pyproject.toml`.) r}sys version_infotomllibtomli ImportErrortomlwarningswarnopenload)rZ read_modeZtoml_rRrfrRrRrSrs"       r)NNFF)|rL 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>r?r@Z mypy.typevarsrAZ mypy.utilrBZ mypy.versionrCZ mypy_versionZpydantic.v1.utilsrDrErPrrrrrhrrvrzrrrNrTrrrYrXr]r~rrrr4r5r6r9r;r<rrrrrrrrrgrrrRrRrRrSs , | $   <           u%.%          S