]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
✅ Add test that runs select with 3 or 4 arguments (#1590)
authorSofie Van Landeghem <svlandeg@users.noreply.github.com>
Wed, 8 Oct 2025 10:53:29 +0000 (12:53 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Oct 2025 10:53:29 +0000 (12:53 +0200)
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
scripts/lint.sh
tests/test_select_typing.py [new file with mode: 0644]

index 7fab52df576c62280c934abd02b2ba08cb857030..e4a7b5bea72b3d600c8e2d8ea595f8b13cafa5a5 100755 (executable)
@@ -4,5 +4,6 @@ set -e
 set -x
 
 mypy sqlmodel
+mypy tests/test_select_typing.py
 ruff check sqlmodel tests docs_src scripts
 ruff format sqlmodel tests docs_src scripts --check
diff --git a/tests/test_select_typing.py b/tests/test_select_typing.py
new file mode 100644 (file)
index 0000000..8f1d030
--- /dev/null
@@ -0,0 +1,64 @@
+from typing import Optional
+
+from sqlmodel import Field, Session, SQLModel, create_engine, select
+from sqlmodel.pool import StaticPool
+
+
+def test_fields() -> None:
+    class Hero(SQLModel, table=True):
+        id: Optional[int] = Field(default=None, primary_key=True)
+        name: str
+        secret_name: str
+        age: Optional[int] = None
+        food: Optional[str] = None
+
+    engine = create_engine(
+        "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
+    )
+
+    SQLModel.metadata.create_all(engine)
+
+    with Session(engine) as session:
+        session.add(Hero(name="Deadpond", secret_name="Dive Wilson"))
+        session.add(
+            Hero(name="Spider-Boy", secret_name="Pedro Parqueador", food="pizza")
+        )
+        session.add(Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48))
+
+        session.commit()
+
+    # check typing of select with 3 fields
+    with Session(engine) as session:
+        statement_3 = select(Hero.id, Hero.name, Hero.secret_name)
+        results_3 = session.exec(statement_3)
+        for hero_3 in results_3:
+            assert len(hero_3) == 3
+            name_3: str = hero_3[1]
+            assert type(name_3) is str
+            assert type(hero_3[0]) is int
+            assert type(hero_3[2]) is str
+
+    # check typing of select with 4 fields
+    with Session(engine) as session:
+        statement_4 = select(Hero.id, Hero.name, Hero.secret_name, Hero.age)
+        results_4 = session.exec(statement_4)
+        for hero_4 in results_4:
+            assert len(hero_4) == 4
+            name_4: str = hero_4[1]
+            assert type(name_4) is str
+            assert type(hero_4[0]) is int
+            assert type(hero_4[2]) is str
+            assert type(hero_4[3]) in [int, type(None)]
+
+    # check typing of select with 5 fields: currently runs but doesn't pass mypy
+    # with Session(engine) as session:
+    #     statement_5 = select(Hero.id, Hero.name, Hero.secret_name, Hero.age, Hero.food)
+    #     results_5 = session.exec(statement_5)
+    #     for hero_5 in results_5:
+    #         assert len(hero_5) == 5
+    #         name_5: str = hero_5[1]
+    #         assert type(name_5) is str
+    #         assert type(hero_5[0]) is int
+    #         assert type(hero_5[2]) is str
+    #         assert type(hero_5[3]) in [int, type(None)]
+    #         assert type(hero_5[4]) in [str, type(None)]