Skip to content

Commit

Permalink
Change various type queries into faster bool type queries (#14330)
Browse files Browse the repository at this point in the history
I measured a 1% performance improvement in self check.
  • Loading branch information
JukkaL authored Dec 21, 2022
1 parent cb1d1a0 commit 2d5108b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
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 @@ -7134,15 +7135,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 @@ -3305,9 +3305,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

0 comments on commit 2d5108b

Please sign in to comment.