Skip to content

Commit

Permalink
Add and update mapped_column typing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acanizares committed Mar 12, 2024
1 parent 507d66c commit b321b8c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
3 changes: 2 additions & 1 deletion test/typing/plain_files/orm/issue_9340.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from typing import Sequence
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -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/")
Expand Down
83 changes: 77 additions & 6 deletions test/typing/plain_files/orm/mapped_column.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from typing import Optional

from sqlalchemy import ForeignKey
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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()
5 changes: 0 additions & 5 deletions test/typing/plain_files/orm/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit b321b8c

Please sign in to comment.