Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change various type queries into faster bool type queries #14330

Merged
merged 2 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@
tuple_fallback,
)
from mypy.types import (
ANY_STRATEGY,
OVERLOAD_NAMES,
AnyType,
BoolTypeQuery,
CallableType,
DeletedType,
ErasedType,
Expand All @@ -196,7 +198,6 @@
TypedDictType,
TypeGuardedType,
TypeOfAny,
TypeQuery,
TypeTranslator,
TypeType,
TypeVarId,
Expand Down Expand Up @@ -7081,15 +7082,15 @@ def is_valid_inferred_type(typ: Type, is_lvalue_final: bool = False) -> bool:
return not typ.accept(InvalidInferredTypes())


class InvalidInferredTypes(TypeQuery[bool]):
class InvalidInferredTypes(BoolTypeQuery):
"""Find type components that are not valid for an inferred type.

These include <Erased> type, and any <nothing> types resulting from failed
(ambiguous) type inference.
"""

def __init__(self) -> None:
super().__init__(any)
super().__init__(ANY_STRATEGY)

def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
return t.ambiguous
Expand Down
20 changes: 10 additions & 10 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5106,9 +5106,9 @@ def has_any_type(t: Type, ignore_in_type_obj: bool = False) -> bool:
return t.accept(HasAnyType(ignore_in_type_obj))


class HasAnyType(types.TypeQuery[bool]):
class HasAnyType(types.BoolTypeQuery):
def __init__(self, ignore_in_type_obj: bool) -> None:
super().__init__(any)
super().__init__(types.ANY_STRATEGY)
self.ignore_in_type_obj = ignore_in_type_obj

def visit_any(self, t: AnyType) -> bool:
Expand Down Expand Up @@ -5185,7 +5185,7 @@ def replace_callable_return_type(c: CallableType, new_ret_type: Type) -> Callabl
return c.copy_modified(ret_type=new_ret_type)


class ArgInferSecondPassQuery(types.TypeQuery[bool]):
class ArgInferSecondPassQuery(types.BoolTypeQuery):
"""Query whether an argument type should be inferred in the second pass.

The result is True if the type has a type variable in a callable return
Expand All @@ -5194,17 +5194,17 @@ class ArgInferSecondPassQuery(types.TypeQuery[bool]):
"""

def __init__(self) -> None:
super().__init__(any)
super().__init__(types.ANY_STRATEGY)

def visit_callable_type(self, t: CallableType) -> bool:
return self.query_types(t.arg_types) or t.accept(HasTypeVarQuery())


class HasTypeVarQuery(types.TypeQuery[bool]):
class HasTypeVarQuery(types.BoolTypeQuery):
"""Visitor for querying whether a type has a type variable component."""

def __init__(self) -> None:
super().__init__(any)
super().__init__(types.ANY_STRATEGY)

def visit_type_var(self, t: TypeVarType) -> bool:
return True
Expand All @@ -5214,11 +5214,11 @@ def has_erased_component(t: Type | None) -> bool:
return t is not None and t.accept(HasErasedComponentsQuery())


class HasErasedComponentsQuery(types.TypeQuery[bool]):
class HasErasedComponentsQuery(types.BoolTypeQuery):
"""Visitor for querying whether a type has an erased component."""

def __init__(self) -> None:
super().__init__(any)
super().__init__(types.ANY_STRATEGY)

def visit_erased_type(self, t: ErasedType) -> bool:
return True
Expand All @@ -5228,11 +5228,11 @@ def has_uninhabited_component(t: Type | None) -> bool:
return t is not None and t.accept(HasUninhabitedComponentsQuery())


class HasUninhabitedComponentsQuery(types.TypeQuery[bool]):
class HasUninhabitedComponentsQuery(types.BoolTypeQuery):
"""Visitor for querying whether a type has an UninhabitedType component."""

def __init__(self) -> None:
super().__init__(any)
super().__init__(types.ANY_STRATEGY)

def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
return True
Expand Down
6 changes: 3 additions & 3 deletions mypy/semanal_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
TypeInfo,
)
from mypy.tvar_scope import TypeVarLikeScope
from mypy.type_visitor import TypeQuery
from mypy.type_visitor import ANY_STRATEGY, BoolTypeQuery
from mypy.types import (
TPDICT_FB_NAMES,
FunctionLike,
Expand Down Expand Up @@ -319,9 +319,9 @@ def paramspec_kwargs(
)


class HasPlaceholders(TypeQuery[bool]):
class HasPlaceholders(BoolTypeQuery):
def __init__(self) -> None:
super().__init__(any)
super().__init__(ANY_STRATEGY)

def visit_placeholder_type(self, t: PlaceholderType) -> bool:
return True
Expand Down
10 changes: 6 additions & 4 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
from mypy.tvar_scope import TypeVarLikeScope
from mypy.types import (
ANNOTATED_TYPE_NAMES,
ANY_STRATEGY,
FINAL_TYPE_NAMES,
LITERAL_TYPE_NAMES,
NEVER_NAMES,
TYPE_ALIAS_NAMES,
AnyType,
BoolTypeQuery,
CallableArgument,
CallableType,
DeletedType,
Expand Down Expand Up @@ -1944,9 +1946,9 @@ def has_any_from_unimported_type(t: Type) -> bool:
return t.accept(HasAnyFromUnimportedType())


class HasAnyFromUnimportedType(TypeQuery[bool]):
class HasAnyFromUnimportedType(BoolTypeQuery):
def __init__(self) -> None:
super().__init__(any)
super().__init__(ANY_STRATEGY)

def visit_any(self, t: AnyType) -> bool:
return t.type_of_any == TypeOfAny.from_unimported_type
Expand Down Expand Up @@ -2033,10 +2035,10 @@ def find_self_type(typ: Type, lookup: Callable[[str], SymbolTableNode | None]) -
return typ.accept(HasSelfType(lookup))


class HasSelfType(TypeQuery[bool]):
class HasSelfType(BoolTypeQuery):
def __init__(self, lookup: Callable[[str], SymbolTableNode | None]) -> None:
self.lookup = lookup
super().__init__(any)
super().__init__(ANY_STRATEGY)

def visit_unbound_type(self, t: UnboundType) -> bool:
sym = self.lookup(t.name)
Expand Down
4 changes: 2 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3301,9 +3301,9 @@ def replace_alias_tvars(
return new_tp


class HasTypeVars(TypeQuery[bool]):
class HasTypeVars(BoolTypeQuery):
def __init__(self) -> None:
super().__init__(any)
super().__init__(ANY_STRATEGY)
self.skip_alias_target = True

def visit_type_var(self, t: TypeVarType) -> bool:
Expand Down