module documentation

Undocumented

Class TypeMeetVisitor 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_enum_overlapping_union Return True if x is an Enum, and y is an Union with at least one Literal from x
Function is_literal_in_union Return True if x is a Literal and y is an Union that includes x
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: ProperType, r: ProperType) -> TupleType|None: (source)

Find out if `left` is a Tuple[A, ...], and adjust its length to `right`

def are_tuples_overlapping(left: Type, right: Type, *, ignore_promotions: bool = False, prohibit_none_typevar_overlap: bool = False) -> bool: (source)

Returns true if left and right are overlapping tuples.

def are_typed_dicts_overlapping(left: TypedDictType, right: TypedDictType, *, ignore_promotions: bool = False, prohibit_none_typevar_overlap: bool = False) -> bool: (source)

Returns 'true' if left and right are overlapping TypeDictTypes.

def get_possible_variants(typ: Type) -> list[Type]: (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 current exceptions are regular TypeVars and ParamSpecs. For these "TypeVarLike"s, we return a list containing that TypeVarLike'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.

def is_enum_overlapping_union(x: ProperType, y: ProperType) -> bool: (source)

Return True if x is an Enum, and y is an Union with at least one Literal from x

def is_literal_in_union(x: ProperType, y: ProperType) -> bool: (source)

Return True if x is a Literal and y is an Union that includes x

def is_overlapping_erased_types(left: Type, right: Type, *, ignore_promotions: bool = False) -> bool: (source)

The same as 'is_overlapping_erased_types', except the types are erased first.

def is_overlapping_types(left: Type, right: Type, ignore_promotions: bool = False, prohibit_none_typevar_overlap: bool = False, ignore_uninhabited: bool = False) -> bool: (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).

def is_tuple(typ: Type) -> bool: (source)

Undocumented

def meet_similar_callables(t: CallableType, s: CallableType) -> CallableType: (source)

Undocumented

def meet_type_list(types: list[Type]) -> Type: (source)

Undocumented

def meet_types(s: Type, t: Type) -> ProperType: (source)

Return the greatest lower bound of two types.

def narrow_declared_type(declared: Type, narrowed: Type) -> Type: (source)

Return the declared type narrowed down to another type.

def trivial_meet(s: Type, t: Type) -> ProperType: (source)

Return one of types (expanded) if it is a subtype of other, otherwise bottom type.

def typed_dict_mapping_overlap(left: Type, right: Type, overlapping: Callable[[Type, Type], bool]) -> bool: (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.

def typed_dict_mapping_pair(left: Type, right: Type) -> bool: (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.