mypy.py 51 KB

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