]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support lambda expression in mypy plugin
authorCyril Chapellier <tchapi@users.noreply.github.com>
Thu, 30 Jun 2022 12:59:38 +0000 (14:59 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Sat, 2 Jul 2022 10:34:13 +0000 (12:34 +0200)
Avoid `error: INTERNAL ERROR` when the default is a lambda

Fixes: #8196
Change-Id: I7346c693519b024c56156db6f4ffc9a45bb748d3
(cherry picked from commit 472dc0735c4d9385b05e5e2088dad9f507a59a63)

doc/build/changelog/unreleased_14/8196.rst [new file with mode: 0644]
lib/sqlalchemy/ext/mypy/infer.py
test/ext/mypy/files/lambda_default.py [new file with mode: 0644]

diff --git a/doc/build/changelog/unreleased_14/8196.rst b/doc/build/changelog/unreleased_14/8196.rst
new file mode 100644 (file)
index 0000000..d5afbb8
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, mypy
+    :tickets: 8196
+
+    Fixed a crash of the mypy plugin when using a lambda as a Column
+    default. Pull request curtesy of tchapi.
+
index 3cd946e04d03747718d352e141e91f02967a9237..f88a960bd2e1cdaab9185039bec8dd162962f682 100644 (file)
@@ -14,6 +14,7 @@ from mypy.nodes import AssignmentStmt
 from mypy.nodes import CallExpr
 from mypy.nodes import Expression
 from mypy.nodes import FuncDef
+from mypy.nodes import LambdaExpr
 from mypy.nodes import MemberExpr
 from mypy.nodes import NameExpr
 from mypy.nodes import RefExpr
@@ -387,6 +388,10 @@ def _infer_type_from_decl_column(
         elif isinstance(column_arg, (StrExpr,)):
             # x = Column("name", String), go to next argument
             continue
+        elif isinstance(column_arg, (LambdaExpr,)):
+            # x = Column("name", String, default=lambda: uuid.uuid4())
+            # go to next argument
+            continue
         else:
             assert False
 
diff --git a/test/ext/mypy/files/lambda_default.py b/test/ext/mypy/files/lambda_default.py
new file mode 100644 (file)
index 0000000..a1019f0
--- /dev/null
@@ -0,0 +1,11 @@
+import uuid
+
+from sqlalchemy import Column
+from sqlalchemy import String
+from sqlalchemy.orm import declarative_base
+
+Base = declarative_base()
+
+
+class MyClass(Base):
+    id = Column(String, default=lambda: uuid.uuid4(), primary_key=True)