Skip to content

Commit 303ae74

Browse files
committed
Fix formatting
1 parent 8c2df81 commit 303ae74

14 files changed

Lines changed: 268 additions & 189 deletions

sqlmodel/_compat.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
from pydantic._internal._model_construction import ModelMetaclass as ModelMetaclass
2626
from pydantic._internal._repr import Representation as Representation
2727
from pydantic.fields import FieldInfo
28-
from sqlalchemy import inspect
29-
from sqlalchemy.orm import InstrumentedAttribute, Mapper
3028
from pydantic_core import PydanticUndefined as Undefined
3129
from pydantic_core import PydanticUndefinedType as PydanticUndefinedType
30+
from sqlalchemy import inspect
31+
from sqlalchemy.orm import Mapper
3232

3333
BaseConfig = ConfigDict
3434
UndefinedType = PydanticUndefinedType
@@ -124,8 +124,7 @@ def _is_polymorphic_subclass(bases: tuple[type, ...]) -> bool:
124124
table model) from a root table model that defines its own table.
125125
"""
126126
return any(
127-
issubclass(base, BaseModel) and hasattr(base, "__tablename__")
128-
for base in bases
127+
issubclass(base, BaseModel) and hasattr(base, "__tablename__") for base in bases
129128
)
130129

131130

@@ -159,7 +158,11 @@ def _collect_inherited_namespace(
159158

160159
def _relationship_keys(instance: _TSQLModel) -> Iterable[str]:
161160
mapper = inspect(type(instance), raiseerr=False)
162-
return mapper.relationships.keys() if mapper is not None else instance.__sqlmodel_relationships__
161+
return (
162+
mapper.relationships.keys()
163+
if mapper is not None
164+
else instance.__sqlmodel_relationships__
165+
)
163166

164167

165168
@contextmanager

sqlmodel/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ def __new__(
595595
)
596596
else:
597597
new_cls = cast(
598-
"SQLModel", super().__new__(cls, name, bases, dict_used, **config_kwargs)
598+
"SQLModel",
599+
super().__new__(cls, name, bases, dict_used, **config_kwargs),
599600
)
600601
new_cls.__annotations__ = {
601602
**relationship_annotations,
@@ -765,7 +766,7 @@ def get_sqlalchemy_type(field: Any) -> Any:
765766
raise ValueError(f"{type_} has no matching SQLAlchemy type")
766767

767768

768-
def get_column_from_field(field: Any) -> Union[Column, MappedColumn]: # type: ignore
769+
def get_column_from_field(field: Any) -> Column | MappedColumn: # type: ignore
769770
field_info = field
770771
sa_column = _get_sqlmodel_field_value(field_info, "sa_column", Undefined)
771772
if isinstance(sa_column, Column) or isinstance(sa_column, MappedColumn):

tests/test_polymorphic/test_basic.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Mirrors sqlalchemy/test/orm/inheritance/test_basic.py :: FalseDiscriminatorTest, CascadeTest"""
22

3-
from typing import Optional
4-
53
from sqlalchemy import Boolean
64
from sqlalchemy.orm import mapped_column
75
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select
@@ -11,8 +9,8 @@ def test_false_discriminator_on_sub():
119
# mirrors FalseDiscriminatorTest.test_false_on_sub
1210
class DFoo(SQLModel, table=True):
1311
__tablename__ = "t_false_sub"
14-
id: Optional[int] = Field(default=None, primary_key=True)
15-
type: Optional[bool] = Field(
12+
id: int | None = Field(default=None, primary_key=True)
13+
type: bool | None = Field(
1614
default=None, sa_column=mapped_column(Boolean, nullable=True)
1715
)
1816

@@ -40,8 +38,8 @@ def test_false_discriminator_on_base():
4038
# mirrors FalseDiscriminatorTest.test_false_on_base
4139
class Ding(SQLModel, table=True):
4240
__tablename__ = "t_false_base"
43-
id: Optional[int] = Field(default=None, primary_key=True)
44-
type: Optional[bool] = Field(
41+
id: int | None = Field(default=None, primary_key=True)
42+
type: bool | None = Field(
4543
default=None, sa_column=mapped_column(Boolean, nullable=True)
4644
)
4745

@@ -68,13 +66,13 @@ def test_cascade_delete_follows_subclass_mapper():
6866
# mirrors CascadeTest.test_cascade
6967
class Note(SQLModel, table=True):
7068
__tablename__ = "cascade_note"
71-
id: Optional[int] = Field(default=None, primary_key=True)
69+
id: int | None = Field(default=None, primary_key=True)
7270
data: str
73-
item_id: Optional[int] = Field(default=None, foreign_key="cascade_item.id")
71+
item_id: int | None = Field(default=None, foreign_key="cascade_item.id")
7472

7573
class Item(SQLModel, table=True):
7674
__tablename__ = "cascade_item"
77-
id: Optional[int] = Field(default=None, primary_key=True)
75+
id: int | None = Field(default=None, primary_key=True)
7876
type: str = Field(default="item")
7977
data: str
8078
notes: list[Note] = Relationship(sa_relationship_kwargs={"cascade": "all"})
@@ -86,7 +84,7 @@ class Item(SQLModel, table=True):
8684

8785
class SubItem(Item):
8886
__tablename__ = "cascade_subitem"
89-
id: Optional[int] = Field(
87+
id: int | None = Field(
9088
default=None, primary_key=True, foreign_key="cascade_item.id"
9189
)
9290
extra: str = Field(default="")
@@ -114,5 +112,3 @@ class SubItem(Item):
114112
with Session(engine) as db:
115113
assert db.exec(select(Note)).all() == []
116114
assert db.exec(select(Item)).all() == []
117-
118-

tests/test_polymorphic/test_concrete.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
"""Mirrors sqlalchemy/test/orm/inheritance/test_concrete.py :: ConcreteTest, PropertyInheritanceTest"""
22

3-
from typing import Optional
4-
53
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select
64

75

86
def test_basic():
97
# mirrors ConcreteTest.test_basic
108
class Manager(SQLModel, table=True):
119
__tablename__ = "cti_managers"
12-
employee_id: Optional[int] = Field(default=None, primary_key=True)
10+
employee_id: int | None = Field(default=None, primary_key=True)
1311
name: str
1412
manager_data: str
1513

1614
class Engineer(SQLModel, table=True):
1715
__tablename__ = "cti_engineers"
18-
employee_id: Optional[int] = Field(default=None, primary_key=True)
16+
employee_id: int | None = Field(default=None, primary_key=True)
1917
name: str
2018
engineer_info: str
2119

@@ -42,19 +40,19 @@ def test_multi_level_no_base():
4240
# mirrors ConcreteTest.test_multi_level_no_base
4341
class Manager(SQLModel, table=True):
4442
__tablename__ = "cti3_managers"
45-
employee_id: Optional[int] = Field(default=None, primary_key=True)
43+
employee_id: int | None = Field(default=None, primary_key=True)
4644
name: str
4745
manager_data: str
4846

4947
class Engineer(SQLModel, table=True):
5048
__tablename__ = "cti3_engineers"
51-
employee_id: Optional[int] = Field(default=None, primary_key=True)
49+
employee_id: int | None = Field(default=None, primary_key=True)
5250
name: str
5351
engineer_info: str
5452

5553
class Hacker(SQLModel, table=True):
5654
__tablename__ = "cti3_hackers"
57-
employee_id: Optional[int] = Field(default=None, primary_key=True)
55+
employee_id: int | None = Field(default=None, primary_key=True)
5856
name: str
5957
engineer_info: str
6058
nickname: str
@@ -65,7 +63,9 @@ class Hacker(SQLModel, table=True):
6563
with Session(engine) as db:
6664
db.add(Manager(name="Sally", manager_data="knows how to manage things"))
6765
db.add(Engineer(name="Jenn", engineer_info="knows how to program"))
68-
db.add(Hacker(name="Karina", engineer_info="knows how to hack", nickname="Badass"))
66+
db.add(
67+
Hacker(name="Karina", engineer_info="knows how to hack", nickname="Badass")
68+
)
6969
db.commit()
7070

7171
with Session(engine) as db:
@@ -85,26 +85,26 @@ def test_relationship():
8585
# mirrors ConcreteTest.test_relationship / PropertyInheritanceTest
8686
class Company(SQLModel, table=True):
8787
__tablename__ = "cti4_companies"
88-
id: Optional[int] = Field(default=None, primary_key=True)
88+
id: int | None = Field(default=None, primary_key=True)
8989
name: str
9090
managers: list["CtiManager"] = Relationship(back_populates="company")
9191
engineers: list["CtiEngineer"] = Relationship(back_populates="company")
9292

9393
class CtiManager(SQLModel, table=True):
9494
__tablename__ = "cti4_managers"
95-
employee_id: Optional[int] = Field(default=None, primary_key=True)
95+
employee_id: int | None = Field(default=None, primary_key=True)
9696
name: str
9797
manager_data: str
98-
company_id: Optional[int] = Field(default=None, foreign_key="cti4_companies.id")
99-
company: Optional[Company] = Relationship(back_populates="managers")
98+
company_id: int | None = Field(default=None, foreign_key="cti4_companies.id")
99+
company: Company | None = Relationship(back_populates="managers")
100100

101101
class CtiEngineer(SQLModel, table=True):
102102
__tablename__ = "cti4_engineers"
103-
employee_id: Optional[int] = Field(default=None, primary_key=True)
103+
employee_id: int | None = Field(default=None, primary_key=True)
104104
name: str
105105
engineer_info: str
106-
company_id: Optional[int] = Field(default=None, foreign_key="cti4_companies.id")
107-
company: Optional[Company] = Relationship(back_populates="engineers")
106+
company_id: int | None = Field(default=None, foreign_key="cti4_companies.id")
107+
company: Company | None = Relationship(back_populates="engineers")
108108

109109
engine = create_engine("sqlite:///:memory:")
110110
SQLModel.metadata.create_all(engine)
@@ -115,10 +115,12 @@ class CtiEngineer(SQLModel, table=True):
115115
db.flush()
116116
cid = corp.id
117117
db.add(CtiManager(name="Bill", manager_data="TPS reports", company_id=cid))
118-
db.add(CtiEngineer(name="Peter", engineer_info="cubicle dweller", company_id=cid))
118+
db.add(
119+
CtiEngineer(name="Peter", engineer_info="cubicle dweller", company_id=cid)
120+
)
119121
db.commit()
120122

121123
with Session(engine) as db:
122124
corp = db.get(Company, cid)
123125
assert len(corp.managers) == 1 and corp.managers[0].name == "Bill"
124-
assert len(corp.engineers) == 1 and corp.engineers[0].name == "Peter"
126+
assert len(corp.engineers) == 1 and corp.engineers[0].name == "Peter"

tests/test_polymorphic/test_manytomany.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
"""Mirrors sqlalchemy/test/orm/inheritance/test_manytomany.py"""
22

3-
from typing import Optional
4-
53
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine
64

75

8-
96
# Two jointed table inheritance subclasses (User, Group) linked many to many; both sides of the relationship resolve correctly.
107
def test_jti_m2m_subclass_to_subclass():
118
class UserGroupLink(SQLModel, table=True):
129
__tablename__ = "prin_user_group_map"
13-
user_id: Optional[int] = Field(
10+
user_id: int | None = Field(
1411
default=None, foreign_key="prin_users.id", primary_key=True
1512
)
16-
group_id: Optional[int] = Field(
13+
group_id: int | None = Field(
1714
default=None, foreign_key="prin_groups.id", primary_key=True
1815
)
1916

2017
class Principal(SQLModel, table=True):
2118
__tablename__ = "principals"
22-
id: Optional[int] = Field(default=None, primary_key=True)
19+
id: int | None = Field(default=None, primary_key=True)
2320
name: str
2421
type: str = Field(default="principal")
2522

@@ -30,7 +27,7 @@ class Principal(SQLModel, table=True):
3027

3128
class User(Principal, table=True):
3229
__tablename__ = "prin_users"
33-
id: Optional[int] = Field(
30+
id: int | None = Field(
3431
default=None, primary_key=True, foreign_key="principals.id"
3532
)
3633
password: str
@@ -44,7 +41,7 @@ class User(Principal, table=True):
4441

4542
class Group(Principal, table=True):
4643
__tablename__ = "prin_groups"
47-
id: Optional[int] = Field(
44+
id: int | None = Field(
4845
default=None, primary_key=True, foreign_key="principals.id"
4946
)
5047
users: list[User] = Relationship(
@@ -80,17 +77,17 @@ class Group(Principal, table=True):
8077
def test_jti_m2m_get_by_subclass_pk():
8178
class FooBarLink(SQLModel, table=True):
8279
__tablename__ = "foo_bar_link"
83-
foo_id: Optional[int] = Field(
80+
foo_id: int | None = Field(
8481
default=None, foreign_key="m2m_foo.id", primary_key=True
8582
)
86-
bar_id: Optional[int] = Field(
83+
bar_id: int | None = Field(
8784
default=None, foreign_key="m2m_bar.id", primary_key=True
8885
)
8986

9087
class Foo(SQLModel, table=True):
9188
__tablename__ = "m2m_foo"
92-
id: Optional[int] = Field(default=None, primary_key=True)
93-
data: Optional[str] = None
89+
id: int | None = Field(default=None, primary_key=True)
90+
data: str | None = None
9491
type: str = Field(default="foo")
9592

9693
__mapper_args__ = {
@@ -100,9 +97,7 @@ class Foo(SQLModel, table=True):
10097

10198
class Bar(Foo, table=True):
10299
__tablename__ = "m2m_bar"
103-
id: Optional[int] = Field(
104-
default=None, primary_key=True, foreign_key="m2m_foo.id"
105-
)
100+
id: int | None = Field(default=None, primary_key=True, foreign_key="m2m_foo.id")
106101
foos: list[Foo] = Relationship(link_model=FooBarLink)
107102

108103
__mapper_args__ = {"polymorphic_identity": "bar"}
@@ -133,26 +128,26 @@ class Bar(Foo, table=True):
133128
def test_jti_m2m_three_level():
134129
class BlubBarLink(SQLModel, table=True):
135130
__tablename__ = "blub_bar_link"
136-
blub_id: Optional[int] = Field(
131+
blub_id: int | None = Field(
137132
default=None, foreign_key="m3_blub.id", primary_key=True
138133
)
139-
bar_id: Optional[int] = Field(
134+
bar_id: int | None = Field(
140135
default=None, foreign_key="m3_bar.id", primary_key=True
141136
)
142137

143138
class BlubFooLink(SQLModel, table=True):
144139
__tablename__ = "blub_foo_link"
145-
blub_id: Optional[int] = Field(
140+
blub_id: int | None = Field(
146141
default=None, foreign_key="m3_blub.id", primary_key=True
147142
)
148-
foo_id: Optional[int] = Field(
143+
foo_id: int | None = Field(
149144
default=None, foreign_key="m3_foo.id", primary_key=True
150145
)
151146

152147
class Foo(SQLModel, table=True):
153148
__tablename__ = "m3_foo"
154-
id: Optional[int] = Field(default=None, primary_key=True)
155-
data: Optional[str] = None
149+
id: int | None = Field(default=None, primary_key=True)
150+
data: str | None = None
156151
type: str = Field(default="foo")
157152

158153
__mapper_args__ = {
@@ -162,19 +157,15 @@ class Foo(SQLModel, table=True):
162157

163158
class Bar(Foo, table=True):
164159
__tablename__ = "m3_bar"
165-
id: Optional[int] = Field(
166-
default=None, primary_key=True, foreign_key="m3_foo.id"
167-
)
168-
bar_data: Optional[str] = None
160+
id: int | None = Field(default=None, primary_key=True, foreign_key="m3_foo.id")
161+
bar_data: str | None = None
169162

170163
__mapper_args__ = {"polymorphic_identity": "bar"}
171164

172165
class Blub(Bar, table=True):
173166
__tablename__ = "m3_blub"
174-
id: Optional[int] = Field(
175-
default=None, primary_key=True, foreign_key="m3_bar.id"
176-
)
177-
blub_data: Optional[str] = None
167+
id: int | None = Field(default=None, primary_key=True, foreign_key="m3_bar.id")
168+
blub_data: str | None = None
178169
bars: list[Bar] = Relationship(link_model=BlubBarLink)
179170
foos: list[Foo] = Relationship(link_model=BlubFooLink)
180171

0 commit comments

Comments
 (0)