Miscellaneous type operations and helpers for use during type checking.
Class | TypeVarExtractor |
Undocumented |
Function | bind_self |
Return a copy of method , with the type of its first parameter (usually self or cls) bound to original_type. |
Function | callable_corresponding_argument |
Return the argument a function that corresponds to model |
Function | callable_type |
Undocumented |
Function | class_callable |
Create a type object type based on the signature of __init__. |
Function | coerce_to_literal |
Recursively converts any Instances that have a last_known_value or are instances of enum types with a single value into the corresponding LiteralType. |
Function | custom_special_method |
Does this type have a custom special method such as __format__() or __eq__()? |
Function | erase_def_to_union_or_bound |
Undocumented |
Function | erase_to_bound |
Undocumented |
Function | erase_to_union_or_bound |
Undocumented |
Function | false_only |
Restricted version of t with only False-ish values |
Function | function_type |
Undocumented |
Function | get_enum_values |
Return the list of values for an Enum. |
Function | get_type_vars |
Undocumented |
Function | is_literal_type_like |
Returns 'true' if the given type context is potentially either a LiteralType, a Union of LiteralType, or something similar. |
Function | is_recursive_pair |
Is this a pair of recursive type aliases? |
Function | is_redundant_literal_instance |
Undocumented |
Function | is_simple_literal |
Whether a type is a simple enough literal to allow for fast Union simplification |
Function | is_singleton_type |
Returns 'true' if this type is a "singleton type" -- if there exists exactly only one runtime value associated with this type. |
Function | make_simplified_union |
Build union type with redundant union items removed. |
Function | map_type_from_supertype |
Map type variables in a type defined in a supertype context to be valid in the subtype context. Assume that the result is unique; if more than one type is possible, return one of the alternatives. |
Function | supported_self_type |
Is this a supported kind of explicit self-types? |
Function | true_only |
Restricted version of t with only True-ish values |
Function | true_or_false |
Unrestricted version of t with both True-ish and False-ish values |
Function | try_contracting_literals_in_union |
Contracts any literal types back into a sum type if possible. |
Function | try_expanding_sum_type_to_union |
Attempts to recursively expand any enum Instances with the given target_fullname into a Union of all of its component LiteralTypes. |
Function | try_getting_instance_fallback |
Returns the Instance fallback for this type if one exists. |
Function | try_getting_int_literals_from_type |
If the given expression or type corresponds to an int Literal or a union of int Literals, returns a list of the underlying ints. Otherwise, returns None. |
Function | try_getting_literals_from_type |
No summary |
Function | try_getting_str_literals |
If the given expression or type corresponds to a string literal or a union of string literals, returns a list of the underlying strings. Otherwise, returns None. |
Function | try_getting_str_literals_from_type |
If the given expression or type corresponds to a string Literal or a union of string Literals, returns a list of the underlying strings. Otherwise, returns None. |
Function | tuple_fallback |
Return fallback type for a tuple. |
Function | type_object_type_from_function |
Undocumented |
Constant | F |
Undocumented |
Constant | T |
Undocumented |
Function | _get_type_special_method_bool_ret_type |
Undocumented |
Return a copy of method
, with the type of its first parameter (usually
self or cls) bound to original_type.
If the type of self
is a generic type (T, or Type[T] for classmethods),
instantiate every occurrence of type with original_type in the rest of the
signature and in the return type.
original_type is the type of E in the expression E.copy(). It is None in compatibility checks. In this case we treat it as the erasure of the declared type of self.
This way we can express "the type of self". For example:
T = TypeVar('T', bound='A') class A:
def copy(self: T) -> T: ...
class B(A): pass
b = B().copy() # type: B
Parameters | |
method:F | Undocumented |
original_type:Optional[ | Undocumented |
is_classmethod:bool | Undocumented |
Returns | |
F | Undocumented |
model
Parameters | |
typ:CallableType | Undocumented |
model:FormalArgument | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
fdef:FuncItem | Undocumented |
fallback:Instance | Undocumented |
ret_type:Optional[ | Undocumented |
Returns | |
CallableType | Undocumented |
Parameters | |
init_type:CallableType | Undocumented |
info:TypeInfo | Undocumented |
type_type:Instance | Undocumented |
special_sig:Optional[ | Undocumented |
is_new:bool | Undocumented |
orig_self_type:Optional[ | Undocumented |
Returns | |
CallableType | Undocumented |
Does this type have a custom special method such as __format__() or __eq__()?
If check_all is True ensure all items of a union have a custom method, not just some.
Parameters | |
typ:Type | Undocumented |
name:str | Undocumented |
check_all:bool | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
t:Type | Undocumented |
Returns | |
ProperType | Undocumented |
Undocumented
Parameters | |
func:FuncBase | Undocumented |
fallback:Instance | Undocumented |
Returns | |
FunctionLike | Undocumented |
Parameters | |
typ:Instance | Undocumented |
Returns | |
List[ | Undocumented |
Parameters | |
t:Optional[ | Undocumented |
Returns | |
bool | Undocumented |
Undocumented
Parameters | |
general:ProperType | Undocumented |
specific:ProperType | Undocumented |
Returns | |
bool | Undocumented |
Whether a type is a simple enough literal to allow for fast Union simplification
For now this means enum or string
Parameters | |
t:ProperType | Undocumented |
Returns | |
bool | Undocumented |
Returns 'true' if this type is a "singleton type" -- if there exists exactly only one runtime value associated with this type.
That is, given two values 'a' and 'b' that have the same type 't', 'is_singleton_type(t)' returns True if and only if the expression 'a is b' is always true.
Currently, this returns True when given NoneTypes, enum LiteralTypes and enum types with a single value.
Note that other kinds of LiteralTypes cannot count as singleton types. For example, suppose we do 'a = 100000 + 1' and 'b = 100001'. It is not guaranteed that 'a is b' will always be true -- some implementations of Python will end up constructing two distinct instances of 100001.
Parameters | |
typ:Type | Undocumented |
Returns | |
bool | Undocumented |
Build union type with redundant union items removed.
If only a single item remains, this may return a non-union type.
Examples:
The keep_erased flag is used for type inference against union types containing type variables. If set to True, keep all ErasedType items.
Parameters | |
items:Sequence[ | Undocumented |
line:int | Undocumented |
column:int | Undocumented |
keep_erased:bool | Undocumented |
contract_literals:bool | Undocumented |
Returns | |
ProperType | Undocumented |
Map type variables in a type defined in a supertype context to be valid in the subtype context. Assume that the result is unique; if more than one type is possible, return one of the alternatives.
For example, assume
class D(Generic[S]): ... class C(D[E[T]], Generic[T]): ...
Now S in the context of D would be mapped to E[T] in the context of C.
Parameters | |
typ:Type | Undocumented |
sub_info:TypeInfo | Undocumented |
super_info:TypeInfo | Undocumented |
Returns | |
Type | Undocumented |
Is this a supported kind of explicit self-types?
Currently, this means a X or Type[X], where X is an instance or a type variable with an instance upper bound.
Parameters | |
typ:ProperType | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
t:Type | Undocumented |
Returns | |
ProperType | Undocumented |
Parameters | |
t:Type | Undocumented |
Returns | |
ProperType | Undocumented |
Contracts any literal types back into a sum type if possible.
Will replace the first instance of the literal with the sum type and remove all others.
If we call try_contracting_union(Literal[Color.RED, Color.BLUE, Color.YELLOW])
,
this function will return Color.
We also treat Literal[True, False]
as bool
.
Parameters | |
types:Sequence[ | Undocumented |
Returns | |
List[ | Undocumented |
Attempts to recursively expand any enum Instances with the given target_fullname into a Union of all of its component LiteralTypes.
For example, if we have:
- class Color(Enum):
- RED = 1 BLUE = 2 YELLOW = 3
- class Status(Enum):
- SUCCESS = 1 FAILURE = 2 UNKNOWN = 3
...and if we call try_expanding_enum_to_union(Union[Color, Status], 'module.Color')
,
this function will return Literal[Color.RED, Color.BLUE, Color.YELLOW, Status].
Parameters | |
typ:Type | Undocumented |
target_fullname:str | Undocumented |
Returns | |
ProperType | Undocumented |
Returns the Instance fallback for this type if one exists.
Otherwise, returns None.
Parameters | |
typ:ProperType | Undocumented |
Returns | |
Optional[ | Undocumented |
If the given expression or type corresponds to an int Literal or a union of int Literals, returns a list of the underlying ints. Otherwise, returns None.
For example, if we had the type 'Literal[1, 2, 3]' as input, this function would return a list of ints [1, 2, 3].
Parameters | |
typ:Type | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
typ:Type | Undocumented |
target_literal_type:TypingType[ | Undocumented |
target_fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
If the given expression or type corresponds to a string literal or a union of string literals, returns a list of the underlying strings. Otherwise, returns None.
Specifically, this function is guaranteed to return a list with one or more strings if one one the following is true:
Parameters | |
expr:Expression | Undocumented |
typ:Type | Undocumented |
Returns | |
Optional[ | Undocumented |
If the given expression or type corresponds to a string Literal or a union of string Literals, returns a list of the underlying strings. Otherwise, returns None.
For example, if we had the type 'Literal["foo", "bar"]' as input, this function would return a list of strings ["foo", "bar"].
Parameters | |
typ:Type | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
signature:FunctionLike | Undocumented |
info:TypeInfo | Undocumented |
def_info:TypeInfo | Undocumented |
fallback:Instance | Undocumented |
is_new:bool | Undocumented |
Returns | |
FunctionLike | Undocumented |