Skip to content

Speed up recursive type check #14326

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

Merged
merged 1 commit into from
Dec 20, 2022
Merged
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
11 changes: 8 additions & 3 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3321,17 +3321,22 @@ def has_type_vars(typ: Type) -> bool:
return typ.accept(HasTypeVars())


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

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


# Use singleton since this is hot (note: call reset() before using)
_has_recursive_type: Final = HasRecursiveType()


def has_recursive_types(typ: Type) -> bool:
"""Check if a type contains any recursive aliases (recursively)."""
return typ.accept(HasRecursiveType())
_has_recursive_type.reset()
return typ.accept(_has_recursive_type)


def flatten_nested_unions(
Expand Down