decl_class.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. # ext/mypy/decl_class.py
  2. # Copyright (C) 2021-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. from __future__ import annotations
  8. from typing import List
  9. from typing import Optional
  10. from typing import Union
  11. from mypy.nodes import AssignmentStmt
  12. from mypy.nodes import CallExpr
  13. from mypy.nodes import ClassDef
  14. from mypy.nodes import Decorator
  15. from mypy.nodes import LambdaExpr
  16. from mypy.nodes import ListExpr
  17. from mypy.nodes import MemberExpr
  18. from mypy.nodes import NameExpr
  19. from mypy.nodes import PlaceholderNode
  20. from mypy.nodes import RefExpr
  21. from mypy.nodes import StrExpr
  22. from mypy.nodes import SymbolNode
  23. from mypy.nodes import SymbolTableNode
  24. from mypy.nodes import TempNode
  25. from mypy.nodes import TypeInfo
  26. from mypy.nodes import Var
  27. from mypy.plugin import SemanticAnalyzerPluginInterface
  28. from mypy.types import AnyType
  29. from mypy.types import CallableType
  30. from mypy.types import get_proper_type
  31. from mypy.types import Instance
  32. from mypy.types import NoneType
  33. from mypy.types import ProperType
  34. from mypy.types import Type
  35. from mypy.types import TypeOfAny
  36. from mypy.types import UnboundType
  37. from mypy.types import UnionType
  38. from . import apply
  39. from . import infer
  40. from . import names
  41. from . import util
  42. def scan_declarative_assignments_and_apply_types(
  43. cls: ClassDef,
  44. api: SemanticAnalyzerPluginInterface,
  45. is_mixin_scan: bool = False,
  46. ) -> Optional[List[util.SQLAlchemyAttribute]]:
  47. info = util.info_for_cls(cls, api)
  48. if info is None:
  49. # this can occur during cached passes
  50. return None
  51. elif cls.fullname.startswith("builtins"):
  52. return None
  53. mapped_attributes: Optional[List[util.SQLAlchemyAttribute]] = (
  54. util.get_mapped_attributes(info, api)
  55. )
  56. # used by assign.add_additional_orm_attributes among others
  57. util.establish_as_sqlalchemy(info)
  58. if mapped_attributes is not None:
  59. # ensure that a class that's mapped is always picked up by
  60. # its mapped() decorator or declarative metaclass before
  61. # it would be detected as an unmapped mixin class
  62. if not is_mixin_scan:
  63. # mypy can call us more than once. it then *may* have reset the
  64. # left hand side of everything, but not the right that we removed,
  65. # removing our ability to re-scan. but we have the types
  66. # here, so lets re-apply them, or if we have an UnboundType,
  67. # we can re-scan
  68. apply.re_apply_declarative_assignments(cls, api, mapped_attributes)
  69. return mapped_attributes
  70. mapped_attributes = []
  71. if not cls.defs.body:
  72. # when we get a mixin class from another file, the body is
  73. # empty (!) but the names are in the symbol table. so use that.
  74. for sym_name, sym in info.names.items():
  75. _scan_symbol_table_entry(
  76. cls, api, sym_name, sym, mapped_attributes
  77. )
  78. else:
  79. for stmt in util.flatten_typechecking(cls.defs.body):
  80. if isinstance(stmt, AssignmentStmt):
  81. _scan_declarative_assignment_stmt(
  82. cls, api, stmt, mapped_attributes
  83. )
  84. elif isinstance(stmt, Decorator):
  85. _scan_declarative_decorator_stmt(
  86. cls, api, stmt, mapped_attributes
  87. )
  88. _scan_for_mapped_bases(cls, api)
  89. if not is_mixin_scan:
  90. apply.add_additional_orm_attributes(cls, api, mapped_attributes)
  91. util.set_mapped_attributes(info, mapped_attributes)
  92. return mapped_attributes
  93. def _scan_symbol_table_entry(
  94. cls: ClassDef,
  95. api: SemanticAnalyzerPluginInterface,
  96. name: str,
  97. value: SymbolTableNode,
  98. attributes: List[util.SQLAlchemyAttribute],
  99. ) -> None:
  100. """Extract mapping information from a SymbolTableNode that's in the
  101. type.names dictionary.
  102. """
  103. value_type = get_proper_type(value.type)
  104. if not isinstance(value_type, Instance):
  105. return
  106. left_hand_explicit_type = None
  107. type_id = names.type_id_for_named_node(value_type.type)
  108. # type_id = names._type_id_for_unbound_type(value.type.type, cls, api)
  109. err = False
  110. # TODO: this is nearly the same logic as that of
  111. # _scan_declarative_decorator_stmt, likely can be merged
  112. if type_id in {
  113. names.MAPPED,
  114. names.RELATIONSHIP,
  115. names.COMPOSITE_PROPERTY,
  116. names.MAPPER_PROPERTY,
  117. names.SYNONYM_PROPERTY,
  118. names.COLUMN_PROPERTY,
  119. }:
  120. if value_type.args:
  121. left_hand_explicit_type = get_proper_type(value_type.args[0])
  122. else:
  123. err = True
  124. elif type_id is names.COLUMN:
  125. if not value_type.args:
  126. err = True
  127. else:
  128. typeengine_arg: Union[ProperType, TypeInfo] = get_proper_type(
  129. value_type.args[0]
  130. )
  131. if isinstance(typeengine_arg, Instance):
  132. typeengine_arg = typeengine_arg.type
  133. if isinstance(typeengine_arg, (UnboundType, TypeInfo)):
  134. sym = api.lookup_qualified(typeengine_arg.name, typeengine_arg)
  135. if sym is not None and isinstance(sym.node, TypeInfo):
  136. if names.has_base_type_id(sym.node, names.TYPEENGINE):
  137. left_hand_explicit_type = UnionType(
  138. [
  139. infer.extract_python_type_from_typeengine(
  140. api, sym.node, []
  141. ),
  142. NoneType(),
  143. ]
  144. )
  145. else:
  146. util.fail(
  147. api,
  148. "Column type should be a TypeEngine "
  149. "subclass not '{}'".format(sym.node.fullname),
  150. value_type,
  151. )
  152. if err:
  153. msg = (
  154. "Can't infer type from attribute {} on class {}. "
  155. "please specify a return type from this function that is "
  156. "one of: Mapped[<python type>], relationship[<target class>], "
  157. "Column[<TypeEngine>], MapperProperty[<python type>]"
  158. )
  159. util.fail(api, msg.format(name, cls.name), cls)
  160. left_hand_explicit_type = AnyType(TypeOfAny.special_form)
  161. if left_hand_explicit_type is not None:
  162. assert value.node is not None
  163. attributes.append(
  164. util.SQLAlchemyAttribute(
  165. name=name,
  166. line=value.node.line,
  167. column=value.node.column,
  168. typ=left_hand_explicit_type,
  169. info=cls.info,
  170. )
  171. )
  172. def _scan_declarative_decorator_stmt(
  173. cls: ClassDef,
  174. api: SemanticAnalyzerPluginInterface,
  175. stmt: Decorator,
  176. attributes: List[util.SQLAlchemyAttribute],
  177. ) -> None:
  178. """Extract mapping information from a @declared_attr in a declarative
  179. class.
  180. E.g.::
  181. @reg.mapped
  182. class MyClass:
  183. # ...
  184. @declared_attr
  185. def updated_at(cls) -> Column[DateTime]:
  186. return Column(DateTime)
  187. Will resolve in mypy as::
  188. @reg.mapped
  189. class MyClass:
  190. # ...
  191. updated_at: Mapped[Optional[datetime.datetime]]
  192. """
  193. for dec in stmt.decorators:
  194. if (
  195. isinstance(dec, (NameExpr, MemberExpr, SymbolNode))
  196. and names.type_id_for_named_node(dec) is names.DECLARED_ATTR
  197. ):
  198. break
  199. else:
  200. return
  201. dec_index = cls.defs.body.index(stmt)
  202. left_hand_explicit_type: Optional[ProperType] = None
  203. if util.name_is_dunder(stmt.name):
  204. # for dunder names like __table_args__, __tablename__,
  205. # __mapper_args__ etc., rewrite these as simple assignment
  206. # statements; otherwise mypy doesn't like if the decorated
  207. # function has an annotation like ``cls: Type[Foo]`` because
  208. # it isn't @classmethod
  209. any_ = AnyType(TypeOfAny.special_form)
  210. left_node = NameExpr(stmt.var.name)
  211. left_node.node = stmt.var
  212. new_stmt = AssignmentStmt([left_node], TempNode(any_))
  213. new_stmt.type = left_node.node.type
  214. cls.defs.body[dec_index] = new_stmt
  215. return
  216. elif isinstance(stmt.func.type, CallableType):
  217. func_type = stmt.func.type.ret_type
  218. if isinstance(func_type, UnboundType):
  219. type_id = names.type_id_for_unbound_type(func_type, cls, api)
  220. else:
  221. # this does not seem to occur unless the type argument is
  222. # incorrect
  223. return
  224. if (
  225. type_id
  226. in {
  227. names.MAPPED,
  228. names.RELATIONSHIP,
  229. names.COMPOSITE_PROPERTY,
  230. names.MAPPER_PROPERTY,
  231. names.SYNONYM_PROPERTY,
  232. names.COLUMN_PROPERTY,
  233. }
  234. and func_type.args
  235. ):
  236. left_hand_explicit_type = get_proper_type(func_type.args[0])
  237. elif type_id is names.COLUMN and func_type.args:
  238. typeengine_arg = func_type.args[0]
  239. if isinstance(typeengine_arg, UnboundType):
  240. sym = api.lookup_qualified(typeengine_arg.name, typeengine_arg)
  241. if sym is not None and isinstance(sym.node, TypeInfo):
  242. if names.has_base_type_id(sym.node, names.TYPEENGINE):
  243. left_hand_explicit_type = UnionType(
  244. [
  245. infer.extract_python_type_from_typeengine(
  246. api, sym.node, []
  247. ),
  248. NoneType(),
  249. ]
  250. )
  251. else:
  252. util.fail(
  253. api,
  254. "Column type should be a TypeEngine "
  255. "subclass not '{}'".format(sym.node.fullname),
  256. func_type,
  257. )
  258. if left_hand_explicit_type is None:
  259. # no type on the decorated function. our option here is to
  260. # dig into the function body and get the return type, but they
  261. # should just have an annotation.
  262. msg = (
  263. "Can't infer type from @declared_attr on function '{}'; "
  264. "please specify a return type from this function that is "
  265. "one of: Mapped[<python type>], relationship[<target class>], "
  266. "Column[<TypeEngine>], MapperProperty[<python type>]"
  267. )
  268. util.fail(api, msg.format(stmt.var.name), stmt)
  269. left_hand_explicit_type = AnyType(TypeOfAny.special_form)
  270. left_node = NameExpr(stmt.var.name)
  271. left_node.node = stmt.var
  272. # totally feeling around in the dark here as I don't totally understand
  273. # the significance of UnboundType. It seems to be something that is
  274. # not going to do what's expected when it is applied as the type of
  275. # an AssignmentStatement. So do a feeling-around-in-the-dark version
  276. # of converting it to the regular Instance/TypeInfo/UnionType structures
  277. # we see everywhere else.
  278. if isinstance(left_hand_explicit_type, UnboundType):
  279. left_hand_explicit_type = get_proper_type(
  280. util.unbound_to_instance(api, left_hand_explicit_type)
  281. )
  282. left_node.node.type = api.named_type(
  283. names.NAMED_TYPE_SQLA_MAPPED, [left_hand_explicit_type]
  284. )
  285. # this will ignore the rvalue entirely
  286. # rvalue = TempNode(AnyType(TypeOfAny.special_form))
  287. # rewrite the node as:
  288. # <attr> : Mapped[<typ>] =
  289. # _sa_Mapped._empty_constructor(lambda: <function body>)
  290. # the function body is maintained so it gets type checked internally
  291. rvalue = names.expr_to_mapped_constructor(
  292. LambdaExpr(stmt.func.arguments, stmt.func.body)
  293. )
  294. new_stmt = AssignmentStmt([left_node], rvalue)
  295. new_stmt.type = left_node.node.type
  296. attributes.append(
  297. util.SQLAlchemyAttribute(
  298. name=left_node.name,
  299. line=stmt.line,
  300. column=stmt.column,
  301. typ=left_hand_explicit_type,
  302. info=cls.info,
  303. )
  304. )
  305. cls.defs.body[dec_index] = new_stmt
  306. def _scan_declarative_assignment_stmt(
  307. cls: ClassDef,
  308. api: SemanticAnalyzerPluginInterface,
  309. stmt: AssignmentStmt,
  310. attributes: List[util.SQLAlchemyAttribute],
  311. ) -> None:
  312. """Extract mapping information from an assignment statement in a
  313. declarative class.
  314. """
  315. lvalue = stmt.lvalues[0]
  316. if not isinstance(lvalue, NameExpr):
  317. return
  318. sym = cls.info.names.get(lvalue.name)
  319. # this establishes that semantic analysis has taken place, which
  320. # means the nodes are populated and we are called from an appropriate
  321. # hook.
  322. assert sym is not None
  323. node = sym.node
  324. if isinstance(node, PlaceholderNode):
  325. return
  326. assert node is lvalue.node
  327. assert isinstance(node, Var)
  328. if node.name == "__abstract__":
  329. if api.parse_bool(stmt.rvalue) is True:
  330. util.set_is_base(cls.info)
  331. return
  332. elif node.name == "__tablename__":
  333. util.set_has_table(cls.info)
  334. elif node.name.startswith("__"):
  335. return
  336. elif node.name == "_mypy_mapped_attrs":
  337. if not isinstance(stmt.rvalue, ListExpr):
  338. util.fail(api, "_mypy_mapped_attrs is expected to be a list", stmt)
  339. else:
  340. for item in stmt.rvalue.items:
  341. if isinstance(item, (NameExpr, StrExpr)):
  342. apply.apply_mypy_mapped_attr(cls, api, item, attributes)
  343. left_hand_mapped_type: Optional[Type] = None
  344. left_hand_explicit_type: Optional[ProperType] = None
  345. if node.is_inferred or node.type is None:
  346. if isinstance(stmt.type, UnboundType):
  347. # look for an explicit Mapped[] type annotation on the left
  348. # side with nothing on the right
  349. # print(stmt.type)
  350. # Mapped?[Optional?[A?]]
  351. left_hand_explicit_type = stmt.type
  352. if stmt.type.name == "Mapped":
  353. mapped_sym = api.lookup_qualified("Mapped", cls)
  354. if (
  355. mapped_sym is not None
  356. and mapped_sym.node is not None
  357. and names.type_id_for_named_node(mapped_sym.node)
  358. is names.MAPPED
  359. ):
  360. left_hand_explicit_type = get_proper_type(
  361. stmt.type.args[0]
  362. )
  363. left_hand_mapped_type = stmt.type
  364. # TODO: do we need to convert from unbound for this case?
  365. # left_hand_explicit_type = util._unbound_to_instance(
  366. # api, left_hand_explicit_type
  367. # )
  368. else:
  369. node_type = get_proper_type(node.type)
  370. if (
  371. isinstance(node_type, Instance)
  372. and names.type_id_for_named_node(node_type.type) is names.MAPPED
  373. ):
  374. # print(node.type)
  375. # sqlalchemy.orm.attributes.Mapped[<python type>]
  376. left_hand_explicit_type = get_proper_type(node_type.args[0])
  377. left_hand_mapped_type = node_type
  378. else:
  379. # print(node.type)
  380. # <python type>
  381. left_hand_explicit_type = node_type
  382. left_hand_mapped_type = None
  383. if isinstance(stmt.rvalue, TempNode) and left_hand_mapped_type is not None:
  384. # annotation without assignment and Mapped is present
  385. # as type annotation
  386. # equivalent to using _infer_type_from_left_hand_type_only.
  387. python_type_for_type = left_hand_explicit_type
  388. elif isinstance(stmt.rvalue, CallExpr) and isinstance(
  389. stmt.rvalue.callee, RefExpr
  390. ):
  391. python_type_for_type = infer.infer_type_from_right_hand_nameexpr(
  392. api, stmt, node, left_hand_explicit_type, stmt.rvalue.callee
  393. )
  394. if python_type_for_type is None:
  395. return
  396. else:
  397. return
  398. assert python_type_for_type is not None
  399. attributes.append(
  400. util.SQLAlchemyAttribute(
  401. name=node.name,
  402. line=stmt.line,
  403. column=stmt.column,
  404. typ=python_type_for_type,
  405. info=cls.info,
  406. )
  407. )
  408. apply.apply_type_to_mapped_statement(
  409. api,
  410. stmt,
  411. lvalue,
  412. left_hand_explicit_type,
  413. python_type_for_type,
  414. )
  415. def _scan_for_mapped_bases(
  416. cls: ClassDef,
  417. api: SemanticAnalyzerPluginInterface,
  418. ) -> None:
  419. """Given a class, iterate through its superclass hierarchy to find
  420. all other classes that are considered as ORM-significant.
  421. Locates non-mapped mixins and scans them for mapped attributes to be
  422. applied to subclasses.
  423. """
  424. info = util.info_for_cls(cls, api)
  425. if info is None:
  426. return
  427. for base_info in info.mro[1:-1]:
  428. if base_info.fullname.startswith("builtins"):
  429. continue
  430. # scan each base for mapped attributes. if they are not already
  431. # scanned (but have all their type info), that means they are unmapped
  432. # mixins
  433. scan_declarative_assignments_and_apply_types(
  434. base_info.defn, api, is_mixin_scan=True
  435. )