diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-06-14 09:31:09 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-06-14 10:58:31 -0400 |
commit | 54f5def028d8f46ead37e8046d2aea3bb9953ebc (patch) | |
tree | 481594f1d9ffe6cce70636cdfffab48be7e21916 /test/ext/mypy/plain_files/composite_dc.py | |
parent | 4010a61af7b88e0d3b18bcd560a465269384f250 (diff) | |
download | sqlalchemy-54f5def028d8f46ead37e8046d2aea3bb9953ebc.tar.gz |
typing adjustments for composites
* if dataclass isn't used, columns have to be named
* _CompositeClassProto is not useful as dataclasses have no
methods / bases we can use, so composite is against Any
* Adjust session.get() feature to work w/ dataclass composites
Change-Id: Icc606cc76871c738dc794ea4555fca8a1ab0e0fd
Diffstat (limited to 'test/ext/mypy/plain_files/composite_dc.py')
-rw-r--r-- | test/ext/mypy/plain_files/composite_dc.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/test/ext/mypy/plain_files/composite_dc.py b/test/ext/mypy/plain_files/composite_dc.py new file mode 100644 index 000000000..fa1b16a2a --- /dev/null +++ b/test/ext/mypy/plain_files/composite_dc.py @@ -0,0 +1,51 @@ +import dataclasses + +from sqlalchemy import select +from sqlalchemy.orm import composite +from sqlalchemy.orm import DeclarativeBase +from sqlalchemy.orm import Mapped +from sqlalchemy.orm import mapped_column + + +class Base(DeclarativeBase): + pass + + +@dataclasses.dataclass +class Point: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + +class Vertex(Base): + __tablename__ = "vertices" + + id: Mapped[int] = mapped_column(primary_key=True) + x1: Mapped[int] + y1: Mapped[int] + x2: Mapped[int] + y2: Mapped[int] + + # inferred from right hand side + start = composite(Point, "x1", "y1") + + # taken from left hand side + end: Mapped[Point] = composite(Point, "x2", "y2") + + +v1 = Vertex(start=Point(3, 4), end=Point(5, 6)) + +stmt = select(Vertex).where(Vertex.start.in_([Point(3, 4)])) + +# EXPECTED_TYPE: Select[Tuple[Vertex]] +reveal_type(stmt) + +# EXPECTED_TYPE: composite.Point +reveal_type(v1.start) + +# EXPECTED_TYPE: composite.Point +reveal_type(v1.end) + +# EXPECTED_TYPE: int +reveal_type(v1.end.y) |