@@ -101,7 +101,7 @@ def __init__(
101101 * ,
102102 back_populates : Optional [str ] = None ,
103103 link_model : Optional [Any ] = None ,
104- sa_relationship : Optional [RelationshipProperty ] = None ,
104+ sa_relationship : Optional [RelationshipProperty ] = None , # type: ignore
105105 sa_relationship_args : Optional [Sequence [Any ]] = None ,
106106 sa_relationship_kwargs : Optional [Mapping [str , Any ]] = None ,
107107 ) -> None :
@@ -127,32 +127,32 @@ def Field(
127127 default : Any = Undefined ,
128128 * ,
129129 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 ,
133133 exclude : Union [
134134 AbstractSet [Union [int , str ]], Mapping [Union [int , str ], Any ], Any
135135 ] = None ,
136136 include : Union [
137137 AbstractSet [Union [int , str ]], Mapping [Union [int , str ], Any ], Any
138138 ] = 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 ,
149149 allow_mutation : bool = True ,
150- regex : str = None ,
150+ regex : Optional [ str ] = None ,
151151 primary_key : bool = False ,
152152 foreign_key : Optional [Any ] = None ,
153153 nullable : Union [bool , UndefinedType ] = Undefined ,
154154 index : Union [bool , UndefinedType ] = Undefined ,
155- sa_column : Union [Column , UndefinedType ] = Undefined ,
155+ sa_column : Union [Column , UndefinedType ] = Undefined , # type: ignore
156156 sa_column_args : Union [Sequence [Any ], UndefinedType ] = Undefined ,
157157 sa_column_kwargs : Union [Mapping [str , Any ], UndefinedType ] = Undefined ,
158158 schema_extra : Optional [Dict [str , Any ]] = None ,
@@ -195,7 +195,7 @@ def Relationship(
195195 * ,
196196 back_populates : Optional [str ] = None ,
197197 link_model : Optional [Any ] = None ,
198- sa_relationship : Optional [RelationshipProperty ] = None ,
198+ sa_relationship : Optional [RelationshipProperty ] = None , # type: ignore
199199 sa_relationship_args : Optional [Sequence [Any ]] = None ,
200200 sa_relationship_kwargs : Optional [Mapping [str , Any ]] = None ,
201201) -> Any :
@@ -217,19 +217,25 @@ class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
217217
218218 # Replicate SQLAlchemy
219219 def __setattr__ (cls , name : str , value : Any ) -> None :
220- if getattr (cls .__config__ , "table" , False ): # type: ignore
220+ if getattr (cls .__config__ , "table" , False ):
221221 DeclarativeMeta .__setattr__ (cls , name , value )
222222 else :
223223 super ().__setattr__ (name , value )
224224
225225 def __delattr__ (cls , name : str ) -> None :
226- if getattr (cls .__config__ , "table" , False ): # type: ignore
226+ if getattr (cls .__config__ , "table" , False ):
227227 DeclarativeMeta .__delattr__ (cls , name )
228228 else :
229229 super ().__delattr__ (name )
230230
231231 # 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 :
233239 relationships : Dict [str , RelationshipInfo ] = {}
234240 dict_for_pydantic = {}
235241 original_annotations = resolve_annotations (
@@ -342,7 +348,7 @@ def __init__(
342348 )
343349 relationship_to = temp_field .type_
344350 if isinstance (temp_field .type_ , ForwardRef ):
345- relationship_to = temp_field .type_ .__forward_arg__ # type: ignore
351+ relationship_to = temp_field .type_ .__forward_arg__
346352 rel_kwargs : Dict [str , Any ] = {}
347353 if rel_info .back_populates :
348354 rel_kwargs ["back_populates" ] = rel_info .back_populates
@@ -360,7 +366,7 @@ def __init__(
360366 rel_args .extend (rel_info .sa_relationship_args )
361367 if rel_info .sa_relationship_kwargs :
362368 rel_kwargs .update (rel_info .sa_relationship_kwargs )
363- rel_value : RelationshipProperty = relationship (
369+ rel_value : RelationshipProperty = relationship ( # type: ignore
364370 relationship_to , * rel_args , ** rel_kwargs
365371 )
366372 dict_used [rel_name ] = rel_value
@@ -408,7 +414,7 @@ def get_sqlachemy_type(field: ModelField) -> Any:
408414 return GUID
409415
410416
411- def get_column_from_field (field : ModelField ) -> Column :
417+ def get_column_from_field (field : ModelField ) -> Column : # type: ignore
412418 sa_column = getattr (field .field_info , "sa_column" , Undefined )
413419 if isinstance (sa_column , Column ):
414420 return sa_column
@@ -440,10 +446,10 @@ def get_column_from_field(field: ModelField) -> Column:
440446 kwargs ["default" ] = sa_default
441447 sa_column_args = getattr (field .field_info , "sa_column_args" , Undefined )
442448 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 )))
444450 sa_column_kwargs = getattr (field .field_info , "sa_column_kwargs" , Undefined )
445451 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 ))
447453 return Column (sa_type , * args , ** kwargs )
448454
449455
@@ -452,24 +458,27 @@ def get_column_from_field(field: ModelField) -> Column:
452458default_registry = registry ()
453459
454460
455- def _value_items_is_true (v ) -> bool :
461+ def _value_items_is_true (v : Any ) -> bool :
456462 # Re-implement Pydantic's ValueItems.is_true() as it hasn't been released as of
457463 # the current latest, Pydantic 1.8.2
458464 return v is True or v is ...
459465
460466
467+ _TSQLModel = TypeVar ("_TSQLModel" , bound = "SQLModel" )
468+
469+
461470class SQLModel (BaseModel , metaclass = SQLModelMetaclass , registry = default_registry ):
462471 # SQLAlchemy needs to set weakref(s), Pydantic will set the other slots values
463472 __slots__ = ("__weakref__" ,)
464473 __tablename__ : ClassVar [Union [str , Callable [..., str ]]]
465- __sqlmodel_relationships__ : ClassVar [Dict [str , RelationshipProperty ]]
474+ __sqlmodel_relationships__ : ClassVar [Dict [str , RelationshipProperty ]] # type: ignore
466475 __name__ : ClassVar [str ]
467476 metadata : ClassVar [MetaData ]
468477
469478 class Config :
470479 orm_mode = True
471480
472- def __new__ (cls , * args , ** kwargs ) -> Any :
481+ def __new__ (cls , * args : Any , ** kwargs : Any ) -> Any :
473482 new_object = super ().__new__ (cls )
474483 # SQLAlchemy doesn't call __init__ on the base class
475484 # Ref: https://docs.sqlalchemy.org/en/14/orm/constructors.html
@@ -520,7 +529,9 @@ def __setattr__(self, name: str, value: Any) -> None:
520529 super ().__setattr__ (name , value )
521530
522531 @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 :
524535 # Duplicated from Pydantic
525536 if not cls .__config__ .orm_mode :
526537 raise ConfigError (
@@ -533,7 +544,7 @@ def from_orm(cls: Type["SQLModel"], obj: Any, update: Dict[str, Any] = None):
533544 # End SQLModel support dict
534545 if not getattr (cls .__config__ , "table" , False ):
535546 # If not table, normal Pydantic code
536- m = cls .__new__ (cls )
547+ m : _TSQLModel = cls .__new__ (cls )
537548 else :
538549 # If table, create the new instance normally to make SQLAlchemy create
539550 # the _sa_instance_state attribute
@@ -554,7 +565,7 @@ def from_orm(cls: Type["SQLModel"], obj: Any, update: Dict[str, Any] = None):
554565
555566 @classmethod
556567 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
558569 ) -> "SQLModel" :
559570 obj = cls ._enforce_dict_if_root (obj )
560571 # SQLModel, support update dict
0 commit comments