Skip to content

Commit 2d5108b

Browse files
authored
Change various type queries into faster bool type queries (#14330)
I measured a 1% performance improvement in self check.
1 parent cb1d1a0 commit 2d5108b

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

mypy/checker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@
177177
tuple_fallback,
178178
)
179179
from mypy.types import (
180+
ANY_STRATEGY,
180181
OVERLOAD_NAMES,
181182
AnyType,
183+
BoolTypeQuery,
182184
CallableType,
183185
DeletedType,
184186
ErasedType,
@@ -196,7 +198,6 @@
196198
TypedDictType,
197199
TypeGuardedType,
198200
TypeOfAny,
199-
TypeQuery,
200201
TypeTranslator,
201202
TypeType,
202203
TypeVarId,
@@ -7134,15 +7135,15 @@ def is_valid_inferred_type(typ: Type, is_lvalue_final: bool = False) -> bool:
71347135
return not typ.accept(InvalidInferredTypes())
71357136

71367137

7137-
class InvalidInferredTypes(TypeQuery[bool]):
7138+
class InvalidInferredTypes(BoolTypeQuery):
71387139
"""Find type components that are not valid for an inferred type.
71397140
71407141
These include <Erased> type, and any <nothing> types resulting from failed
71417142
(ambiguous) type inference.
71427143
"""
71437144

71447145
def __init__(self) -> None:
7145-
super().__init__(any)
7146+
super().__init__(ANY_STRATEGY)
71467147

71477148
def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
71487149
return t.ambiguous

mypy/checkexpr.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5106,9 +5106,9 @@ def has_any_type(t: Type, ignore_in_type_obj: bool = False) -> bool:
51065106
return t.accept(HasAnyType(ignore_in_type_obj))
51075107

51085108

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

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

51875187

5188-
class ArgInferSecondPassQuery(types.TypeQuery[bool]):
5188+
class ArgInferSecondPassQuery(types.BoolTypeQuery):
51895189
"""Query whether an argument type should be inferred in the second pass.
51905190
51915191
The result is True if the type has a type variable in a callable return
@@ -5194,17 +5194,17 @@ class ArgInferSecondPassQuery(types.TypeQuery[bool]):
51945194
"""
51955195

51965196
def __init__(self) -> None:
5197-
super().__init__(any)
5197+
super().__init__(types.ANY_STRATEGY)
51985198

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

52025202

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

52065206
def __init__(self) -> None:
5207-
super().__init__(any)
5207+
super().__init__(types.ANY_STRATEGY)
52085208

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

52165216

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

52205220
def __init__(self) -> None:
5221-
super().__init__(any)
5221+
super().__init__(types.ANY_STRATEGY)
52225222

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

52305230

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

52345234
def __init__(self) -> None:
5235-
super().__init__(any)
5235+
super().__init__(types.ANY_STRATEGY)
52365236

52375237
def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
52385238
return True

mypy/semanal_shared.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
TypeInfo,
2222
)
2323
from mypy.tvar_scope import TypeVarLikeScope
24-
from mypy.type_visitor import TypeQuery
24+
from mypy.type_visitor import ANY_STRATEGY, BoolTypeQuery
2525
from mypy.types import (
2626
TPDICT_FB_NAMES,
2727
FunctionLike,
@@ -319,9 +319,9 @@ def paramspec_kwargs(
319319
)
320320

321321

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

326326
def visit_placeholder_type(self, t: PlaceholderType) -> bool:
327327
return True

mypy/typeanal.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@
4242
from mypy.tvar_scope import TypeVarLikeScope
4343
from mypy.types import (
4444
ANNOTATED_TYPE_NAMES,
45+
ANY_STRATEGY,
4546
FINAL_TYPE_NAMES,
4647
LITERAL_TYPE_NAMES,
4748
NEVER_NAMES,
4849
TYPE_ALIAS_NAMES,
4950
AnyType,
51+
BoolTypeQuery,
5052
CallableArgument,
5153
CallableType,
5254
DeletedType,
@@ -1944,9 +1946,9 @@ def has_any_from_unimported_type(t: Type) -> bool:
19441946
return t.accept(HasAnyFromUnimportedType())
19451947

19461948

1947-
class HasAnyFromUnimportedType(TypeQuery[bool]):
1949+
class HasAnyFromUnimportedType(BoolTypeQuery):
19481950
def __init__(self) -> None:
1949-
super().__init__(any)
1951+
super().__init__(ANY_STRATEGY)
19501952

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

20352037

2036-
class HasSelfType(TypeQuery[bool]):
2038+
class HasSelfType(BoolTypeQuery):
20372039
def __init__(self, lookup: Callable[[str], SymbolTableNode | None]) -> None:
20382040
self.lookup = lookup
2039-
super().__init__(any)
2041+
super().__init__(ANY_STRATEGY)
20402042

20412043
def visit_unbound_type(self, t: UnboundType) -> bool:
20422044
sym = self.lookup(t.name)

mypy/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,9 +3305,9 @@ def replace_alias_tvars(
33053305
return new_tp
33063306

33073307

3308-
class HasTypeVars(TypeQuery[bool]):
3308+
class HasTypeVars(BoolTypeQuery):
33093309
def __init__(self) -> None:
3310-
super().__init__(any)
3310+
super().__init__(ANY_STRATEGY)
33113311
self.skip_alias_target = True
33123312

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

0 commit comments

Comments
 (0)