Skip to content

Commit 2ca9957

Browse files
authored
Merge branch 'main' into dependabot/pip/ruff-0.14.0
2 parents 916abfb + 2d3b25d commit 2ca9957

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

docs/release-notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
## Latest Changes
44

5+
### Fixes
6+
7+
* 🐛 Fix attribute handling in `model_dump` for compatibility with the latest Pydantic versions. PR [#1595](https://github.com/fastapi/sqlmodel/pull/1595) by [@spazm](https://github.com/spazm).
8+
59
### Docs
610

711
* 📝 Fix typo in `docs/tutorial/fastapi/simple-hero-api.md`. PR [#1583](https://github.com/fastapi/sqlmodel/pull/1583) by [@kofi-kusi](https://github.com/kofi-kusi).
812

913
### Internal
1014

15+
* ✅ Add test that runs select with 3 or 4 arguments. PR [#1590](https://github.com/fastapi/sqlmodel/pull/1590) by [@svlandeg](https://github.com/svlandeg).
1116
* ⬆ Bump mkdocs-macros-plugin from 1.3.9 to 1.4.0. PR [#1581](https://github.com/fastapi/sqlmodel/pull/1581) by [@dependabot[bot]](https://github.com/apps/dependabot).
1217
* ⬆ Bump mkdocs-material from 9.6.20 to 9.6.21. PR [#1588](https://github.com/fastapi/sqlmodel/pull/1588) by [@dependabot[bot]](https://github.com/apps/dependabot).
1318
*[pre-commit.ci] pre-commit autoupdate. PR [#1584](https://github.com/fastapi/sqlmodel/pull/1584) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci).

scripts/lint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ set -e
44
set -x
55

66
mypy sqlmodel
7+
mypy tests/test_select_typing.py
78
ruff check sqlmodel tests docs_src scripts
89
ruff format sqlmodel tests docs_src scripts --check

sqlmodel/main.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def __dataclass_transform__(
109109
return lambda a: a
110110

111111

112-
class FieldInfo(PydanticFieldInfo):
112+
class FieldInfo(PydanticFieldInfo): # type: ignore[misc]
113+
# mypy - ignore that PydanticFieldInfo is @final
113114
def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
114115
primary_key = kwargs.pop("primary_key", False)
115116
nullable = kwargs.pop("nullable", Undefined)
@@ -863,27 +864,27 @@ def model_dump(
863864
mode: Union[Literal["json", "python"], str] = "python",
864865
include: Union[IncEx, None] = None,
865866
exclude: Union[IncEx, None] = None,
866-
context: Union[Any, None] = None,
867+
context: Union[Any, None] = None, # v2.7
867868
by_alias: Union[bool, None] = None,
868869
exclude_unset: bool = False,
869870
exclude_defaults: bool = False,
870871
exclude_none: bool = False,
872+
exclude_computed_fields: bool = False, # v2.12
871873
round_trip: bool = False,
872874
warnings: Union[bool, Literal["none", "warn", "error"]] = True,
873-
fallback: Union[Callable[[Any], Any], None] = None,
874-
serialize_as_any: bool = False,
875+
fallback: Union[Callable[[Any], Any], None] = None, # v2.11
876+
serialize_as_any: bool = False, # v2.7
875877
) -> Dict[str, Any]:
876878
if PYDANTIC_MINOR_VERSION < (2, 11):
877879
by_alias = by_alias or False
880+
extra_kwargs: Dict[str, Any] = {}
878881
if PYDANTIC_MINOR_VERSION >= (2, 7):
879-
extra_kwargs: Dict[str, Any] = {
880-
"context": context,
881-
"serialize_as_any": serialize_as_any,
882-
}
882+
extra_kwargs["context"] = context
883+
extra_kwargs["serialize_as_any"] = serialize_as_any
883884
if PYDANTIC_MINOR_VERSION >= (2, 11):
884885
extra_kwargs["fallback"] = fallback
885-
else:
886-
extra_kwargs = {}
886+
if PYDANTIC_MINOR_VERSION >= (2, 12):
887+
extra_kwargs["exclude_computed_fields"] = exclude_computed_fields
887888
if IS_PYDANTIC_V2:
888889
return super().model_dump(
889890
mode=mode,

tests/test_select_typing.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, Session, SQLModel, create_engine, select
4+
from sqlmodel.pool import StaticPool
5+
6+
7+
def test_fields() -> None:
8+
class Hero(SQLModel, table=True):
9+
id: Optional[int] = Field(default=None, primary_key=True)
10+
name: str
11+
secret_name: str
12+
age: Optional[int] = None
13+
food: Optional[str] = None
14+
15+
engine = create_engine(
16+
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
17+
)
18+
19+
SQLModel.metadata.create_all(engine)
20+
21+
with Session(engine) as session:
22+
session.add(Hero(name="Deadpond", secret_name="Dive Wilson"))
23+
session.add(
24+
Hero(name="Spider-Boy", secret_name="Pedro Parqueador", food="pizza")
25+
)
26+
session.add(Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48))
27+
28+
session.commit()
29+
30+
# check typing of select with 3 fields
31+
with Session(engine) as session:
32+
statement_3 = select(Hero.id, Hero.name, Hero.secret_name)
33+
results_3 = session.exec(statement_3)
34+
for hero_3 in results_3:
35+
assert len(hero_3) == 3
36+
name_3: str = hero_3[1]
37+
assert type(name_3) is str
38+
assert type(hero_3[0]) is int
39+
assert type(hero_3[2]) is str
40+
41+
# check typing of select with 4 fields
42+
with Session(engine) as session:
43+
statement_4 = select(Hero.id, Hero.name, Hero.secret_name, Hero.age)
44+
results_4 = session.exec(statement_4)
45+
for hero_4 in results_4:
46+
assert len(hero_4) == 4
47+
name_4: str = hero_4[1]
48+
assert type(name_4) is str
49+
assert type(hero_4[0]) is int
50+
assert type(hero_4[2]) is str
51+
assert type(hero_4[3]) in [int, type(None)]
52+
53+
# check typing of select with 5 fields: currently runs but doesn't pass mypy
54+
# with Session(engine) as session:
55+
# statement_5 = select(Hero.id, Hero.name, Hero.secret_name, Hero.age, Hero.food)
56+
# results_5 = session.exec(statement_5)
57+
# for hero_5 in results_5:
58+
# assert len(hero_5) == 5
59+
# name_5: str = hero_5[1]
60+
# assert type(name_5) is str
61+
# assert type(hero_5[0]) is int
62+
# assert type(hero_5[2]) is str
63+
# assert type(hero_5[3]) in [int, type(None)]
64+
# assert type(hero_5[4]) in [str, type(None)]

0 commit comments

Comments
 (0)