From: Cyril Chapellier Date: Thu, 30 Jun 2022 12:59:38 +0000 (+0200) Subject: Support lambda expression in mypy plugin X-Git-Tag: rel_1_4_40~35^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80580991ecebeb173df1e8dcf716508309f60883;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Support lambda expression in mypy plugin Avoid `error: INTERNAL ERROR` when the default is a lambda Fixes: #8196 Change-Id: I7346c693519b024c56156db6f4ffc9a45bb748d3 (cherry picked from commit 472dc0735c4d9385b05e5e2088dad9f507a59a63) --- diff --git a/doc/build/changelog/unreleased_14/8196.rst b/doc/build/changelog/unreleased_14/8196.rst new file mode 100644 index 0000000000..d5afbb8f7a --- /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 3cd946e04d..f88a960bd2 100644 --- a/lib/sqlalchemy/ext/mypy/infer.py +++ b/lib/sqlalchemy/ext/mypy/infer.py @@ -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 index 0000000000..a1019f0d02 --- /dev/null +++ b/test/ext/mypy/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)