Skip to content

Commit 7ed4f5e

Browse files
authored
Speed up recursive type check (#14326)
Use a faster type query visitor and reuse visitor across calls. This should speed up type checking slightly. My measurements show a ~0.5% improvement, but it may be below the noise floor.
1 parent b5fc748 commit 7ed4f5e

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

mypy/types.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -3321,17 +3321,22 @@ def has_type_vars(typ: Type) -> bool:
33213321
return typ.accept(HasTypeVars())
33223322

33233323

3324-
class HasRecursiveType(TypeQuery[bool]):
3324+
class HasRecursiveType(BoolTypeQuery):
33253325
def __init__(self) -> None:
3326-
super().__init__(any)
3326+
super().__init__(ANY_STRATEGY)
33273327

33283328
def visit_type_alias_type(self, t: TypeAliasType) -> bool:
33293329
return t.is_recursive or self.query_types(t.args)
33303330

33313331

3332+
# Use singleton since this is hot (note: call reset() before using)
3333+
_has_recursive_type: Final = HasRecursiveType()
3334+
3335+
33323336
def has_recursive_types(typ: Type) -> bool:
33333337
"""Check if a type contains any recursive aliases (recursively)."""
3334-
return typ.accept(HasRecursiveType())
3338+
_has_recursive_type.reset()
3339+
return typ.accept(_has_recursive_type)
33353340

33363341

33373342
def flatten_nested_unions(

0 commit comments

Comments
 (0)