Skip to content

Commit 458968d

Browse files
Formatting
1 parent 684ff65 commit 458968d

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

sqlmodel/main.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@
1111
Callable,
1212
ClassVar,
1313
Dict,
14+
ForwardRef,
1415
List,
1516
Mapping,
1617
Optional,
1718
Sequence,
1819
Set,
1920
Tuple,
2021
Type,
21-
TypeVar,ForwardRef,
22+
TypeVar,
2223
Union,
23-
cast,get_origin,get_args
24+
cast,
25+
get_args,
26+
get_origin,
2427
)
2528

26-
from pydantic import BaseModel, ValidationError
27-
from pydantic_core import PydanticUndefined, PydanticUndefinedType
28-
from pydantic.fields import FieldInfo as PydanticFieldInfo
29+
from pydantic import BaseModel
30+
from pydantic._internal._fields import PydanticGeneralMetadata
2931
from pydantic._internal._model_construction import ModelMetaclass
3032
from pydantic._internal._repr import Representation
31-
from pydantic._internal._fields import PydanticGeneralMetadata
33+
from pydantic.fields import FieldInfo as PydanticFieldInfo
34+
from pydantic_core import PydanticUndefined, PydanticUndefinedType
3235
from sqlalchemy import Boolean, Column, Date, DateTime
3336
from sqlalchemy import Enum as sa_Enum
3437
from sqlalchemy import Float, ForeignKey, Integer, Interval, Numeric, inspect
@@ -148,7 +151,9 @@ def Field(
148151
index: Union[bool, PydanticUndefinedType] = PydanticUndefined,
149152
sa_column: Union[Column, PydanticUndefinedType] = PydanticUndefined, # type: ignore
150153
sa_column_args: Union[Sequence[Any], PydanticUndefinedType] = PydanticUndefined,
151-
sa_column_kwargs: Union[Mapping[str, Any], PydanticUndefinedType] = PydanticUndefined,
154+
sa_column_kwargs: Union[
155+
Mapping[str, Any], PydanticUndefinedType
156+
] = PydanticUndefined,
152157
schema_extra: Optional[Dict[str, Any]] = None,
153158
) -> Any:
154159
current_schema_extra = schema_extra or {}
@@ -230,7 +235,7 @@ def __new__(
230235
class_dict: Dict[str, Any],
231236
**kwargs: Any,
232237
) -> Any:
233-
238+
234239
relationships: Dict[str, RelationshipInfo] = {}
235240
dict_for_pydantic = {}
236241
original_annotations = class_dict.get("__annotations__", {})
@@ -267,21 +272,21 @@ def __new__(
267272
key: pydantic_kwargs.pop(key)
268273
for key in pydantic_kwargs.keys() & allowed_config_kwargs
269274
}
270-
config_table = getattr(class_dict.get('Config', object()), 'table', False)
275+
config_table = getattr(class_dict.get("Config", object()), "table", False)
271276
# If we have a table, we need to have defaults for all fields
272277
# Pydantic v2 sets a __pydantic_core_schema__ which is very hard to change. Changing the fields does not do anything
273278
if config_table is True:
274279
for key in original_annotations.keys():
275280
if dict_used.get(key, PydanticUndefined) is PydanticUndefined:
276281
dict_used[key] = None
277-
282+
278283
new_cls = super().__new__(cls, name, bases, dict_used, **config_kwargs)
279284
new_cls.__annotations__ = {
280285
**relationship_annotations,
281286
**pydantic_annotations,
282287
**new_cls.__annotations__,
283288
}
284-
289+
285290
def get_config(name: str) -> Any:
286291
config_class_value = new_cls.model_config.get(name, PydanticUndefined)
287292
if config_class_value is not PydanticUndefined:
@@ -294,7 +299,7 @@ def get_config(name: str) -> Any:
294299
config_table = get_config("table")
295300
if config_table is True:
296301
# If it was passed by kwargs, ensure it's also set in config
297-
new_cls.model_config['table'] = config_table
302+
new_cls.model_config["table"] = config_table
298303
for k, v in new_cls.model_fields.items():
299304
col = get_column_from_field(v)
300305
setattr(new_cls, k, col)
@@ -303,13 +308,13 @@ def get_config(name: str) -> Any:
303308
# This could be done by reading new_cls.model_config['table'] in FastAPI, but
304309
# that's very specific about SQLModel, so let's have another config that
305310
# other future tools based on Pydantic can use.
306-
new_cls.model_config['read_from_attributes'] = True
311+
new_cls.model_config["read_from_attributes"] = True
307312

308313
config_registry = get_config("registry")
309314
if config_registry is not PydanticUndefined:
310315
config_registry = cast(registry, config_registry)
311316
# If it was passed by kwargs, ensure it's also set in config
312-
new_cls.model_config['registry'] = config_table
317+
new_cls.model_config["registry"] = config_table
313318
setattr(new_cls, "_sa_registry", config_registry)
314319
setattr(new_cls, "metadata", config_registry.metadata)
315320
setattr(new_cls, "__abstract__", True)
@@ -366,7 +371,7 @@ def __init__(
366371
rel_args.extend(rel_info.sa_relationship_args)
367372
if rel_info.sa_relationship_kwargs:
368373
rel_kwargs.update(rel_info.sa_relationship_kwargs)
369-
rel_value: RelationshipProperty = relationship(
374+
rel_value: RelationshipProperty = relationship(
370375
relationship_to, *rel_args, **rel_kwargs
371376
)
372377
dict_used[rel_name] = rel_value
@@ -382,8 +387,10 @@ def get_sqlalchemy_type(field: FieldInfo) -> Any:
382387
if type_ is not None and get_origin(type_) is Union:
383388
bases = get_args(type_)
384389
if len(bases) > 2:
385-
raise RuntimeError("Cannot have a (non-optional) union as a SQL alchemy field")
386-
type_ = bases[0]
390+
raise RuntimeError(
391+
"Cannot have a (non-optional) union as a SQL alchemy field"
392+
)
393+
type_ = bases[0]
387394

388395
# The 3rd is PydanticGeneralMetadata
389396
metadata = _get_field_metadata(field)
@@ -515,7 +522,6 @@ def __repr_args__(self) -> Sequence[Tuple[Optional[str], Any]]:
515522
# Don't show SQLAlchemy private attributes
516523
return [(k, v) for k, v in self.__dict__.items() if not k.startswith("_sa_")]
517524

518-
519525
@declared_attr # type: ignore
520526
def __tablename__(cls) -> str:
521527
return cls.__name__.lower()
@@ -532,8 +538,9 @@ def _is_field_noneable(field: FieldInfo) -> bool:
532538
return False
533539
return False
534540

541+
535542
def _get_field_metadata(field: FieldInfo) -> object:
536543
for meta in field.metadata:
537544
if isinstance(meta, PydanticGeneralMetadata):
538545
return meta
539-
return object()
546+
return object()

sqlmodel/typing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from typing import Any, Optional
2+
13
from pydantic import ConfigDict
2-
from typing import Optional, Any
4+
35

46
class SQLModelConfig(ConfigDict):
57
table: Optional[bool]
68
read_from_attributes: Optional[bool]
7-
registry: Optional[Any]
9+
registry: Optional[Any]

tests/test_instance_no_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
def test_allow_instantiation_without_arguments(clear_sqlmodel):
99
class Item(SQLModel):
1010
id: Optional[int] = Field(default=None, primary_key=True)
11-
name: str
11+
name: str
1212
description: Optional[str] = None
1313

1414
class Config:

tests/test_missing_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import Optional
22

33
import pytest
4-
from sqlmodel import Field, SQLModel
54
from pydantic import BaseModel
5+
from sqlmodel import Field, SQLModel
66

77

88
def test_missing_sql_type():

0 commit comments

Comments
 (0)