2020
2121
2222# List[X] -> list, list -> list
23- def optional_origin (clazz : Type [ Any ]) -> Type [ Any ]:
23+ def optional_origin (clazz : Union [ type , Tuple [ Any ], None ] ) -> Union [ type , Tuple [ Any ], None ]:
2424 maybe_optional = get_args (clazz )[0 ] if is_optional (clazz ) else clazz
2525 origin = get_origin (maybe_optional )
26- return origin if origin else maybe_optional # type: ignore
26+ return origin if origin else maybe_optional
2727
2828
2929# Optional[x] -> true
30- def is_optional (clazz : Union [type , Tuple [Any ]]) -> bool :
30+ def is_optional (clazz : Union [type , Tuple [Any ], None ]) -> bool :
3131 args = get_args (clazz )
3232 return get_origin (clazz ) is Union and type (None ) in args and len (args ) == 2
3333
3434
3535# List[x] -> true, list -> true
36- def is_collection (clazz : type ) -> bool :
36+ def is_collection (clazz : Union [ type , Tuple [ Any ], None ] ) -> bool :
3737 return optional_origin (clazz ) in [list , set , tuple ]
3838
3939
4040# Dict[x,y] -> true, dict -> true
41- def is_dict (clazz : type ) -> bool :
41+ def is_dict (clazz : Union [ type , Tuple [ Any ], None ] ) -> bool :
4242 return optional_origin (clazz ) in [dict ]
4343
4444
4545# either enum or optional enum
46- def is_enum (clazz : type ) -> bool :
46+ def is_enum (clazz : Union [ type , Tuple [ Any ], None ] ) -> bool :
4747 origin = optional_origin (clazz )
4848 return isinstance (origin , type ) and issubclass (origin , Enum )
4949
5050
5151# List[X] -> X, list -> object
52- def type_arg (clazz : type ) -> type :
52+ def type_arg (clazz : Union [ type , Tuple [ Any ], None ] ) -> type :
5353 maybe_optional = get_args (clazz )[0 ] if is_optional (clazz ) else clazz
5454 args = get_args (maybe_optional )
55- return args [0 ] if args and len (args ) == 1 else object # type: ignore
55+ return args [0 ] if args and len (args ) == 1 else object
5656
5757
5858# Dict[X,Y] -> (X,Y), dict -> (object, object)
59- def dict_types (clazz : type ) -> Tuple [type , type ]:
59+ def dict_types (clazz : Union [ type , Tuple [ Any ], None ] ) -> Tuple [type , type ]:
6060 maybe_optional = get_args (clazz )[0 ] if is_optional (clazz ) else clazz
6161 args = get_args (maybe_optional )
6262 return (args [0 ], args [1 ]) if args and len (args ) == 2 else (object , object )
@@ -76,7 +76,7 @@ def check(to_check: type) -> None:
7676 check (value_type )
7777 elif is_collection (clazz ):
7878 check (type_arg (to_check ))
79- elif attrs .has (clazz ):
79+ elif isinstance ( clazz , type ) and attrs .has (clazz ):
8080 if getattr (clazz , "_model_export" , True ) is False :
8181 return
8282 resolve_types (clazz )
@@ -91,7 +91,7 @@ def check(to_check: type) -> None:
9191 continue
9292 check (field .type )
9393 elif is_enum (clazz ):
94- all_classes .add (clazz )
94+ all_classes .add (clazz ) # type: ignore
9595
9696 for c in classes :
9797 check (c )
@@ -123,7 +123,7 @@ def model_name(clazz: Union[type, Tuple[Any], None]) -> str:
123123 return f"dictionary[{ model_name (key_type )} , { model_name (value_type )} ]"
124124 elif is_enum (to_check ):
125125 # camel case to snake case
126- return re .sub (r"(?<!^)(?=[A-Z])" , "_" , to_check .__name__ ).lower ()
126+ return re .sub (r"(?<!^)(?=[A-Z])" , "_" , to_check .__name__ ).lower () # type: ignore
127127 elif get_origin (to_check ) == Union :
128128 # this is a union of different types other than none.
129129 # since union types are not supported, we fallback to any here
@@ -132,7 +132,7 @@ def model_name(clazz: Union[type, Tuple[Any], None]) -> str:
132132 return model_name (get_args (to_check ))
133133 elif isinstance (to_check , type ) and issubclass (to_check , simple_type ):
134134 return lookup [to_check ]
135- elif attrs .has (to_check ):
135+ elif isinstance ( to_check , type ) and attrs .has (to_check ):
136136 name = getattr (to_check , "kind" , None )
137137 if not name :
138138 raise AttributeError (f"dataclass { to_check } need to define a ClassVar kind!" )
@@ -191,7 +191,7 @@ def prop(field: Attribute) -> List[Json]: # type: ignore
191191 kind = meta .pop ("type_hint" , model_name (field .type ))
192192 desc = meta .pop ("description" , None )
193193 desc = desc if with_prop_description else None
194- required = meta .pop ("required" , use_optional_as_required and not is_optional (field .type )) # type: ignore
194+ required = meta .pop ("required" , use_optional_as_required and not is_optional (field .type ))
195195 synthetic = meta .pop ("synthetic" , None )
196196 synthetic = synthetic if synthetic else {}
197197 for ps in property_metadata_to_strip :
0 commit comments