Undocumented
Class | ProperSubtypeVisitor |
Undocumented |
Class | SubtypeVisitor |
Undocumented |
Function | are_args_compatible |
Undocumented |
Function | check_type_parameter |
Undocumented |
Function | covers_at_runtime |
Will isinstance(item, supertype) always return True at runtime? |
Function | find_member |
Find the type of member by 'name' in 'itype's TypeInfo. |
Function | find_node_type |
Find type of a variable or method 'node' (maybe also a decorated method). Apply type arguments from 'itype', and bind 'self' to 'subtype'. |
Function | flip_compat_check |
Undocumented |
Function | get_member_flags |
Detect whether a member 'name' is settable, whether it is an instance or class variable, and whether it is class or static method. |
Function | ignore_type_parameter |
Undocumented |
Function | is_callable_compatible |
Is the left compatible with the right, using the provided compatibility check? |
Function | is_equivalent |
Undocumented |
Function | is_more_precise |
Check if left is a more precise type than right. |
Function | is_proper_subtype |
Is left a proper subtype of right? |
Function | is_protocol_implementation |
Check whether 'left' implements the protocol 'right'. |
Function | is_subtype |
Is 'left' subtype of 'right'? |
Function | is_subtype_ignoring_tvars |
Undocumented |
Function | non_method_protocol_members |
Find all non-callable members of a protocol. |
Function | pop_on_exit |
Undocumented |
Function | restrict_subtype_away |
Return t minus s for runtime type assertions. |
Function | unify_generic_callable |
Try to unify a generic callable type with another callable type. |
Constant | IS_CLASS_OR_STATIC |
Undocumented |
Constant | IS_CLASSVAR |
Undocumented |
Constant | IS_SETTABLE |
Undocumented |
Constant | T |
Undocumented |
Variable | TypeParameterChecker |
Undocumented |
Function | _is_proper_subtype |
Undocumented |
Function | _is_subtype |
Undocumented |
Undocumented
Parameters | |
left:FormalArgument | Undocumented |
right:FormalArgument | Undocumented |
ignore_pos_arg_names:bool | Undocumented |
allow_partial_overlap:bool | Undocumented |
is_compat:Callable[ | Undocumented |
Returns | |
bool | Undocumented |
Find the type of member by 'name' in 'itype's TypeInfo.
Find the member type after applying type arguments from 'itype', and binding 'self' to 'subtype'. Return None if member was not found.
Parameters | |
name:str | Undocumented |
itype:Instance | Undocumented |
subtype:Type | Undocumented |
is_operator:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Detect whether a member 'name' is settable, whether it is an instance or class variable, and whether it is class or static method.
The flags are defined as following: * IS_SETTABLE: whether this attribute can be set, not set for methods and
non-settable properties;
Parameters | |
name:str | Undocumented |
info:TypeInfo | Undocumented |
Returns | |
Set[ | Undocumented |
Is the left compatible with the right, using the provided compatibility check?
If true, check if the left's args is compatible with the right's instead of the other way around (contravariantly).
This function is mostly used to check if the left is a subtype of the right which is why the default is to check the args contravariantly. However, it's occasionally useful to check the args using some other check, so we leave the variance configurable.
For example, when checking the validity of overloads, it's useful to see if the first overload alternative has more precise arguments then the second. We would want to check the arguments covariantly in that case.
Note! The following two function calls are NOT equivalent:
is_callable_compatible(f, g, is_compat=is_subtype, check_args_covariantly=False) is_callable_compatible(g, f, is_compat=is_subtype, check_args_covariantly=True)
The two calls are similar in that they both check the function arguments in
the same direction: they both run is_subtype(argument_from_g, argument_from_f)
.
However, the two calls differ in which direction they check things like keyword arguments. For example, suppose f and g are defined like so:
def f(x: int, *y: int) -> int: ... def g(x: int) -> int: ...
In this case, the first call will succeed and the second will fail: f is a valid stand-in for g but not vice-versa.
By default this function returns True if and only if all calls to left are also calls to right (with respect to the provided 'is_compat' function).
If this parameter is set to 'True', we return True if there exists at least one call to left that's also a call to right.
In other words, we perform an existential check instead of a universal one; we require left to only overlap with right instead of being a subset.
For example, suppose we set 'is_compat' to some subtype check and compare following:
f(x: float, y: str = "...", *args: bool) -> str g(*args: int) -> str
This function would normally return 'False': f is not a subtype of g. However, we would return True if this parameter is set to 'True': the two calls are compatible if the user runs "f_or_g(3)". In the context of that specific call, the two functions effectively have signatures of:
f2(float) -> str g2(int) -> str
Here, f2 is a valid subtype of g2 so we return True.
Specifically, if this parameter is set this function will:
Note: when this argument is set to True, this function becomes "symmetric" -- the following calls are equivalent:
- is_callable_compatible(f, g,
- is_compat=some_check, check_args_covariantly=False, allow_partial_overlap=True)
- is_callable_compatible(g, f,
- is_compat=some_check, check_args_covariantly=True, allow_partial_overlap=True)
If the 'some_check' function is also symmetric, the two calls would be equivalent whether or not we check the args covariantly.
Parameters | |
left:CallableType | Undocumented |
right:CallableType | Undocumented |
is_compat:Callable[ | Undocumented |
is_compat_return:Optional[ | Undocumented |
ignore_return:bool | Undocumented |
ignore_pos_arg_names:bool | Undocumented |
check_args_covariantly:bool | Undocumented |
allow_partial_overlap:bool | Undocumented |
Returns | |
bool | Undocumented |
Check if left is a more precise type than right.
A left is a proper subtype of right, left is also more precise than right. Also, if right is Any, left is more precise than right, for any left.
Parameters | |
left:Type | Undocumented |
right:Type | Undocumented |
ignore_promotions:bool | Undocumented |
Returns | |
bool | Undocumented |
Is left a proper subtype of right?
For proper subtypes, there's no need to rely on compatibility due to Any types. Every usable type is a proper subtype of itself.
If erase_instances is True, erase left instance after mapping it to supertype (this is useful for runtime isinstance() checks). If keep_erased_types is True, do not consider ErasedType a subtype of all types (used by type inference against unions).
Parameters | |
left:Type | Undocumented |
right:Type | Undocumented |
ignore_promotions:bool | Undocumented |
erase_instances:bool | Undocumented |
keep_erased_types:bool | Undocumented |
Returns | |
bool | Undocumented |
Check whether 'left' implements the protocol 'right'.
If 'proper_subtype' is True, then check for a proper subtype. Treat recursive protocols by using the 'assuming' structural subtype matrix (in sparse representation, i.e. as a list of pairs (subtype, supertype)), see also comment in nodes.TypeInfo. When we enter a check for classes (A, P), defined as following:
class P(Protocol): def f(self) -> P: ... class A: def f(self) -> A: ...
this results in A being a subtype of P without infinite recursion. On every false result, we pop the assumption, thus avoiding an infinite recursion as well.
Parameters | |
left:Instance | Undocumented |
right:Instance | Undocumented |
proper_subtype:bool | Undocumented |
Returns | |
bool | Undocumented |
Is 'left' subtype of 'right'?
Also consider Any to be a subtype of any type, and vice versa. This recursively applies to components of composite types (List[int] is subtype of List[Any], for example).
type_parameter_checker is used to check the type parameters (for example, A with B in is_subtype(C[A], C[B]). The default checks for subtype relation between the type arguments (e.g., A and B), taking the variance of the type var into account.
Parameters | |
left:Type | Undocumented |
right:Type | Undocumented |
ignore_type_params:bool | Undocumented |
ignore_pos_arg_names:bool | Undocumented |
ignore_declared_variance:bool | Undocumented |
ignore_promotions:bool | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
tp:TypeInfo | Undocumented |
Returns | |
List[ | Undocumented |
Return t minus s for runtime type assertions.
If we can't determine a precise result, return a supertype of the ideal result (just t is a valid result).
This is used for type inference of runtime type checks such as isinstance(). Currently this just removes elements of a union type.
Parameters | |
t:Type | Undocumented |
s:Type | Undocumented |
ignore_promotions:bool | Undocumented |
Returns | |
Type | Undocumented |
Try to unify a generic callable type with another callable type.
Return unified CallableType if successful; otherwise, return None.
Parameters | |
type:CallableType | Undocumented |
target:CallableType | Undocumented |
ignore_return:bool | Undocumented |
return_constraint_direction:Optional[ | Undocumented |
Returns | |
Optional[ | Undocumented |