diff --git a/test/typing/plain_files/orm/issue_9340.py b/test/typing/plain_files/orm/issue_9340.py index 6ccd2eed314..3706ffe0600 100644 --- a/test/typing/plain_files/orm/issue_9340.py +++ b/test/typing/plain_files/orm/issue_9340.py @@ -1,3 +1,4 @@ +from typing import Optional from typing import Sequence from typing import TYPE_CHECKING @@ -28,7 +29,7 @@ class UserComment(Message): __mapper_args__ = { "polymorphic_identity": "user_comment", } - username: Mapped[str] = mapped_column(nullable=True) + username: Mapped[Optional[str]] = mapped_column(nullable=True) engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/") diff --git a/test/typing/plain_files/orm/mapped_column.py b/test/typing/plain_files/orm/mapped_column.py index 26f5722a6fc..cf8e09e4e44 100644 --- a/test/typing/plain_files/orm/mapped_column.py +++ b/test/typing/plain_files/orm/mapped_column.py @@ -1,3 +1,4 @@ +import typing from typing import Optional from sqlalchemy import ForeignKey @@ -44,7 +45,7 @@ class X(Base): b: Mapped[Optional[str]] = mapped_column() # this can't be detected because we don't know the type - c: Mapped[str] = mapped_column(nullable=True) + c: Mapped[Optional[str]] = mapped_column(nullable=True) d: Mapped[str] = mapped_column(nullable=False) e: Mapped[Optional[str]] = mapped_column(ForeignKey(c), nullable=True) @@ -58,8 +59,6 @@ class X(Base): # this probably is wrong. however at the moment it seems better to # decouple the right hand arguments from declaring things about the # left side since it mostly doesn't work in any case. - i: Mapped[str] = mapped_column(String, nullable=True) - j: Mapped[str] = mapped_column(String, nullable=False) k: Mapped[Optional[str]] = mapped_column(String, nullable=True) @@ -69,7 +68,6 @@ class X(Base): a_name: Mapped[str] = mapped_column("a_name") b_name: Mapped[Optional[str]] = mapped_column("b_name") - c_name: Mapped[str] = mapped_column("c_name", nullable=True) d_name: Mapped[str] = mapped_column("d_name", nullable=False) e_name: Mapped[Optional[str]] = mapped_column("e_name", nullable=True) @@ -79,8 +77,6 @@ class X(Base): g_name: Mapped[str] = mapped_column("g_name", String) h_name: Mapped[Optional[str]] = mapped_column("h_name", String) - i_name: Mapped[str] = mapped_column("i_name", String, nullable=True) - j_name: Mapped[str] = mapped_column("j_name", String, nullable=False) k_name: Mapped[Optional[str]] = mapped_column( @@ -94,3 +90,78 @@ class X(Base): ) __table_args__ = (UniqueConstraint(a, b, name="uq1"), Index("ix1", c, d)) + + +if typing.TYPE_CHECKING: + # EXPECTED_RE_TYPE: sqlalchemy.orm.properties.MappedColumn\[builtins.int\] + reveal_type(mapped_column(Integer)) + + # EXPECTED_RE_TYPE: sqlalchemy.orm.properties.MappedColumn\[Union\[builtins.int, None\]\] + reveal_type(mapped_column(Integer, nullable=True)) + + # EXPECTED_RE_TYPE: sqlalchemy.orm.properties.MappedColumn\[builtins.int\] + reveal_type(mapped_column(Integer, default=7)) + + # EXPECTED_MYPY_RE: Argument 1 to "mapped_column" has incompatible type.* + a_err: Mapped[str] = mapped_column(Integer) + + # EXPECTED_MYPY_RE: Argument 2 to "mapped_column" has incompatible type.* + a_err_name: Mapped[str] = mapped_column("a", Integer) + + # EXPECTED_MYPY_RE: Argument "default" to "mapped_column" has incompatible type "None".* + b_err: Mapped[int] = mapped_column(Integer, default=None) + + # EXPECTED_MYPY_RE: Argument "default" to "mapped_column" has incompatible type "None".* + b_err_name: Mapped[int] = mapped_column("b", Integer, default=None) + + # EXPECTED_MYPY_RE: Argument "default" to "mapped_column" has incompatible type "None".* + c_err: Mapped[int] = mapped_column(default=None) + + # EXPECTED_MYPY_RE: Argument "default" to "mapped_column" has incompatible type "None".* + c_err_name: Mapped[int] = mapped_column("c", default=None) + + # EXPECTED_MYPY_RE: Incompatible types in assignment.* + d_err: Mapped[int] = mapped_column(Integer, nullable=True) + + # EXPECTED_MYPY_RE: Incompatible types in assignment.* + d_err_name: Mapped[int] = mapped_column("d", Integer, nullable=True) + + # EXPECTED_MYPY_RE: Incompatible types in assignment.* + e_err: Mapped[int] = mapped_column(nullable=True) + + # EXPECTED_MYPY_RE: Incompatible types in assignment.* + e_err_name: Mapped[int] = mapped_column("e", nullable=True) + + # EXPECTED_MYPY_RE: Argument "default" to "mapped_column" has incompatible type.* + f_err: Mapped[int] = mapped_column(default="a") + + # EXPECTED_MYPY_RE: Argument "default" to "mapped_column" has incompatible type.* + f_err_name: Mapped[int] = mapped_column("f", default="a") + + # All of these are fine + x1: Mapped[str] = mapped_column(String, default="a", nullable=False) + x2: Mapped[str] = mapped_column(String, default="a") + x3: Mapped[str] = mapped_column(default="a", nullable=False) + x4: Mapped[str] = mapped_column(String, nullable=False) + x5: Mapped[str] = mapped_column(String) + x6: Mapped[str] = mapped_column(default="a") + x7: Mapped[str] = mapped_column(nullable=False) + x8: Mapped[str] = mapped_column() + + y1: Mapped[Optional[int]] = mapped_column(Integer, default=None, nullable=True) + y2: Mapped[Optional[int]] = mapped_column(Integer, default=None) + y3: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) + y4: Mapped[Optional[int]] = mapped_column(default=None, nullable=True) + y5: Mapped[Optional[int]] = mapped_column(default=None) + y6: Mapped[Optional[int]] = mapped_column(Integer) + y7: Mapped[Optional[int]] = mapped_column(nullable=True) + y8: Mapped[Optional[int]] = mapped_column() + + z1: Mapped[int] = mapped_column(Integer, default=7, nullable=False) + z2: Mapped[int] = mapped_column(Integer, default=7) + z3: Mapped[int] = mapped_column(default=7, nullable=False) + z4: Mapped[int] = mapped_column(Integer, nullable=False) + z5: Mapped[int] = mapped_column(Integer) + z6: Mapped[int] = mapped_column(default=7) + z7: Mapped[int] = mapped_column(nullable=False) + z8: Mapped[int] = mapped_column() diff --git a/test/typing/plain_files/orm/relationship.py b/test/typing/plain_files/orm/relationship.py index 6bfe19cc4e8..8757821e24c 100644 --- a/test/typing/plain_files/orm/relationship.py +++ b/test/typing/plain_files/orm/relationship.py @@ -35,11 +35,6 @@ class User(Base): id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column() - # this currently doesnt generate an error. not sure how to get the - # overloads to hit this one, nor am i sure i really want to do that - # anyway - name_this_works_atm: Mapped[str] = mapped_column(nullable=True) - extra: Mapped[Optional[str]] = mapped_column() extra_name: Mapped[Optional[str]] = mapped_column("extra_name")