]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
check for MemberExpr looking for column argument
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Mar 2021 19:26:05 +0000 (15:26 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Mar 2021 20:13:10 +0000 (16:13 -0400)
Fixed issue in MyPy extension which crashed on detecting the type of a
:class:`.Column` if the type were given with a module prefix like
``sa.Integer()``.

Fixes: sqlalchemy/sqlalchemy2-stubs/#2
Change-Id: I71f53a6ced501ae144e28ce255cf3f50ea2b2e84

doc/build/changelog/unreleased_14/stubs_2.rst [new file with mode: 0644]
lib/sqlalchemy/ext/mypy/decl_class.py
test/ext/mypy/files/sa_module_prefix.py [new file with mode: 0644]

diff --git a/doc/build/changelog/unreleased_14/stubs_2.rst b/doc/build/changelog/unreleased_14/stubs_2.rst
new file mode 100644 (file)
index 0000000..20689cb
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, mypy
+    :tickets: sqlalchemy/sqlalchemy2-stubs/2
+
+    Fixed issue in MyPy extension which crashed on detecting the type of a
+    :class:`.Column` if the type were given with a module prefix like
+    ``sa.Integer()``.
+
index f5215ca1cf8e474755a5a75312873b174745256f..7a0c251c3b3ace0453fd5f2127ba20573d5227be 100644 (file)
@@ -693,7 +693,7 @@ def _infer_type_from_decl_column(
             # x = Column(String(50))
             callee = column_arg.callee
             break
-        elif isinstance(column_arg, nodes.NameExpr):
+        elif isinstance(column_arg, (nodes.NameExpr, nodes.MemberExpr)):
             if isinstance(column_arg.node, TypeInfo):
                 # x = Column(String)
                 callee = column_arg
diff --git a/test/ext/mypy/files/sa_module_prefix.py b/test/ext/mypy/files/sa_module_prefix.py
new file mode 100644 (file)
index 0000000..a37ae6b
--- /dev/null
@@ -0,0 +1,33 @@
+from typing import List
+from typing import Optional
+
+import sqlalchemy as sa
+from sqlalchemy import orm as saorm
+
+
+Base = saorm.declarative_base()
+
+
+class B(Base):
+    __tablename__ = "b"
+    id = sa.Column(sa.Integer, primary_key=True)
+    a_id: int = sa.Column(sa.ForeignKey("a.id"))
+    data = sa.Column(sa.String)
+
+    a: Optional["A"] = saorm.relationship("A", back_populates="bs")
+
+
+class A(Base):
+    __tablename__ = "a"
+
+    id = sa.Column(sa.Integer, primary_key=True)
+    data = sa.Column(sa.String)
+    bs = saorm.relationship(B, uselist=True, back_populates="a")
+
+
+a1 = A(bs=[B(data="b"), B(data="b")])
+
+x: List[B] = a1.bs
+
+
+b1 = B(a=A())