From 2909242d4e29d1dfd58cb29402a4485b4fb86cda Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 6 Oct 2025 17:29:27 +0200 Subject: [PATCH 1/5] add test that runs select with 3 or 4 arguments --- tests/test_select_typing.py | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/test_select_typing.py diff --git a/tests/test_select_typing.py b/tests/test_select_typing.py new file mode 100644 index 0000000000..8f1d030631 --- /dev/null +++ b/tests/test_select_typing.py @@ -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)] From 86ef4178032115bf7e67ba11c8a08cb50f13b64e Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 6 Oct 2025 17:41:47 +0200 Subject: [PATCH 2/5] run mypy check on test_select_typing.py --- .github/workflows/test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c4fdb911a8..dd6b30ad57 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -82,6 +82,9 @@ jobs: - name: Lint if: matrix.pydantic-version == 'pydantic-v2' && matrix.python-version != '3.8' run: bash scripts/lint.sh + - name: Extra Mypy check + if: matrix.python-version == '3.13' + run: mypy tests/test_select_typing.py - run: mkdir coverage - name: Test run: bash scripts/test.sh From 140d3472ec351748f02e072c0c242da7fffbca3f Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 6 Oct 2025 17:54:13 +0200 Subject: [PATCH 3/5] add additional mypy check to lint script --- .github/workflows/test.yml | 3 --- scripts/lint.sh | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dd6b30ad57..c4fdb911a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -82,9 +82,6 @@ jobs: - name: Lint if: matrix.pydantic-version == 'pydantic-v2' && matrix.python-version != '3.8' run: bash scripts/lint.sh - - name: Extra Mypy check - if: matrix.python-version == '3.13' - run: mypy tests/test_select_typing.py - run: mkdir coverage - name: Test run: bash scripts/test.sh diff --git a/scripts/lint.sh b/scripts/lint.sh index 7fab52df57..e4a7b5bea7 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -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 From 6feb0595d82255f0e4ce18f65fc6994c0d82ac81 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 6 Oct 2025 18:01:21 +0200 Subject: [PATCH 4/5] for demonstration: uncomment part of test using select with 5 fields --- tests/test_select_typing.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_select_typing.py b/tests/test_select_typing.py index 8f1d030631..eef0e30705 100644 --- a/tests/test_select_typing.py +++ b/tests/test_select_typing.py @@ -51,14 +51,14 @@ class Hero(SQLModel, table=True): 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)] + 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)] From 427ed2dc8d718e1e48ba65aa408965e44bca4467 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 6 Oct 2025 18:03:52 +0200 Subject: [PATCH 5/5] uncomment unsupported code --- tests/test_select_typing.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_select_typing.py b/tests/test_select_typing.py index eef0e30705..8f1d030631 100644 --- a/tests/test_select_typing.py +++ b/tests/test_select_typing.py @@ -51,14 +51,14 @@ class Hero(SQLModel, table=True): 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)] + # 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)]