@@ -359,7 +359,7 @@ def can_use_in_object_var(cls: GenericType) -> bool:
359
359
return all (can_use_in_object_var (t ) for t in types .get_args (cls ))
360
360
return (
361
361
inspect .isclass (cls )
362
- and not issubclass (cls , Var )
362
+ and not safe_issubclass (cls , Var )
363
363
and serializers .can_serialize (cls , dict )
364
364
)
365
365
@@ -796,7 +796,7 @@ def to(
796
796
797
797
if inspect .isclass (output ):
798
798
for var_subclass in _var_subclasses [::- 1 ]:
799
- if issubclass (output , var_subclass .var_subclass ):
799
+ if safe_issubclass (output , var_subclass .var_subclass ):
800
800
current_var_type = self ._var_type
801
801
if current_var_type is Any :
802
802
new_var_type = var_type
@@ -808,7 +808,7 @@ def to(
808
808
return to_operation_return # pyright: ignore [reportReturnType]
809
809
810
810
# If we can't determine the first argument, we just replace the _var_type.
811
- if not issubclass (output , Var ) or var_type is None :
811
+ if not safe_issubclass (output , Var ) or var_type is None :
812
812
return dataclasses .replace (
813
813
self ,
814
814
_var_type = output ,
@@ -850,7 +850,6 @@ def guess_type(self) -> Var:
850
850
Raises:
851
851
TypeError: If the type is not supported for guessing.
852
852
"""
853
- from .number import NumberVar
854
853
from .object import ObjectVar
855
854
856
855
var_type = self ._var_type
@@ -868,11 +867,20 @@ def guess_type(self) -> Var:
868
867
869
868
if fixed_type in types .UnionTypes :
870
869
inner_types = get_args (var_type )
870
+ non_optional_inner_types = [
871
+ types .value_inside_optional (inner_type ) for inner_type in inner_types
872
+ ]
873
+ fixed_inner_types = [
874
+ get_origin (inner_type ) or inner_type
875
+ for inner_type in non_optional_inner_types
876
+ ]
871
877
872
- if all (
873
- inspect .isclass (t ) and issubclass (t , (int , float )) for t in inner_types
874
- ):
875
- return self .to (NumberVar , self ._var_type )
878
+ for var_subclass in _var_subclasses [::- 1 ]:
879
+ if all (
880
+ safe_issubclass (t , var_subclass .python_types )
881
+ for t in fixed_inner_types
882
+ ):
883
+ return self .to (var_subclass .var_subclass , self ._var_type )
876
884
877
885
if can_use_in_object_var (var_type ):
878
886
return self .to (ObjectVar , self ._var_type )
@@ -890,7 +898,7 @@ def guess_type(self) -> Var:
890
898
return self .to (None )
891
899
892
900
for var_subclass in _var_subclasses [::- 1 ]:
893
- if issubclass (fixed_type , var_subclass .python_types ):
901
+ if safe_issubclass (fixed_type , var_subclass .python_types ):
894
902
return self .to (var_subclass .var_subclass , self ._var_type )
895
903
896
904
if can_use_in_object_var (fixed_type ):
@@ -918,17 +926,17 @@ def _get_default_value(self) -> Any:
918
926
if type_ is Literal :
919
927
args = get_args (self ._var_type )
920
928
return args [0 ] if args else None
921
- if issubclass (type_ , str ):
929
+ if safe_issubclass (type_ , str ):
922
930
return ""
923
- if issubclass (type_ , types .get_args (int | float )):
931
+ if safe_issubclass (type_ , types .get_args (int | float )):
924
932
return 0
925
- if issubclass (type_ , bool ):
933
+ if safe_issubclass (type_ , bool ):
926
934
return False
927
- if issubclass (type_ , list ):
935
+ if safe_issubclass (type_ , list ):
928
936
return []
929
- if issubclass (type_ , Mapping ):
937
+ if safe_issubclass (type_ , Mapping ):
930
938
return {}
931
- if issubclass (type_ , tuple ):
939
+ if safe_issubclass (type_ , tuple ):
932
940
return ()
933
941
if types .is_dataframe (type_ ):
934
942
try :
@@ -939,7 +947,7 @@ def _get_default_value(self) -> Any:
939
947
raise ImportError (
940
948
"Please install pandas to use dataframes in your app."
941
949
) from e
942
- return set () if issubclass (type_ , set ) else None
950
+ return set () if safe_issubclass (type_ , set ) else None
943
951
944
952
def _get_setter_name (self , include_state : bool = True ) -> str :
945
953
"""Get the name of the var's generated setter function.
@@ -1412,7 +1420,7 @@ def __init_subclass__(cls, **kwargs):
1412
1420
possible_bases = [
1413
1421
base
1414
1422
for base in bases_normalized
1415
- if issubclass (base , Var ) and base != LiteralVar
1423
+ if safe_issubclass (base , Var ) and base != LiteralVar
1416
1424
]
1417
1425
1418
1426
if not possible_bases :
@@ -2706,7 +2714,7 @@ def create(
2706
2714
2707
2715
def var_operation_return (
2708
2716
js_expression : str ,
2709
- var_type : Type [RETURN ] | None = None ,
2717
+ var_type : Type [RETURN ] | GenericType | None = None ,
2710
2718
var_data : VarData | None = None ,
2711
2719
) -> CustomVarOperationReturn [RETURN ]:
2712
2720
"""Shortcut for creating a CustomVarOperationReturn.
0 commit comments