module documentation
(source)

Undocumented

Class ​Type​Meet​Visitor Undocumented
Function adjust​_tuple Find out if left is a Tuple[A, ...], and adjust its length to right
Function are​_tuples​_overlapping Returns true if left and right are overlapping tuples.
Function are​_typed​_dicts​_overlapping Returns 'true' if left and right are overlapping TypeDictTypes.
Function get​_possible​_variants This function takes any "Union-like" type and returns a list of the available "options".
Function is​_overlapping​_erased​_types The same as 'is_overlapping_erased_types', except the types are erased first.
Function is​_overlapping​_types Can a value of type 'left' also be of type 'right' or vice-versa?
Function is​_tuple Undocumented
Function meet​_similar​_callables Undocumented
Function meet​_type​_list Undocumented
Function meet​_types Return the greatest lower bound of two types.
Function narrow​_declared​_type Return the declared type narrowed down to another type.
Function trivial​_meet Return one of types (expanded) if it is a subtype of other, otherwise bottom type.
Function typed​_dict​_mapping​_overlap Check if a TypedDict type is overlapping with a Mapping.
Function typed​_dict​_mapping​_pair Is this a pair where one type is a TypedDict and another one is an instance of Mapping?
def adjust_tuple(left, r): (source)
Find out if left is a Tuple[A, ...], and adjust its length to right
Parameters
left:ProperTypeUndocumented
r:ProperTypeUndocumented
Returns
Optional[TupleType]Undocumented
def are_tuples_overlapping(left, right, *, ignore_promotions=False, prohibit_none_typevar_overlap=False): (source)
Returns true if left and right are overlapping tuples.
Parameters
left:TypeUndocumented
right:TypeUndocumented
ignore​_promotions:boolUndocumented
prohibit​_none​_typevar​_overlap:boolUndocumented
Returns
boolUndocumented
def are_typed_dicts_overlapping(left, right, *, ignore_promotions=False, prohibit_none_typevar_overlap=False): (source)
Returns 'true' if left and right are overlapping TypeDictTypes.
Parameters
left:TypedDictTypeUndocumented
right:TypedDictTypeUndocumented
ignore​_promotions:boolUndocumented
prohibit​_none​_typevar​_overlap:boolUndocumented
Returns
boolUndocumented
def get_possible_variants(typ): (source)

This function takes any "Union-like" type and returns a list of the available "options".

Specifically, there are currently exactly three different types that can have "variants" or are "union-like":

  • Unions
  • TypeVars with value restrictions
  • Overloads

This function will return a list of each "option" present in those types.

If this function receives any other type, we return a list containing just that original type. (E.g. pretend the type was contained within a singleton union).

The only exception is regular TypeVars: we return a list containing that TypeVar's upper bound.

This function is useful primarily when checking to see if two types are overlapping: the algorithm to check if two unions are overlapping is fundamentally the same as the algorithm for checking if two overloads are overlapping.

Normalizing both kinds of types in the same way lets us reuse the same algorithm for both.

Parameters
typ:TypeUndocumented
Returns
List[Type]Undocumented
def is_overlapping_erased_types(left, right, *, ignore_promotions=False): (source)
The same as 'is_overlapping_erased_types', except the types are erased first.
Parameters
left:TypeUndocumented
right:TypeUndocumented
ignore​_promotions:boolUndocumented
Returns
boolUndocumented
def is_overlapping_types(left, right, ignore_promotions=False, prohibit_none_typevar_overlap=False): (source)

Can a value of type 'left' also be of type 'right' or vice-versa?

If 'ignore_promotions' is True, we ignore promotions while checking for overlaps. If 'prohibit_none_typevar_overlap' is True, we disallow None from overlapping with TypeVars (in both strict-optional and non-strict-optional mode).

Parameters
left:TypeUndocumented
right:TypeUndocumented
ignore​_promotions:boolUndocumented
prohibit​_none​_typevar​_overlap:boolUndocumented
Returns
boolUndocumented
def is_tuple(typ): (source)

Undocumented

Parameters
typ:TypeUndocumented
Returns
boolUndocumented
def meet_similar_callables(t, s): (source)

Undocumented

Parameters
t:CallableTypeUndocumented
s:CallableTypeUndocumented
Returns
CallableTypeUndocumented
def meet_type_list(types): (source)

Undocumented

Parameters
types:List[Type]Undocumented
Returns
TypeUndocumented
def meet_types(s, t): (source)
Return the greatest lower bound of two types.
Parameters
s:TypeUndocumented
t:TypeUndocumented
Returns
ProperTypeUndocumented
def narrow_declared_type(declared, narrowed): (source)
Return the declared type narrowed down to another type.
Parameters
declared:TypeUndocumented
narrowed:TypeUndocumented
Returns
TypeUndocumented
def trivial_meet(s, t): (source)
Return one of types (expanded) if it is a subtype of other, otherwise bottom type.
Parameters
s:TypeUndocumented
t:TypeUndocumented
Returns
ProperTypeUndocumented
def typed_dict_mapping_overlap(left, right, overlapping): (source)

Check if a TypedDict type is overlapping with a Mapping.

The basic logic here consists of two rules:

  • A TypedDict with some required keys is overlapping with Mapping[str, <some type>] if and only if every key type is overlapping with <some type>. For example:

    • TypedDict(x=int, y=str) overlaps with Dict[str, Union[str, int]]
    • TypedDict(x=int, y=str) doesn't overlap with Dict[str, int]

    Note that any additional non-required keys can't change the above result.

  • A TypedDict with no required keys overlaps with Mapping[str, <some type>] if and only if at least one of key types overlaps with <some type>. For example:

    • TypedDict(x=str, y=str, total=False) overlaps with Dict[str, str]
    • TypedDict(x=str, y=str, total=False) doesn't overlap with Dict[str, int]
    • TypedDict(x=int, y=str, total=False) overlaps with Dict[str, str]

As usual empty, dictionaries lie in a gray area. In general, List[str] and List[str] are considered non-overlapping despite empty list belongs to both. However, List[int] and List[<nothing>] are considered overlapping.

So here we follow the same logic: a TypedDict with no required keys is considered non-overlapping with Mapping[str, <some type>], but is considered overlapping with Mapping[<nothing>, <nothing>]. This way we avoid false positives for overloads, and also avoid false positives for comparisons like SomeTypedDict == {} under --strict-equality.

Parameters
left:TypeUndocumented
right:TypeUndocumented
overlapping:Callable[[Type, Type], bool]Undocumented
Returns
boolUndocumented
def typed_dict_mapping_pair(left, right): (source)

Is this a pair where one type is a TypedDict and another one is an instance of Mapping?

This case requires a precise/principled consideration because there are two use cases that push the boundary the opposite ways: we need to avoid spurious overlaps to avoid false positives for overloads, but we also need to avoid spuriously non-overlapping types to avoid false positives with --strict-equality.

Parameters
left:TypeUndocumented
right:TypeUndocumented
Returns
boolUndocumented