summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCyril Chapellier <tchapi@users.noreply.github.com>2022-06-30 14:59:38 +0200
committerFederico Caselli <cfederico87@gmail.com>2022-07-02 12:33:58 +0200
commitccfec76b829e7eccd9e08d7f757da3590497f197 (patch)
treec939c1c0a1ea9e0e379c4a8d4812a4b00fbddfe3
parentb35bbdb13be9a359fc3cfae00e2b6fdca63dea29 (diff)
downloadsqlalchemy-ccfec76b829e7eccd9e08d7f757da3590497f197.tar.gz
Support lambda expression in mypy plugin
Avoid `error: INTERNAL ERROR` when the default is a lambda Fixes: #8196 Change-Id: I7346c693519b024c56156db6f4ffc9a45bb748d3
-rw-r--r--doc/build/changelog/unreleased_14/8196.rst7
-rw-r--r--lib/sqlalchemy/ext/mypy/infer.py5
-rw-r--r--test/ext/mypy/plugin_files/lambda_default.py11
3 files changed, 23 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_14/8196.rst b/doc/build/changelog/unreleased_14/8196.rst
new file mode 100644
index 000000000..d5afbb8f7
--- /dev/null
+++ b/doc/build/changelog/unreleased_14/8196.rst
@@ -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.
+
diff --git a/lib/sqlalchemy/ext/mypy/infer.py b/lib/sqlalchemy/ext/mypy/infer.py
index 08035d74c..479be40c3 100644
--- a/lib/sqlalchemy/ext/mypy/infer.py
+++ b/lib/sqlalchemy/ext/mypy/infer.py
@@ -16,6 +16,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
@@ -423,6 +424,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/plugin_files/lambda_default.py b/test/ext/mypy/plugin_files/lambda_default.py
new file mode 100644
index 000000000..a1019f0d0
--- /dev/null
+++ b/test/ext/mypy/plugin_files/lambda_default.py
@@ -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)