mypy.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. """This module includes classes and functions designed specifically for use with the mypy plugin."""
  2. from __future__ import annotations
  3. import sys
  4. from collections.abc import Iterator
  5. from configparser import ConfigParser
  6. from typing import Any, Callable
  7. from mypy.errorcodes import ErrorCode
  8. from mypy.expandtype import expand_type, expand_type_by_instance
  9. from mypy.nodes import (
  10. ARG_NAMED,
  11. ARG_NAMED_OPT,
  12. ARG_OPT,
  13. ARG_POS,
  14. ARG_STAR2,
  15. INVARIANT,
  16. MDEF,
  17. Argument,
  18. AssignmentStmt,
  19. Block,
  20. CallExpr,
  21. ClassDef,
  22. Context,
  23. Decorator,
  24. DictExpr,
  25. EllipsisExpr,
  26. Expression,
  27. FuncDef,
  28. IfStmt,
  29. JsonDict,
  30. MemberExpr,
  31. NameExpr,
  32. PassStmt,
  33. PlaceholderNode,
  34. RefExpr,
  35. Statement,
  36. StrExpr,
  37. SymbolTableNode,
  38. TempNode,
  39. TypeAlias,
  40. TypeInfo,
  41. Var,
  42. )
  43. from mypy.options import Options
  44. from mypy.plugin import (
  45. CheckerPluginInterface,
  46. ClassDefContext,
  47. DynamicClassDefContext,
  48. MethodContext,
  49. Plugin,
  50. ReportConfigContext,
  51. SemanticAnalyzerPluginInterface,
  52. )
  53. from mypy.plugins.common import (
  54. deserialize_and_fixup_type,
  55. )
  56. from mypy.semanal import set_callable_name
  57. from mypy.server.trigger import make_wildcard_trigger
  58. from mypy.state import state
  59. from mypy.type_visitor import TypeTranslator
  60. from mypy.typeops import map_type_from_supertype
  61. from mypy.types import (
  62. AnyType,
  63. CallableType,
  64. Instance,
  65. NoneType,
  66. Type,
  67. TypeOfAny,
  68. TypeType,
  69. TypeVarType,
  70. UnionType,
  71. get_proper_type,
  72. )
  73. from mypy.typevars import fill_typevars
  74. from mypy.util import get_unique_redefinition_name
  75. from mypy.version import __version__ as mypy_version
  76. from pydantic._internal import _fields
  77. from pydantic.version import parse_mypy_version
  78. CONFIGFILE_KEY = 'pydantic-mypy'
  79. METADATA_KEY = 'pydantic-mypy-metadata'
  80. BASEMODEL_FULLNAME = 'pydantic.main.BaseModel'
  81. CREATE_MODEL_FULLNAME = 'pydantic.main.create_model'
  82. BASESETTINGS_FULLNAME = 'pydantic_settings.main.BaseSettings'
  83. ROOT_MODEL_FULLNAME = 'pydantic.root_model.RootModel'
  84. MODEL_METACLASS_FULLNAME = 'pydantic._internal._model_construction.ModelMetaclass'
  85. FIELD_FULLNAME = 'pydantic.fields.Field'
  86. DATACLASS_FULLNAME = 'pydantic.dataclasses.dataclass'
  87. MODEL_VALIDATOR_FULLNAME = 'pydantic.functional_validators.model_validator'
  88. DECORATOR_FULLNAMES = {
  89. 'pydantic.functional_validators.field_validator',
  90. 'pydantic.functional_validators.model_validator',
  91. 'pydantic.functional_serializers.serializer',
  92. 'pydantic.functional_serializers.model_serializer',
  93. 'pydantic.deprecated.class_validators.validator',
  94. 'pydantic.deprecated.class_validators.root_validator',
  95. }
  96. IMPLICIT_CLASSMETHOD_DECORATOR_FULLNAMES = DECORATOR_FULLNAMES - {'pydantic.functional_serializers.model_serializer'}
  97. MYPY_VERSION_TUPLE = parse_mypy_version(mypy_version)
  98. BUILTINS_NAME = 'builtins'
  99. # Increment version if plugin changes and mypy caches should be invalidated
  100. __version__ = 2
  101. def plugin(version: str) -> type[Plugin]:
  102. """`version` is the mypy version string.
  103. We might want to use this to print a warning if the mypy version being used is
  104. newer, or especially older, than we expect (or need).
  105. Args:
  106. version: The mypy version string.
  107. Return:
  108. The Pydantic mypy plugin type.
  109. """
  110. return PydanticPlugin
  111. class PydanticPlugin(Plugin):
  112. """The Pydantic mypy plugin."""
  113. def __init__(self, options: Options) -> None:
  114. self.plugin_config = PydanticPluginConfig(options)
  115. self._plugin_data = self.plugin_config.to_data()
  116. super().__init__(options)
  117. def get_base_class_hook(self, fullname: str) -> Callable[[ClassDefContext], None] | None:
  118. """Update Pydantic model class."""
  119. sym = self.lookup_fully_qualified(fullname)
  120. if sym and isinstance(sym.node, TypeInfo): # pragma: no branch
  121. # No branching may occur if the mypy cache has not been cleared
  122. if sym.node.has_base(BASEMODEL_FULLNAME):
  123. return self._pydantic_model_class_maker_callback
  124. return None
  125. def get_metaclass_hook(self, fullname: str) -> Callable[[ClassDefContext], None] | None:
  126. """Update Pydantic `ModelMetaclass` definition."""
  127. if fullname == MODEL_METACLASS_FULLNAME:
  128. return self._pydantic_model_metaclass_marker_callback
  129. return None
  130. def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None:
  131. """Adjust return type of `from_orm` method call."""
  132. if fullname.endswith('.from_orm'):
  133. return from_attributes_callback
  134. return None
  135. def get_dynamic_class_hook(self, fullname: str) -> Callable[[DynamicClassDefContext], None] | None:
  136. """Recognize `create_model()` calls as dynamic BaseModel subclasses."""
  137. if fullname == CREATE_MODEL_FULLNAME:
  138. return self._pydantic_create_model_callback
  139. return None
  140. def report_config_data(self, ctx: ReportConfigContext) -> dict[str, Any]:
  141. """Return all plugin config data.
  142. Used by mypy to determine if cache needs to be discarded.
  143. """
  144. return self._plugin_data
  145. def _pydantic_model_class_maker_callback(self, ctx: ClassDefContext) -> None:
  146. transformer = PydanticModelTransformer(ctx.cls, ctx.reason, ctx.api, self.plugin_config)
  147. transformer.transform()
  148. def _pydantic_model_metaclass_marker_callback(self, ctx: ClassDefContext) -> None:
  149. """Reset dataclass_transform_spec attribute of ModelMetaclass.
  150. Let the plugin handle it. This behavior can be disabled
  151. if 'debug_dataclass_transform' is set to True', for testing purposes.
  152. """
  153. if self.plugin_config.debug_dataclass_transform:
  154. return
  155. info_metaclass = ctx.cls.info.declared_metaclass
  156. assert info_metaclass, "callback not passed from 'get_metaclass_hook'"
  157. if getattr(info_metaclass.type, 'dataclass_transform_spec', None):
  158. info_metaclass.type.dataclass_transform_spec = None
  159. def _pydantic_create_model_callback(self, ctx: DynamicClassDefContext) -> None:
  160. """Make variables assigned from `create_model()` usable as types by mypy."""
  161. # Determine the base class from __base__ argument if provided
  162. base_fullname = BASEMODEL_FULLNAME
  163. for arg_name, arg_expr in zip(ctx.call.arg_names, ctx.call.args):
  164. if arg_name == '__base__' and isinstance(arg_expr, RefExpr) and arg_expr.node is not None:
  165. if isinstance(arg_expr.node, TypeInfo):
  166. base_fullname = arg_expr.node.fullname
  167. elif isinstance(arg_expr.node, Var) and isinstance(arg_expr.node.type, Instance):
  168. base_fullname = arg_expr.node.type.type.fullname
  169. base_sym = ctx.api.lookup_fully_qualified_or_none(base_fullname)
  170. if base_sym is None or not isinstance(base_sym.node, TypeInfo):
  171. # Fall back to BaseModel
  172. base_sym = ctx.api.lookup_fully_qualified_or_none(BASEMODEL_FULLNAME)
  173. if base_sym is None or not isinstance(base_sym.node, TypeInfo):
  174. return
  175. base_info = base_sym.node
  176. base_instance = fill_typevars(base_info)
  177. assert isinstance(base_instance, Instance)
  178. info = ctx.api.basic_new_typeinfo(ctx.name, base_instance, ctx.call.line)
  179. info.metaclass_type = base_info.metaclass_type
  180. ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(MDEF, info))
  181. class PydanticPluginConfig:
  182. """A Pydantic mypy plugin config holder.
  183. Attributes:
  184. init_forbid_extra: Whether to add a `**kwargs` at the end of the generated `__init__` signature.
  185. init_typed: Whether to annotate fields in the generated `__init__`.
  186. warn_required_dynamic_aliases: Whether to raise required dynamic aliases error.
  187. debug_dataclass_transform: Whether to not reset `dataclass_transform_spec` attribute
  188. of `ModelMetaclass` for testing purposes.
  189. """
  190. __slots__ = (
  191. 'init_forbid_extra',
  192. 'init_typed',
  193. 'warn_required_dynamic_aliases',
  194. 'debug_dataclass_transform',
  195. )
  196. init_forbid_extra: bool
  197. init_typed: bool
  198. warn_required_dynamic_aliases: bool
  199. debug_dataclass_transform: bool # undocumented
  200. def __init__(self, options: Options) -> None:
  201. if options.config_file is None: # pragma: no cover
  202. return
  203. toml_config = parse_toml(options.config_file)
  204. if toml_config is not None:
  205. config = toml_config.get('tool', {}).get('pydantic-mypy', {})
  206. for key in self.__slots__:
  207. setting = config.get(key, False)
  208. if not isinstance(setting, bool):
  209. raise ValueError(f'Configuration value must be a boolean for key: {key}')
  210. setattr(self, key, setting)
  211. else:
  212. plugin_config = ConfigParser()
  213. plugin_config.read(options.config_file)
  214. for key in self.__slots__:
  215. setting = plugin_config.getboolean(CONFIGFILE_KEY, key, fallback=False)
  216. setattr(self, key, setting)
  217. def to_data(self) -> dict[str, Any]:
  218. """Returns a dict of config names to their values."""
  219. return {key: getattr(self, key) for key in self.__slots__}
  220. def from_attributes_callback(ctx: MethodContext) -> Type:
  221. """Raise an error if from_attributes is not enabled."""
  222. model_type: Instance
  223. ctx_type = ctx.type
  224. if isinstance(ctx_type, TypeType):
  225. ctx_type = ctx_type.item
  226. if isinstance(ctx_type, CallableType) and isinstance(ctx_type.ret_type, Instance):
  227. model_type = ctx_type.ret_type # called on the class
  228. elif isinstance(ctx_type, Instance):
  229. model_type = ctx_type # called on an instance (unusual, but still valid)
  230. else: # pragma: no cover
  231. detail = f'ctx.type: {ctx_type} (of type {ctx_type.__class__.__name__})'
  232. error_unexpected_behavior(detail, ctx.api, ctx.context)
  233. return ctx.default_return_type
  234. pydantic_metadata = model_type.type.metadata.get(METADATA_KEY)
  235. if pydantic_metadata is None:
  236. return ctx.default_return_type
  237. if not model_type.type.has_base(BASEMODEL_FULLNAME):
  238. # not a Pydantic v2 model
  239. return ctx.default_return_type
  240. from_attributes = pydantic_metadata.get('config', {}).get('from_attributes')
  241. if from_attributes is not True:
  242. error_from_attributes(model_type.type.name, ctx.api, ctx.context)
  243. return ctx.default_return_type
  244. class PydanticModelField:
  245. """Based on mypy.plugins.dataclasses.DataclassAttribute."""
  246. def __init__(
  247. self,
  248. name: str,
  249. alias: str | None,
  250. is_frozen: bool,
  251. has_dynamic_alias: bool,
  252. has_default: bool,
  253. strict: bool | None,
  254. line: int,
  255. column: int,
  256. type: Type | None,
  257. info: TypeInfo,
  258. ):
  259. self.name = name
  260. self.alias = alias
  261. self.is_frozen = is_frozen
  262. self.has_dynamic_alias = has_dynamic_alias
  263. self.has_default = has_default
  264. self.strict = strict
  265. self.line = line
  266. self.column = column
  267. self.type = type
  268. self.info = info
  269. def to_argument(
  270. self,
  271. current_info: TypeInfo,
  272. typed: bool,
  273. model_strict: bool,
  274. force_optional: bool,
  275. use_alias: bool,
  276. api: SemanticAnalyzerPluginInterface,
  277. force_typevars_invariant: bool,
  278. is_root_model_root: bool,
  279. ) -> Argument:
  280. """Based on mypy.plugins.dataclasses.DataclassAttribute.to_argument."""
  281. variable = self.to_var(current_info, api, use_alias, force_typevars_invariant)
  282. strict = model_strict if self.strict is None else self.strict
  283. if typed or strict:
  284. type_annotation = self.expand_type(current_info, api, include_root_type=True)
  285. else:
  286. type_annotation = AnyType(TypeOfAny.explicit)
  287. return Argument(
  288. variable=variable,
  289. type_annotation=type_annotation,
  290. initializer=None,
  291. kind=ARG_OPT
  292. if is_root_model_root
  293. else (ARG_NAMED_OPT if force_optional or self.has_default else ARG_NAMED),
  294. )
  295. def expand_type(
  296. self,
  297. current_info: TypeInfo,
  298. api: SemanticAnalyzerPluginInterface,
  299. force_typevars_invariant: bool = False,
  300. include_root_type: bool = False,
  301. ) -> Type | None:
  302. """Based on mypy.plugins.dataclasses.DataclassAttribute.expand_type."""
  303. if force_typevars_invariant:
  304. # In some cases, mypy will emit an error "Cannot use a covariant type variable as a parameter"
  305. # To prevent that, we add an option to replace typevars with invariant ones while building certain
  306. # method signatures (in particular, `__init__`). There may be a better way to do this, if this causes
  307. # us problems in the future, we should look into why the dataclasses plugin doesn't have this issue.
  308. if isinstance(self.type, TypeVarType):
  309. modified_type = self.type.copy_modified()
  310. modified_type.variance = INVARIANT
  311. self.type = modified_type
  312. if self.type is not None and self.info.self_type is not None:
  313. # In general, it is not safe to call `expand_type()` during semantic analysis,
  314. # however this plugin is called very late, so all types should be fully ready.
  315. # Also, it is tricky to avoid eager expansion of Self types here (e.g. because
  316. # we serialize attributes).
  317. with state.strict_optional_set(api.options.strict_optional):
  318. filled_with_typevars = fill_typevars(current_info)
  319. # Cannot be TupleType as current_info represents a Pydantic model:
  320. assert isinstance(filled_with_typevars, Instance)
  321. if force_typevars_invariant:
  322. for arg in filled_with_typevars.args:
  323. if isinstance(arg, TypeVarType):
  324. arg.variance = INVARIANT
  325. expanded_type = expand_type(self.type, {self.info.self_type.id: filled_with_typevars})
  326. if include_root_type and isinstance(expanded_type, Instance) and is_root_model(expanded_type.type):
  327. # When a root model is used as a field, Pydantic allows both an instance of the root model
  328. # as well as instances of the `root` field type:
  329. root_type = expanded_type.type['root'].type
  330. if root_type is None:
  331. # Happens if the hint for 'root' has unsolved forward references
  332. return expanded_type
  333. expanded_root_type = expand_type_by_instance(root_type, expanded_type)
  334. expanded_type = UnionType([expanded_type, expanded_root_type])
  335. return expanded_type
  336. return self.type
  337. def to_var(
  338. self,
  339. current_info: TypeInfo,
  340. api: SemanticAnalyzerPluginInterface,
  341. use_alias: bool,
  342. force_typevars_invariant: bool = False,
  343. ) -> Var:
  344. """Based on mypy.plugins.dataclasses.DataclassAttribute.to_var."""
  345. if use_alias and self.alias is not None:
  346. name = self.alias
  347. else:
  348. name = self.name
  349. return Var(name, self.expand_type(current_info, api, force_typevars_invariant))
  350. def serialize(self) -> JsonDict:
  351. """Based on mypy.plugins.dataclasses.DataclassAttribute.serialize."""
  352. assert self.type
  353. return {
  354. 'name': self.name,
  355. 'alias': self.alias,
  356. 'is_frozen': self.is_frozen,
  357. 'has_dynamic_alias': self.has_dynamic_alias,
  358. 'has_default': self.has_default,
  359. 'strict': self.strict,
  360. 'line': self.line,
  361. 'column': self.column,
  362. 'type': self.type.serialize(),
  363. }
  364. @classmethod
  365. def deserialize(cls, info: TypeInfo, data: JsonDict, api: SemanticAnalyzerPluginInterface) -> PydanticModelField:
  366. """Based on mypy.plugins.dataclasses.DataclassAttribute.deserialize."""
  367. data = data.copy()
  368. typ = deserialize_and_fixup_type(data.pop('type'), api)
  369. return cls(type=typ, info=info, **data)
  370. def expand_typevar_from_subtype(self, sub_type: TypeInfo, api: SemanticAnalyzerPluginInterface) -> None:
  371. """Expands type vars in the context of a subtype when an attribute is inherited
  372. from a generic super type.
  373. """
  374. if self.type is not None:
  375. with state.strict_optional_set(api.options.strict_optional):
  376. self.type = map_type_from_supertype(self.type, sub_type, self.info)
  377. class PydanticModelClassVar:
  378. """Based on mypy.plugins.dataclasses.DataclassAttribute.
  379. ClassVars are ignored by subclasses.
  380. Attributes:
  381. name: the ClassVar name
  382. """
  383. def __init__(self, name):
  384. self.name = name
  385. @classmethod
  386. def deserialize(cls, data: JsonDict) -> PydanticModelClassVar:
  387. """Based on mypy.plugins.dataclasses.DataclassAttribute.deserialize."""
  388. data = data.copy()
  389. return cls(**data)
  390. def serialize(self) -> JsonDict:
  391. """Based on mypy.plugins.dataclasses.DataclassAttribute.serialize."""
  392. return {
  393. 'name': self.name,
  394. }
  395. class PydanticModelTransformer:
  396. """Transform the BaseModel subclass according to the plugin settings.
  397. Attributes:
  398. tracked_config_fields: A set of field configs that the plugin has to track their value.
  399. """
  400. tracked_config_fields: set[str] = {
  401. 'extra',
  402. 'frozen',
  403. 'from_attributes',
  404. 'populate_by_name',
  405. 'validate_by_alias',
  406. 'validate_by_name',
  407. 'alias_generator',
  408. 'strict',
  409. }
  410. def __init__(
  411. self,
  412. cls: ClassDef,
  413. reason: Expression | Statement,
  414. api: SemanticAnalyzerPluginInterface,
  415. plugin_config: PydanticPluginConfig,
  416. ) -> None:
  417. self._cls = cls
  418. self._reason = reason
  419. self._api = api
  420. self.plugin_config = plugin_config
  421. def transform(self) -> bool:
  422. """Configures the BaseModel subclass according to the plugin settings.
  423. In particular:
  424. * determines the model config and fields,
  425. * adds a fields-aware signature for the initializer and construct methods
  426. * freezes the class if frozen = True
  427. * stores the fields, config, and if the class is settings in the mypy metadata for access by subclasses
  428. """
  429. info = self._cls.info
  430. is_a_root_model = is_root_model(info)
  431. config = self.collect_config()
  432. fields, class_vars = self.collect_fields_and_class_vars(config, is_a_root_model)
  433. if fields is None or class_vars is None:
  434. # Some definitions are not ready. We need another pass.
  435. return False
  436. for field in fields:
  437. if field.type is None:
  438. return False
  439. is_settings = info.has_base(BASESETTINGS_FULLNAME)
  440. self.add_initializer(fields, config, is_settings, is_a_root_model)
  441. self.add_model_construct_method(fields, config, is_settings, is_a_root_model)
  442. self.set_frozen(fields, self._api, frozen=config.frozen is True)
  443. self.adjust_decorator_signatures()
  444. info.metadata[METADATA_KEY] = {
  445. 'fields': {field.name: field.serialize() for field in fields},
  446. 'class_vars': {class_var.name: class_var.serialize() for class_var in class_vars},
  447. 'config': config.get_values_dict(),
  448. }
  449. return True
  450. def adjust_decorator_signatures(self) -> None:
  451. """When we decorate a function `f` with `pydantic.validator(...)`, `pydantic.field_validator`
  452. or `pydantic.serializer(...)`, mypy sees `f` as a regular method taking a `self` instance,
  453. even though pydantic internally wraps `f` with `classmethod` if necessary.
  454. Teach mypy this by marking any function whose outermost decorator is a `validator()`,
  455. `field_validator()` or `serializer()` call as a `classmethod`.
  456. """
  457. for sym in self._cls.info.names.values():
  458. if isinstance(sym.node, Decorator):
  459. first_dec = sym.node.original_decorators[0]
  460. if (
  461. isinstance(first_dec, CallExpr)
  462. and isinstance(first_dec.callee, NameExpr)
  463. and first_dec.callee.fullname in IMPLICIT_CLASSMETHOD_DECORATOR_FULLNAMES
  464. # @model_validator(mode="after") is an exception, it expects a regular method
  465. and not (
  466. first_dec.callee.fullname == MODEL_VALIDATOR_FULLNAME
  467. and any(
  468. first_dec.arg_names[i] == 'mode' and isinstance(arg, StrExpr) and arg.value == 'after'
  469. for i, arg in enumerate(first_dec.args)
  470. )
  471. )
  472. ):
  473. # TODO: Only do this if the first argument of the decorated function is `cls`
  474. sym.node.func.is_class = True
  475. def collect_config(self) -> ModelConfigData: # noqa: C901 (ignore complexity)
  476. """Collects the values of the config attributes that are used by the plugin, accounting for parent classes."""
  477. cls = self._cls
  478. config = ModelConfigData()
  479. has_config_kwargs = False
  480. has_config_from_namespace = False
  481. # Handle `class MyModel(BaseModel, <name>=<expr>, ...):`
  482. for name, expr in cls.keywords.items():
  483. config_data = self.get_config_update(name, expr)
  484. if config_data:
  485. has_config_kwargs = True
  486. config.update(config_data)
  487. # Handle `model_config`
  488. stmt: Statement | None = None
  489. for stmt in cls.defs.body:
  490. if not isinstance(stmt, (AssignmentStmt, ClassDef)):
  491. continue
  492. if isinstance(stmt, AssignmentStmt):
  493. lhs = stmt.lvalues[0]
  494. if not isinstance(lhs, NameExpr) or lhs.name != 'model_config':
  495. continue
  496. if isinstance(stmt.rvalue, CallExpr): # calls to `dict` or `ConfigDict`
  497. for arg_name, arg in zip(stmt.rvalue.arg_names, stmt.rvalue.args):
  498. if arg_name is None:
  499. continue
  500. config.update(self.get_config_update(arg_name, arg, lax_extra=True))
  501. elif isinstance(stmt.rvalue, DictExpr): # dict literals
  502. for key_expr, value_expr in stmt.rvalue.items:
  503. if not isinstance(key_expr, StrExpr):
  504. continue
  505. config.update(self.get_config_update(key_expr.value, value_expr))
  506. elif isinstance(stmt, ClassDef):
  507. if stmt.name != 'Config': # 'deprecated' Config-class
  508. continue
  509. for substmt in stmt.defs.body:
  510. if not isinstance(substmt, AssignmentStmt):
  511. continue
  512. lhs = substmt.lvalues[0]
  513. if not isinstance(lhs, NameExpr):
  514. continue
  515. config.update(self.get_config_update(lhs.name, substmt.rvalue))
  516. if has_config_kwargs:
  517. self._api.fail(
  518. 'Specifying config in two places is ambiguous, use either Config attribute or class kwargs',
  519. cls,
  520. )
  521. break
  522. has_config_from_namespace = True
  523. if has_config_kwargs or has_config_from_namespace:
  524. if (
  525. stmt
  526. and config.has_alias_generator
  527. and not (config.validate_by_name or config.populate_by_name)
  528. and self.plugin_config.warn_required_dynamic_aliases
  529. ):
  530. error_required_dynamic_aliases(self._api, stmt)
  531. for info in cls.info.mro[1:]: # 0 is the current class
  532. if METADATA_KEY not in info.metadata:
  533. continue
  534. # Each class depends on the set of fields in its ancestors
  535. self._api.add_plugin_dependency(make_wildcard_trigger(info.fullname))
  536. for name, value in info.metadata[METADATA_KEY]['config'].items():
  537. config.setdefault(name, value)
  538. return config
  539. def collect_fields_and_class_vars(
  540. self, model_config: ModelConfigData, is_root_model: bool
  541. ) -> tuple[list[PydanticModelField] | None, list[PydanticModelClassVar] | None]:
  542. """Collects the fields for the model, accounting for parent classes."""
  543. cls = self._cls
  544. # First, collect fields and ClassVars belonging to any class in the MRO, ignoring duplicates.
  545. #
  546. # We iterate through the MRO in reverse because attrs defined in the parent must appear
  547. # earlier in the attributes list than attrs defined in the child. See:
  548. # https://docs.python.org/3/library/dataclasses.html#inheritance
  549. #
  550. # However, we also want fields defined in the subtype to override ones defined
  551. # in the parent. We can implement this via a dict without disrupting the attr order
  552. # because dicts preserve insertion order in Python 3.7+.
  553. found_fields: dict[str, PydanticModelField] = {}
  554. found_class_vars: dict[str, PydanticModelClassVar] = {}
  555. for info in reversed(cls.info.mro[1:-1]): # 0 is the current class, -2 is BaseModel, -1 is object
  556. # if BASEMODEL_METADATA_TAG_KEY in info.metadata and BASEMODEL_METADATA_KEY not in info.metadata:
  557. # # We haven't processed the base class yet. Need another pass.
  558. # return None, None
  559. if METADATA_KEY not in info.metadata:
  560. continue
  561. # Each class depends on the set of attributes in its dataclass ancestors.
  562. self._api.add_plugin_dependency(make_wildcard_trigger(info.fullname))
  563. for name, data in info.metadata[METADATA_KEY]['fields'].items():
  564. field = PydanticModelField.deserialize(info, data, self._api)
  565. # (The following comment comes directly from the dataclasses plugin)
  566. # TODO: We shouldn't be performing type operations during the main
  567. # semantic analysis pass, since some TypeInfo attributes might
  568. # still be in flux. This should be performed in a later phase.
  569. field.expand_typevar_from_subtype(cls.info, self._api)
  570. found_fields[name] = field
  571. sym_node = cls.info.names.get(name)
  572. if sym_node and sym_node.node and not isinstance(sym_node.node, (Var, PlaceholderNode)):
  573. self._api.fail(
  574. 'BaseModel field may only be overridden by another field',
  575. sym_node.node,
  576. )
  577. # Collect ClassVars
  578. for name, data in info.metadata[METADATA_KEY]['class_vars'].items():
  579. found_class_vars[name] = PydanticModelClassVar.deserialize(data)
  580. # Second, collect fields and ClassVars belonging to the current class.
  581. current_field_names: set[str] = set()
  582. current_class_vars_names: set[str] = set()
  583. for stmt in self._get_assignment_statements_from_block(cls.defs):
  584. maybe_field = self.collect_field_or_class_var_from_stmt(stmt, model_config, found_class_vars)
  585. if maybe_field is None:
  586. continue
  587. lhs = stmt.lvalues[0]
  588. assert isinstance(lhs, NameExpr) # collect_field_or_class_var_from_stmt guarantees this
  589. if isinstance(maybe_field, PydanticModelField):
  590. if is_root_model and lhs.name != 'root':
  591. error_extra_fields_on_root_model(self._api, stmt)
  592. else:
  593. current_field_names.add(lhs.name)
  594. found_fields[lhs.name] = maybe_field
  595. elif isinstance(maybe_field, PydanticModelClassVar):
  596. current_class_vars_names.add(lhs.name)
  597. found_class_vars[lhs.name] = maybe_field
  598. return list(found_fields.values()), list(found_class_vars.values())
  599. def _get_assignment_statements_from_if_statement(self, stmt: IfStmt) -> Iterator[AssignmentStmt]:
  600. for body in stmt.body:
  601. if not body.is_unreachable:
  602. yield from self._get_assignment_statements_from_block(body)
  603. if stmt.else_body is not None and not stmt.else_body.is_unreachable:
  604. yield from self._get_assignment_statements_from_block(stmt.else_body)
  605. def _get_assignment_statements_from_block(self, block: Block) -> Iterator[AssignmentStmt]:
  606. for stmt in block.body:
  607. if isinstance(stmt, AssignmentStmt):
  608. yield stmt
  609. elif isinstance(stmt, IfStmt):
  610. yield from self._get_assignment_statements_from_if_statement(stmt)
  611. def collect_field_or_class_var_from_stmt( # noqa C901
  612. self, stmt: AssignmentStmt, model_config: ModelConfigData, class_vars: dict[str, PydanticModelClassVar]
  613. ) -> PydanticModelField | PydanticModelClassVar | None:
  614. """Get pydantic model field from statement.
  615. Args:
  616. stmt: The statement.
  617. model_config: Configuration settings for the model.
  618. class_vars: ClassVars already known to be defined on the model.
  619. Returns:
  620. A pydantic model field if it could find the field in statement. Otherwise, `None`.
  621. """
  622. cls = self._cls
  623. lhs = stmt.lvalues[0]
  624. if not isinstance(lhs, NameExpr) or not _fields.is_valid_field_name(lhs.name) or lhs.name == 'model_config':
  625. return None
  626. if not stmt.new_syntax:
  627. if (
  628. isinstance(stmt.rvalue, CallExpr)
  629. and isinstance(stmt.rvalue.callee, CallExpr)
  630. and isinstance(stmt.rvalue.callee.callee, NameExpr)
  631. and stmt.rvalue.callee.callee.fullname in DECORATOR_FULLNAMES
  632. ):
  633. # This is a (possibly-reused) validator or serializer, not a field
  634. # In particular, it looks something like: my_validator = validator('my_field')(f)
  635. # Eventually, we may want to attempt to respect model_config['ignored_types']
  636. return None
  637. if lhs.name in class_vars:
  638. # Class vars are not fields and are not required to be annotated
  639. return None
  640. # The assignment does not have an annotation, and it's not anything else we recognize
  641. error_untyped_fields(self._api, stmt)
  642. return None
  643. lhs = stmt.lvalues[0]
  644. if not isinstance(lhs, NameExpr):
  645. return None
  646. if not _fields.is_valid_field_name(lhs.name) or lhs.name == 'model_config':
  647. return None
  648. sym = cls.info.names.get(lhs.name)
  649. if sym is None: # pragma: no cover
  650. # This is likely due to a star import (see the dataclasses plugin for a more detailed explanation)
  651. # This is the same logic used in the dataclasses plugin
  652. return None
  653. node = sym.node
  654. if isinstance(node, PlaceholderNode): # pragma: no cover
  655. # See the PlaceholderNode docstring for more detail about how this can occur
  656. # Basically, it is an edge case when dealing with complex import logic
  657. # The dataclasses plugin now asserts this cannot happen, but I'd rather not error if it does..
  658. return None
  659. if isinstance(node, TypeAlias):
  660. self._api.fail(
  661. 'Type aliases inside BaseModel definitions are not supported at runtime',
  662. node,
  663. )
  664. # Skip processing this node. This doesn't match the runtime behaviour,
  665. # but the only alternative would be to modify the SymbolTable,
  666. # and it's a little hairy to do that in a plugin.
  667. return None
  668. if not isinstance(node, Var): # pragma: no cover
  669. # Don't know if this edge case still happens with the `is_valid_field` check above
  670. # but better safe than sorry
  671. # The dataclasses plugin now asserts this cannot happen, but I'd rather not error if it does..
  672. return None
  673. # x: ClassVar[int] is not a field
  674. if node.is_classvar:
  675. return PydanticModelClassVar(lhs.name)
  676. # x: InitVar[int] is not supported in BaseModel
  677. node_type = get_proper_type(node.type)
  678. if isinstance(node_type, Instance) and node_type.type.fullname == 'dataclasses.InitVar':
  679. self._api.fail(
  680. 'InitVar is not supported in BaseModel',
  681. node,
  682. )
  683. has_default = self.get_has_default(stmt)
  684. strict = self.get_strict(stmt)
  685. if sym.type is None and node.is_final and node.is_inferred:
  686. # This follows the logic from the dataclasses plugin. The following comment is taken verbatim:
  687. #
  688. # This is a special case, assignment like x: Final = 42 is classified
  689. # annotated above, but mypy strips the `Final` turning it into x = 42.
  690. # We do not support inferred types in dataclasses, so we can try inferring
  691. # type for simple literals, and otherwise require an explicit type
  692. # argument for Final[...].
  693. typ = self._api.analyze_simple_literal_type(stmt.rvalue, is_final=True)
  694. if typ:
  695. node.type = typ
  696. else:
  697. self._api.fail(
  698. 'Need type argument for Final[...] with non-literal default in BaseModel',
  699. stmt,
  700. )
  701. node.type = AnyType(TypeOfAny.from_error)
  702. if node.is_final and has_default:
  703. # TODO this path should be removed (see https://github.com/pydantic/pydantic/issues/11119)
  704. return PydanticModelClassVar(lhs.name)
  705. alias, has_dynamic_alias = self.get_alias_info(stmt)
  706. if (
  707. has_dynamic_alias
  708. and not (model_config.validate_by_name or model_config.populate_by_name)
  709. and self.plugin_config.warn_required_dynamic_aliases
  710. ):
  711. error_required_dynamic_aliases(self._api, stmt)
  712. is_frozen = self.is_field_frozen(stmt)
  713. init_type = self._infer_dataclass_attr_init_type(sym, lhs.name, stmt)
  714. return PydanticModelField(
  715. name=lhs.name,
  716. has_dynamic_alias=has_dynamic_alias,
  717. has_default=has_default,
  718. strict=strict,
  719. alias=alias,
  720. is_frozen=is_frozen,
  721. line=stmt.line,
  722. column=stmt.column,
  723. type=init_type,
  724. info=cls.info,
  725. )
  726. def _infer_dataclass_attr_init_type(self, sym: SymbolTableNode, name: str, context: Context) -> Type | None:
  727. """Infer __init__ argument type for an attribute.
  728. In particular, possibly use the signature of __set__.
  729. """
  730. default = sym.type
  731. if sym.implicit:
  732. return default
  733. t = get_proper_type(sym.type)
  734. # Perform a simple-minded inference from the signature of __set__, if present.
  735. # We can't use mypy.checkmember here, since this plugin runs before type checking.
  736. # We only support some basic scanerios here, which is hopefully sufficient for
  737. # the vast majority of use cases.
  738. if not isinstance(t, Instance):
  739. return default
  740. setter = t.type.get('__set__')
  741. if setter:
  742. if isinstance(setter.node, FuncDef):
  743. super_info = t.type.get_containing_type_info('__set__')
  744. assert super_info
  745. if setter.type:
  746. setter_type = get_proper_type(map_type_from_supertype(setter.type, t.type, super_info))
  747. else:
  748. return AnyType(TypeOfAny.unannotated)
  749. if isinstance(setter_type, CallableType) and setter_type.arg_kinds == [
  750. ARG_POS,
  751. ARG_POS,
  752. ARG_POS,
  753. ]:
  754. return expand_type_by_instance(setter_type.arg_types[2], t)
  755. else:
  756. self._api.fail(f'Unsupported signature for "__set__" in "{t.type.name}"', context)
  757. else:
  758. self._api.fail(f'Unsupported "__set__" in "{t.type.name}"', context)
  759. return default
  760. def add_initializer(
  761. self, fields: list[PydanticModelField], config: ModelConfigData, is_settings: bool, is_root_model: bool
  762. ) -> None:
  763. """Adds a fields-aware `__init__` method to the class.
  764. The added `__init__` will be annotated with types vs. all `Any` depending on the plugin settings.
  765. """
  766. if '__init__' in self._cls.info.names and not self._cls.info.names['__init__'].plugin_generated:
  767. return # Don't generate an __init__ if one already exists
  768. typed = self.plugin_config.init_typed
  769. model_strict = bool(config.strict)
  770. use_alias = not (config.validate_by_name or config.populate_by_name) and config.validate_by_alias is not False
  771. requires_dynamic_aliases = bool(config.has_alias_generator and not config.validate_by_name)
  772. args = self.get_field_arguments(
  773. fields,
  774. typed=typed,
  775. model_strict=model_strict,
  776. requires_dynamic_aliases=requires_dynamic_aliases,
  777. use_alias=use_alias,
  778. is_settings=is_settings,
  779. is_root_model=is_root_model,
  780. force_typevars_invariant=True,
  781. )
  782. if is_settings:
  783. base_settings_node = self._api.lookup_fully_qualified(BASESETTINGS_FULLNAME).node
  784. assert isinstance(base_settings_node, TypeInfo)
  785. if '__init__' in base_settings_node.names:
  786. base_settings_init_node = base_settings_node.names['__init__'].node
  787. assert isinstance(base_settings_init_node, FuncDef)
  788. if base_settings_init_node is not None and base_settings_init_node.type is not None:
  789. func_type = base_settings_init_node.type
  790. assert isinstance(func_type, CallableType)
  791. for arg_idx, arg_name in enumerate(func_type.arg_names):
  792. if arg_name is None or arg_name.startswith('__') or not arg_name.startswith('_'):
  793. continue
  794. analyzed_variable_type = self._api.anal_type(func_type.arg_types[arg_idx])
  795. if analyzed_variable_type is not None and arg_name in (
  796. '_cli_settings_source',
  797. '_build_sources',
  798. ):
  799. # These arg names are annotated with types explicitly parameterized with `Any`, and as such
  800. # the Any causes issues with --disallow-any-explicit. As a workaround, change
  801. # the Any type (as if the generic type was left unparameterized):
  802. analyzed_variable_type = analyzed_variable_type.accept(
  803. ChangeExplicitTypeOfAny(TypeOfAny.from_omitted_generics)
  804. )
  805. variable = Var(arg_name, analyzed_variable_type)
  806. args.append(Argument(variable, analyzed_variable_type, None, ARG_OPT))
  807. if not self.should_init_forbid_extra(fields, config):
  808. var = Var('kwargs')
  809. args.append(Argument(var, AnyType(TypeOfAny.explicit), None, ARG_STAR2))
  810. add_method(self._api, self._cls, '__init__', args=args, return_type=NoneType())
  811. def add_model_construct_method(
  812. self,
  813. fields: list[PydanticModelField],
  814. config: ModelConfigData,
  815. is_settings: bool,
  816. is_root_model: bool,
  817. ) -> None:
  818. """Adds a fully typed `model_construct` classmethod to the class.
  819. Similar to the fields-aware __init__ method, but always uses the field names (not aliases),
  820. and does not treat settings fields as optional.
  821. """
  822. set_str = self._api.named_type(f'{BUILTINS_NAME}.set', [self._api.named_type(f'{BUILTINS_NAME}.str')])
  823. optional_set_str = UnionType([set_str, NoneType()])
  824. fields_set_argument = Argument(Var('_fields_set', optional_set_str), optional_set_str, None, ARG_OPT)
  825. with state.strict_optional_set(self._api.options.strict_optional):
  826. args = self.get_field_arguments(
  827. fields,
  828. typed=True,
  829. model_strict=bool(config.strict),
  830. requires_dynamic_aliases=False,
  831. use_alias=False,
  832. is_settings=is_settings,
  833. is_root_model=is_root_model,
  834. )
  835. if not self.should_init_forbid_extra(fields, config):
  836. var = Var('kwargs')
  837. args.append(Argument(var, AnyType(TypeOfAny.explicit), None, ARG_STAR2))
  838. args = args + [fields_set_argument] if is_root_model else [fields_set_argument] + args
  839. add_method(
  840. self._api,
  841. self._cls,
  842. 'model_construct',
  843. args=args,
  844. return_type=fill_typevars(self._cls.info),
  845. is_classmethod=True,
  846. )
  847. def set_frozen(self, fields: list[PydanticModelField], api: SemanticAnalyzerPluginInterface, frozen: bool) -> None:
  848. """Marks all fields as properties so that attempts to set them trigger mypy errors.
  849. This is the same approach used by the attrs and dataclasses plugins.
  850. """
  851. info = self._cls.info
  852. for field in fields:
  853. sym_node = info.names.get(field.name)
  854. if sym_node is not None:
  855. var = sym_node.node
  856. if isinstance(var, Var):
  857. var.is_property = frozen or field.is_frozen
  858. elif isinstance(var, PlaceholderNode) and not self._api.final_iteration:
  859. # See https://github.com/pydantic/pydantic/issues/5191 to hit this branch for test coverage
  860. self._api.defer()
  861. # `var` can also be a FuncDef or Decorator node (e.g. when overriding a field with a function or property).
  862. # In that case, we don't want to do anything. Mypy will already raise an error that a field was not properly
  863. # overridden.
  864. else:
  865. var = field.to_var(info, api, use_alias=False)
  866. var.info = info
  867. var.is_property = frozen
  868. var._fullname = info.fullname + '.' + var.name
  869. info.names[var.name] = SymbolTableNode(MDEF, var)
  870. def get_config_update(self, name: str, arg: Expression, lax_extra: bool = False) -> ModelConfigData | None:
  871. """Determines the config update due to a single kwarg in the ConfigDict definition.
  872. Warns if a tracked config attribute is set to a value the plugin doesn't know how to interpret (e.g., an int)
  873. """
  874. if name not in self.tracked_config_fields:
  875. return None
  876. if name == 'extra':
  877. if isinstance(arg, StrExpr):
  878. forbid_extra = arg.value == 'forbid'
  879. elif isinstance(arg, MemberExpr):
  880. forbid_extra = arg.name == 'forbid'
  881. else:
  882. if not lax_extra:
  883. # Only emit an error for other types of `arg` (e.g., `NameExpr`, `ConditionalExpr`, etc.) when
  884. # reading from a config class, etc. If a ConfigDict is used, then we don't want to emit an error
  885. # because you'll get type checking from the ConfigDict itself.
  886. #
  887. # It would be nice if we could introspect the types better otherwise, but I don't know what the API
  888. # is to evaluate an expr into its type and then check if that type is compatible with the expected
  889. # type. Note that you can still get proper type checking via: `model_config = ConfigDict(...)`, just
  890. # if you don't use an explicit string, the plugin won't be able to infer whether extra is forbidden.
  891. error_invalid_config_value(name, self._api, arg)
  892. return None
  893. return ModelConfigData(forbid_extra=forbid_extra)
  894. if name == 'alias_generator':
  895. has_alias_generator = True
  896. if isinstance(arg, NameExpr) and arg.fullname == 'builtins.None':
  897. has_alias_generator = False
  898. return ModelConfigData(has_alias_generator=has_alias_generator)
  899. if isinstance(arg, NameExpr) and arg.fullname in ('builtins.True', 'builtins.False'):
  900. return ModelConfigData(**{name: arg.fullname == 'builtins.True'})
  901. error_invalid_config_value(name, self._api, arg)
  902. return None
  903. @staticmethod
  904. def get_has_default(stmt: AssignmentStmt) -> bool:
  905. """Returns a boolean indicating whether the field defined in `stmt` is a required field."""
  906. expr = stmt.rvalue
  907. if isinstance(expr, TempNode):
  908. # TempNode means annotation-only, so has no default
  909. return False
  910. if isinstance(expr, CallExpr) and isinstance(expr.callee, RefExpr) and expr.callee.fullname == FIELD_FULLNAME:
  911. # The "default value" is a call to `Field`; at this point, the field has a default if and only if:
  912. # * there is a positional argument that is not `...`
  913. # * there is a keyword argument named "default" that is not `...`
  914. # * there is a "default_factory" that is not `None`
  915. for arg, name in zip(expr.args, expr.arg_names):
  916. # If name is None, then this arg is the default because it is the only positional argument.
  917. if name is None or name == 'default':
  918. return arg.__class__ is not EllipsisExpr
  919. if name == 'default_factory':
  920. return not (isinstance(arg, NameExpr) and arg.fullname == 'builtins.None')
  921. return False
  922. # Has no default if the "default value" is Ellipsis (i.e., `field_name: Annotation = ...`)
  923. return not isinstance(expr, EllipsisExpr)
  924. @staticmethod
  925. def get_strict(stmt: AssignmentStmt) -> bool | None:
  926. """Returns a the `strict` value of a field if defined, otherwise `None`."""
  927. expr = stmt.rvalue
  928. if isinstance(expr, CallExpr) and isinstance(expr.callee, RefExpr) and expr.callee.fullname == FIELD_FULLNAME:
  929. for arg, name in zip(expr.args, expr.arg_names):
  930. if name != 'strict':
  931. continue
  932. if isinstance(arg, NameExpr):
  933. if arg.fullname == 'builtins.True':
  934. return True
  935. elif arg.fullname == 'builtins.False':
  936. return False
  937. return None
  938. return None
  939. @staticmethod
  940. def get_alias_info(stmt: AssignmentStmt) -> tuple[str | None, bool]:
  941. """Returns a pair (alias, has_dynamic_alias), extracted from the declaration of the field defined in `stmt`.
  942. `has_dynamic_alias` is True if and only if an alias is provided, but not as a string literal.
  943. If `has_dynamic_alias` is True, `alias` will be None.
  944. """
  945. expr = stmt.rvalue
  946. if isinstance(expr, TempNode):
  947. # TempNode means annotation-only
  948. return None, False
  949. if not (
  950. isinstance(expr, CallExpr) and isinstance(expr.callee, RefExpr) and expr.callee.fullname == FIELD_FULLNAME
  951. ):
  952. # Assigned value is not a call to pydantic.fields.Field
  953. return None, False
  954. if 'validation_alias' in expr.arg_names:
  955. arg = expr.args[expr.arg_names.index('validation_alias')]
  956. elif 'alias' in expr.arg_names:
  957. arg = expr.args[expr.arg_names.index('alias')]
  958. else:
  959. return None, False
  960. if isinstance(arg, StrExpr):
  961. return arg.value, False
  962. else:
  963. return None, True
  964. @staticmethod
  965. def is_field_frozen(stmt: AssignmentStmt) -> bool:
  966. """Returns whether the field is frozen, extracted from the declaration of the field defined in `stmt`.
  967. Note that this is only whether the field was declared to be frozen in a `<field_name> = Field(frozen=True)`
  968. sense; this does not determine whether the field is frozen because the entire model is frozen; that is
  969. handled separately.
  970. """
  971. expr = stmt.rvalue
  972. if isinstance(expr, TempNode):
  973. # TempNode means annotation-only
  974. return False
  975. if not (
  976. isinstance(expr, CallExpr) and isinstance(expr.callee, RefExpr) and expr.callee.fullname == FIELD_FULLNAME
  977. ):
  978. # Assigned value is not a call to pydantic.fields.Field
  979. return False
  980. for i, arg_name in enumerate(expr.arg_names):
  981. if arg_name == 'frozen':
  982. arg = expr.args[i]
  983. return isinstance(arg, NameExpr) and arg.fullname == 'builtins.True'
  984. return False
  985. def get_field_arguments(
  986. self,
  987. fields: list[PydanticModelField],
  988. typed: bool,
  989. model_strict: bool,
  990. use_alias: bool,
  991. requires_dynamic_aliases: bool,
  992. is_settings: bool,
  993. is_root_model: bool,
  994. force_typevars_invariant: bool = False,
  995. ) -> list[Argument]:
  996. """Helper function used during the construction of the `__init__` and `model_construct` method signatures.
  997. Returns a list of mypy Argument instances for use in the generated signatures.
  998. """
  999. info = self._cls.info
  1000. arguments = [
  1001. field.to_argument(
  1002. info,
  1003. typed=typed,
  1004. model_strict=model_strict,
  1005. force_optional=requires_dynamic_aliases or is_settings,
  1006. use_alias=use_alias,
  1007. api=self._api,
  1008. force_typevars_invariant=force_typevars_invariant,
  1009. is_root_model_root=is_root_model and field.name == 'root',
  1010. )
  1011. for field in fields
  1012. if not (use_alias and field.has_dynamic_alias)
  1013. ]
  1014. return arguments
  1015. def should_init_forbid_extra(self, fields: list[PydanticModelField], config: ModelConfigData) -> bool:
  1016. """Indicates whether the generated `__init__` should get a `**kwargs` at the end of its signature.
  1017. We disallow arbitrary kwargs if the extra config setting is "forbid", or if the plugin config says to,
  1018. *unless* a required dynamic alias is present (since then we can't determine a valid signature).
  1019. """
  1020. if not (config.validate_by_name or config.populate_by_name):
  1021. if self.is_dynamic_alias_present(fields, bool(config.has_alias_generator)):
  1022. return False
  1023. if config.forbid_extra:
  1024. return True
  1025. return self.plugin_config.init_forbid_extra
  1026. @staticmethod
  1027. def is_dynamic_alias_present(fields: list[PydanticModelField], has_alias_generator: bool) -> bool:
  1028. """Returns whether any fields on the model have a "dynamic alias", i.e., an alias that cannot be
  1029. determined during static analysis.
  1030. """
  1031. for field in fields:
  1032. if field.has_dynamic_alias:
  1033. return True
  1034. if has_alias_generator:
  1035. for field in fields:
  1036. if field.alias is None:
  1037. return True
  1038. return False
  1039. class ChangeExplicitTypeOfAny(TypeTranslator):
  1040. """A type translator used to change type of Any's, if explicit."""
  1041. def __init__(self, type_of_any: int) -> None:
  1042. self._type_of_any = type_of_any
  1043. super().__init__()
  1044. def visit_any(self, t: AnyType) -> Type: # noqa: D102
  1045. if t.type_of_any == TypeOfAny.explicit:
  1046. return t.copy_modified(type_of_any=self._type_of_any)
  1047. else:
  1048. return t
  1049. class ModelConfigData:
  1050. """Pydantic mypy plugin model config class."""
  1051. def __init__(
  1052. self,
  1053. forbid_extra: bool | None = None,
  1054. frozen: bool | None = None,
  1055. from_attributes: bool | None = None,
  1056. populate_by_name: bool | None = None,
  1057. validate_by_alias: bool | None = None,
  1058. validate_by_name: bool | None = None,
  1059. has_alias_generator: bool | None = None,
  1060. strict: bool | None = None,
  1061. ):
  1062. self.forbid_extra = forbid_extra
  1063. self.frozen = frozen
  1064. self.from_attributes = from_attributes
  1065. self.populate_by_name = populate_by_name
  1066. self.validate_by_alias = validate_by_alias
  1067. self.validate_by_name = validate_by_name
  1068. self.has_alias_generator = has_alias_generator
  1069. self.strict = strict
  1070. def get_values_dict(self) -> dict[str, Any]:
  1071. """Returns a dict of Pydantic model config names to their values.
  1072. It includes the config if config value is not `None`.
  1073. """
  1074. return {k: v for k, v in self.__dict__.items() if v is not None}
  1075. def update(self, config: ModelConfigData | None) -> None:
  1076. """Update Pydantic model config values."""
  1077. if config is None:
  1078. return
  1079. for k, v in config.get_values_dict().items():
  1080. setattr(self, k, v)
  1081. def setdefault(self, key: str, value: Any) -> None:
  1082. """Set default value for Pydantic model config if config value is `None`."""
  1083. if getattr(self, key) is None:
  1084. setattr(self, key, value)
  1085. def is_root_model(info: TypeInfo) -> bool:
  1086. """Return whether the type info is a root model subclass (or the `RootModel` class itself)."""
  1087. return info.has_base(ROOT_MODEL_FULLNAME)
  1088. ERROR_ORM = ErrorCode('pydantic-orm', 'Invalid from_attributes call', 'Pydantic')
  1089. ERROR_CONFIG = ErrorCode('pydantic-config', 'Invalid config value', 'Pydantic')
  1090. ERROR_ALIAS = ErrorCode('pydantic-alias', 'Dynamic alias disallowed', 'Pydantic')
  1091. ERROR_UNEXPECTED = ErrorCode('pydantic-unexpected', 'Unexpected behavior', 'Pydantic')
  1092. ERROR_UNTYPED = ErrorCode('pydantic-field', 'Untyped field disallowed', 'Pydantic')
  1093. ERROR_FIELD_DEFAULTS = ErrorCode('pydantic-field', 'Invalid Field defaults', 'Pydantic')
  1094. ERROR_EXTRA_FIELD_ROOT_MODEL = ErrorCode('pydantic-field', 'Extra field on RootModel subclass', 'Pydantic')
  1095. def error_from_attributes(model_name: str, api: CheckerPluginInterface, context: Context) -> None:
  1096. """Emits an error when the model does not have `from_attributes=True`."""
  1097. api.fail(f'"{model_name}" does not have from_attributes=True', context, code=ERROR_ORM)
  1098. def error_invalid_config_value(name: str, api: SemanticAnalyzerPluginInterface, context: Context) -> None:
  1099. """Emits an error when the config value is invalid."""
  1100. api.fail(f'Invalid value for "Config.{name}"', context, code=ERROR_CONFIG)
  1101. def error_required_dynamic_aliases(api: SemanticAnalyzerPluginInterface, context: Context) -> None:
  1102. """Emits required dynamic aliases error.
  1103. This will be called when `warn_required_dynamic_aliases=True`.
  1104. """
  1105. api.fail('Required dynamic aliases disallowed', context, code=ERROR_ALIAS)
  1106. def error_unexpected_behavior(
  1107. detail: str, api: CheckerPluginInterface | SemanticAnalyzerPluginInterface, context: Context
  1108. ) -> None: # pragma: no cover
  1109. """Emits unexpected behavior error."""
  1110. # Can't think of a good way to test this, but I confirmed it renders as desired by adding to a non-error path
  1111. link = 'https://github.com/pydantic/pydantic/issues/new/choose'
  1112. full_message = f'The pydantic mypy plugin ran into unexpected behavior: {detail}\n'
  1113. full_message += f'Please consider reporting this bug at {link} so we can try to fix it!'
  1114. api.fail(full_message, context, code=ERROR_UNEXPECTED)
  1115. def error_untyped_fields(api: SemanticAnalyzerPluginInterface, context: Context) -> None:
  1116. """Emits an error when there is an untyped field in the model."""
  1117. api.fail('Untyped fields disallowed', context, code=ERROR_UNTYPED)
  1118. def error_extra_fields_on_root_model(api: CheckerPluginInterface, context: Context) -> None:
  1119. """Emits an error when there is more than just a root field defined for a subclass of RootModel."""
  1120. api.fail('Only `root` is allowed as a field of a `RootModel`', context, code=ERROR_EXTRA_FIELD_ROOT_MODEL)
  1121. def add_method(
  1122. api: SemanticAnalyzerPluginInterface | CheckerPluginInterface,
  1123. cls: ClassDef,
  1124. name: str,
  1125. args: list[Argument],
  1126. return_type: Type,
  1127. self_type: Type | None = None,
  1128. tvar_def: TypeVarType | None = None,
  1129. is_classmethod: bool = False,
  1130. ) -> None:
  1131. """Very closely related to `mypy.plugins.common.add_method_to_class`, with a few pydantic-specific changes."""
  1132. info = cls.info
  1133. # First remove any previously generated methods with the same name
  1134. # to avoid clashes and problems in the semantic analyzer.
  1135. if name in info.names:
  1136. sym = info.names[name]
  1137. if sym.plugin_generated and isinstance(sym.node, FuncDef):
  1138. cls.defs.body.remove(sym.node) # pragma: no cover
  1139. if isinstance(api, SemanticAnalyzerPluginInterface):
  1140. function_type = api.named_type('builtins.function')
  1141. else:
  1142. function_type = api.named_generic_type('builtins.function', [])
  1143. if is_classmethod:
  1144. self_type = self_type or TypeType(fill_typevars(info))
  1145. first = [Argument(Var('_cls'), self_type, None, ARG_POS, True)]
  1146. else:
  1147. self_type = self_type or fill_typevars(info)
  1148. # `self` is positional *ONLY* here, but this can't be expressed
  1149. # fully in the mypy internal API. ARG_POS is the closest we can get.
  1150. # Using ARG_POS will, however, give mypy errors if a `self` field
  1151. # is present on a model:
  1152. #
  1153. # Name "self" already defined (possibly by an import) [no-redef]
  1154. #
  1155. # As a workaround, we give this argument a name that will
  1156. # never conflict. By its positional nature, this name will not
  1157. # be used or exposed to users.
  1158. first = [Argument(Var('__pydantic_self__'), self_type, None, ARG_POS)]
  1159. args = first + args
  1160. arg_types, arg_names, arg_kinds = [], [], []
  1161. for arg in args:
  1162. assert arg.type_annotation, 'All arguments must be fully typed.'
  1163. arg_types.append(arg.type_annotation)
  1164. arg_names.append(arg.variable.name)
  1165. arg_kinds.append(arg.kind)
  1166. signature = CallableType(
  1167. arg_types, arg_kinds, arg_names, return_type, function_type, variables=[tvar_def] if tvar_def else None
  1168. )
  1169. func = FuncDef(name, args, Block([PassStmt()]))
  1170. func.info = info
  1171. func.type = set_callable_name(signature, func)
  1172. func.is_class = is_classmethod
  1173. func._fullname = info.fullname + '.' + name
  1174. func.line = info.line
  1175. # NOTE: we would like the plugin generated node to dominate, but we still
  1176. # need to keep any existing definitions so they get semantically analyzed.
  1177. if name in info.names:
  1178. # Get a nice unique name instead.
  1179. r_name = get_unique_redefinition_name(name, info.names)
  1180. info.names[r_name] = info.names[name]
  1181. # Add decorator for is_classmethod
  1182. # The dataclasses plugin claims this is unnecessary for classmethods, but not including it results in a
  1183. # signature incompatible with the superclass, which causes mypy errors to occur for every subclass of BaseModel.
  1184. if is_classmethod:
  1185. func.is_decorated = True
  1186. v = Var(name, func.type)
  1187. v.info = info
  1188. v._fullname = func._fullname
  1189. v.is_classmethod = True
  1190. dec = Decorator(func, [NameExpr('classmethod')], v)
  1191. dec.line = info.line
  1192. sym = SymbolTableNode(MDEF, dec)
  1193. else:
  1194. sym = SymbolTableNode(MDEF, func)
  1195. sym.plugin_generated = True
  1196. info.names[name] = sym
  1197. info.defn.defs.body.append(func)
  1198. def parse_toml(config_file: str) -> dict[str, Any] | None:
  1199. """Returns a dict of config keys to values.
  1200. It reads configs from toml file and returns `None` if the file is not a toml file.
  1201. """
  1202. if not config_file.endswith('.toml'):
  1203. return None
  1204. if sys.version_info >= (3, 11):
  1205. import tomllib as toml_
  1206. else:
  1207. try:
  1208. import tomli as toml_
  1209. except ImportError: # pragma: no cover
  1210. import warnings
  1211. warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.', stacklevel=2)
  1212. return None
  1213. with open(config_file, 'rb') as rf:
  1214. return toml_.load(rf)