@@ -101,7 +101,7 @@ def __init__(
101
101
* ,
102
102
back_populates : Optional [str ] = None ,
103
103
link_model : Optional [Any ] = None ,
104
- sa_relationship : Optional [RelationshipProperty ] = None ,
104
+ sa_relationship : Optional [RelationshipProperty ] = None , # type: ignore
105
105
sa_relationship_args : Optional [Sequence [Any ]] = None ,
106
106
sa_relationship_kwargs : Optional [Mapping [str , Any ]] = None ,
107
107
) -> None :
@@ -127,32 +127,32 @@ def Field(
127
127
default : Any = Undefined ,
128
128
* ,
129
129
default_factory : Optional [NoArgAnyCallable ] = None ,
130
- alias : str = None ,
131
- title : str = None ,
132
- description : str = None ,
130
+ alias : Optional [ str ] = None ,
131
+ title : Optional [ str ] = None ,
132
+ description : Optional [ str ] = None ,
133
133
exclude : Union [
134
134
AbstractSet [Union [int , str ]], Mapping [Union [int , str ], Any ], Any
135
135
] = None ,
136
136
include : Union [
137
137
AbstractSet [Union [int , str ]], Mapping [Union [int , str ], Any ], Any
138
138
] = None ,
139
- const : bool = None ,
140
- gt : float = None ,
141
- ge : float = None ,
142
- lt : float = None ,
143
- le : float = None ,
144
- multiple_of : float = None ,
145
- min_items : int = None ,
146
- max_items : int = None ,
147
- min_length : int = None ,
148
- max_length : int = None ,
139
+ const : Optional [ bool ] = None ,
140
+ gt : Optional [ float ] = None ,
141
+ ge : Optional [ float ] = None ,
142
+ lt : Optional [ float ] = None ,
143
+ le : Optional [ float ] = None ,
144
+ multiple_of : Optional [ float ] = None ,
145
+ min_items : Optional [ int ] = None ,
146
+ max_items : Optional [ int ] = None ,
147
+ min_length : Optional [ int ] = None ,
148
+ max_length : Optional [ int ] = None ,
149
149
allow_mutation : bool = True ,
150
- regex : str = None ,
150
+ regex : Optional [ str ] = None ,
151
151
primary_key : bool = False ,
152
152
foreign_key : Optional [Any ] = None ,
153
153
nullable : Union [bool , UndefinedType ] = Undefined ,
154
154
index : Union [bool , UndefinedType ] = Undefined ,
155
- sa_column : Union [Column , UndefinedType ] = Undefined ,
155
+ sa_column : Union [Column , UndefinedType ] = Undefined , # type: ignore
156
156
sa_column_args : Union [Sequence [Any ], UndefinedType ] = Undefined ,
157
157
sa_column_kwargs : Union [Mapping [str , Any ], UndefinedType ] = Undefined ,
158
158
schema_extra : Optional [Dict [str , Any ]] = None ,
@@ -195,7 +195,7 @@ def Relationship(
195
195
* ,
196
196
back_populates : Optional [str ] = None ,
197
197
link_model : Optional [Any ] = None ,
198
- sa_relationship : Optional [RelationshipProperty ] = None ,
198
+ sa_relationship : Optional [RelationshipProperty ] = None , # type: ignore
199
199
sa_relationship_args : Optional [Sequence [Any ]] = None ,
200
200
sa_relationship_kwargs : Optional [Mapping [str , Any ]] = None ,
201
201
) -> Any :
@@ -217,19 +217,25 @@ class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
217
217
218
218
# Replicate SQLAlchemy
219
219
def __setattr__ (cls , name : str , value : Any ) -> None :
220
- if getattr (cls .__config__ , "table" , False ): # type: ignore
220
+ if getattr (cls .__config__ , "table" , False ):
221
221
DeclarativeMeta .__setattr__ (cls , name , value )
222
222
else :
223
223
super ().__setattr__ (name , value )
224
224
225
225
def __delattr__ (cls , name : str ) -> None :
226
- if getattr (cls .__config__ , "table" , False ): # type: ignore
226
+ if getattr (cls .__config__ , "table" , False ):
227
227
DeclarativeMeta .__delattr__ (cls , name )
228
228
else :
229
229
super ().__delattr__ (name )
230
230
231
231
# From Pydantic
232
- def __new__ (cls , name , bases , class_dict : dict , ** kwargs ) -> Any :
232
+ def __new__ (
233
+ cls ,
234
+ name : str ,
235
+ bases : Tuple [Type [Any ], ...],
236
+ class_dict : Dict [str , Any ],
237
+ ** kwargs : Any ,
238
+ ) -> Any :
233
239
relationships : Dict [str , RelationshipInfo ] = {}
234
240
dict_for_pydantic = {}
235
241
original_annotations = resolve_annotations (
@@ -342,7 +348,7 @@ def __init__(
342
348
)
343
349
relationship_to = temp_field .type_
344
350
if isinstance (temp_field .type_ , ForwardRef ):
345
- relationship_to = temp_field .type_ .__forward_arg__ # type: ignore
351
+ relationship_to = temp_field .type_ .__forward_arg__
346
352
rel_kwargs : Dict [str , Any ] = {}
347
353
if rel_info .back_populates :
348
354
rel_kwargs ["back_populates" ] = rel_info .back_populates
@@ -360,7 +366,7 @@ def __init__(
360
366
rel_args .extend (rel_info .sa_relationship_args )
361
367
if rel_info .sa_relationship_kwargs :
362
368
rel_kwargs .update (rel_info .sa_relationship_kwargs )
363
- rel_value : RelationshipProperty = relationship (
369
+ rel_value : RelationshipProperty = relationship ( # type: ignore
364
370
relationship_to , * rel_args , ** rel_kwargs
365
371
)
366
372
dict_used [rel_name ] = rel_value
@@ -408,7 +414,7 @@ def get_sqlachemy_type(field: ModelField) -> Any:
408
414
return GUID
409
415
410
416
411
- def get_column_from_field (field : ModelField ) -> Column :
417
+ def get_column_from_field (field : ModelField ) -> Column : # type: ignore
412
418
sa_column = getattr (field .field_info , "sa_column" , Undefined )
413
419
if isinstance (sa_column , Column ):
414
420
return sa_column
@@ -440,10 +446,10 @@ def get_column_from_field(field: ModelField) -> Column:
440
446
kwargs ["default" ] = sa_default
441
447
sa_column_args = getattr (field .field_info , "sa_column_args" , Undefined )
442
448
if sa_column_args is not Undefined :
443
- args .extend (list (cast (Sequence , sa_column_args )))
449
+ args .extend (list (cast (Sequence [ Any ] , sa_column_args )))
444
450
sa_column_kwargs = getattr (field .field_info , "sa_column_kwargs" , Undefined )
445
451
if sa_column_kwargs is not Undefined :
446
- kwargs .update (cast (dict , sa_column_kwargs ))
452
+ kwargs .update (cast (Dict [ Any , Any ] , sa_column_kwargs ))
447
453
return Column (sa_type , * args , ** kwargs )
448
454
449
455
@@ -452,24 +458,27 @@ def get_column_from_field(field: ModelField) -> Column:
452
458
default_registry = registry ()
453
459
454
460
455
- def _value_items_is_true (v ) -> bool :
461
+ def _value_items_is_true (v : Any ) -> bool :
456
462
# Re-implement Pydantic's ValueItems.is_true() as it hasn't been released as of
457
463
# the current latest, Pydantic 1.8.2
458
464
return v is True or v is ...
459
465
460
466
467
+ _TSQLModel = TypeVar ("_TSQLModel" , bound = "SQLModel" )
468
+
469
+
461
470
class SQLModel (BaseModel , metaclass = SQLModelMetaclass , registry = default_registry ):
462
471
# SQLAlchemy needs to set weakref(s), Pydantic will set the other slots values
463
472
__slots__ = ("__weakref__" ,)
464
473
__tablename__ : ClassVar [Union [str , Callable [..., str ]]]
465
- __sqlmodel_relationships__ : ClassVar [Dict [str , RelationshipProperty ]]
474
+ __sqlmodel_relationships__ : ClassVar [Dict [str , RelationshipProperty ]] # type: ignore
466
475
__name__ : ClassVar [str ]
467
476
metadata : ClassVar [MetaData ]
468
477
469
478
class Config :
470
479
orm_mode = True
471
480
472
- def __new__ (cls , * args , ** kwargs ) -> Any :
481
+ def __new__ (cls , * args : Any , ** kwargs : Any ) -> Any :
473
482
new_object = super ().__new__ (cls )
474
483
# SQLAlchemy doesn't call __init__ on the base class
475
484
# Ref: https://docs.sqlalchemy.org/en/14/orm/constructors.html
@@ -520,7 +529,9 @@ def __setattr__(self, name: str, value: Any) -> None:
520
529
super ().__setattr__ (name , value )
521
530
522
531
@classmethod
523
- def from_orm (cls : Type ["SQLModel" ], obj : Any , update : Dict [str , Any ] = None ):
532
+ def from_orm (
533
+ cls : Type [_TSQLModel ], obj : Any , update : Optional [Dict [str , Any ]] = None
534
+ ) -> _TSQLModel :
524
535
# Duplicated from Pydantic
525
536
if not cls .__config__ .orm_mode :
526
537
raise ConfigError (
@@ -533,7 +544,7 @@ def from_orm(cls: Type["SQLModel"], obj: Any, update: Dict[str, Any] = None):
533
544
# End SQLModel support dict
534
545
if not getattr (cls .__config__ , "table" , False ):
535
546
# If not table, normal Pydantic code
536
- m = cls .__new__ (cls )
547
+ m : _TSQLModel = cls .__new__ (cls )
537
548
else :
538
549
# If table, create the new instance normally to make SQLAlchemy create
539
550
# the _sa_instance_state attribute
@@ -554,7 +565,7 @@ def from_orm(cls: Type["SQLModel"], obj: Any, update: Dict[str, Any] = None):
554
565
555
566
@classmethod
556
567
def parse_obj (
557
- cls : Type ["SQLModel" ], obj : Any , update : Dict [str , Any ] = None
568
+ cls : Type ["SQLModel" ], obj : Any , update : Optional [ Dict [str , Any ] ] = None
558
569
) -> "SQLModel" :
559
570
obj = cls ._enforce_dict_if_root (obj )
560
571
# SQLModel, support update dict
0 commit comments