@@ -903,6 +903,31 @@ class Instance(ProperType):
903
903
"""An instance type of form C[T1, ..., Tn].
904
904
905
905
The list of type variables may be empty.
906
+
907
+ Several types has fallbacks to `Instance`. Why?
908
+ Because, for example `TupleTuple` is related to `builtins.tuple` instance.
909
+ And `FunctionLike` has `builtins.function` fallback.
910
+ This allows us to use types defined
911
+ in typeshed for our "special" and more precise types.
912
+
913
+ We used to have this helper function to get a fallback from different types.
914
+ Note, that it might be incomplete, since it is not used and not updated.
915
+ It just illustrates the concept:
916
+
917
+ def try_getting_instance_fallback(typ: ProperType) -> Optional[Instance]:
918
+ '''Returns the Instance fallback for this type if one exists or None.'''
919
+ if isinstance(typ, Instance):
920
+ return typ
921
+ elif isinstance(typ, TupleType):
922
+ return tuple_fallback(typ)
923
+ elif isinstance(typ, TypedDictType):
924
+ return typ.fallback
925
+ elif isinstance(typ, FunctionLike):
926
+ return typ.fallback
927
+ elif isinstance(typ, LiteralType):
928
+ return typ.fallback
929
+ return None
930
+
906
931
"""
907
932
908
933
__slots__ = ('type' , 'args' , 'erased' , 'invalid' , 'type_ref' , 'last_known_value' )
@@ -1033,10 +1058,11 @@ class FunctionLike(ProperType):
1033
1058
1034
1059
__slots__ = ('fallback' ,)
1035
1060
1061
+ fallback : Instance
1062
+
1036
1063
def __init__ (self , line : int = - 1 , column : int = - 1 ) -> None :
1037
1064
super ().__init__ (line , column )
1038
1065
self .can_be_false = False
1039
- self .fallback : Instance
1040
1066
1041
1067
@abstractmethod
1042
1068
def is_type_obj (self ) -> bool : pass
@@ -1639,7 +1665,7 @@ def copy_modified(self, *, fallback: Optional[Instance] = None,
1639
1665
required_keys = self .required_keys
1640
1666
return TypedDictType (items , required_keys , fallback , self .line , self .column )
1641
1667
1642
- def create_anonymous_fallback (self , * , value_type : Type ) -> Instance :
1668
+ def create_anonymous_fallback (self ) -> Instance :
1643
1669
anonymous = self .as_anonymous ()
1644
1670
return anonymous .fallback
1645
1671
0 commit comments