Skip to content

Avoid infinite recursion in type queries #5434

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
Aug 8, 2018
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
12 changes: 11 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,7 @@ class TypeQuery(SyntheticTypeVisitor[T]):

def __init__(self, strategy: Callable[[Iterable[T]], T]) -> None:
self.strategy = strategy
self.seen = [] # type: List[Type]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How big do we expect seen to get? If we think it will get pretty big we could make it a set of ids of Typess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My expectation is that seen should be never larger than few dozens of types. I think we discussed this with @JukkaL when implementing ForwardReferenceResolver that uses the same pattern and decided it is "small" (whatever it means).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


def visit_unbound_type(self, t: UnboundType) -> T:
return self.query_types(t.args)
Expand Down Expand Up @@ -1984,8 +1985,17 @@ def query_types(self, types: Iterable[Type]) -> T:
"""Perform a query for a list of types.

Use the strategy to combine the results.
Skip types already visited types to avoid infinite recursion.
Note: types can be recursive until they are fully analyzed and "unentangled"
in patches after the semantic analysis.
"""
return self.strategy(t.accept(self) for t in types)
res = [] # type: List[T]
for t in types:
if any(t is s for s in self.seen):
continue
self.seen.append(t)
res.append(t.accept(self))
return self.strategy(res)


def strip_type(typ: Type) -> Type:
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,29 @@ ignore_missing_imports = True
always_true = YOLO1, YOLO
always_false = BLAH, BLAH1
[builtins fixtures/bool.pyi]

[case testCheckDisallowAnyGenericsNamedTuple]
# flags: --disallow-any-generics
from typing import NamedTuple

N = NamedTuple('N', [('x', N)]) # type: ignore
n: N
[out]

[case testCheckDisallowAnyGenericsTypedDict]
# flags: --disallow-any-generics
from typing import Dict, Any, Optional
from mypy_extensions import TypedDict

VarsDict = Dict[str, Any]
HostsDict = Dict[str, Optional[VarsDict]]

GroupDataDict = TypedDict( # type: ignore
"GroupDataDict", {"children": "GroupsDict",
"vars": VarsDict,
"hosts": HostsDict}, total=False
)

GroupsDict = Dict[str, GroupDataDict] # type: ignore
[builtins fixtures/dict.pyi]
[out]
2 changes: 1 addition & 1 deletion test-data/unit/fixtures/dict.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ class float: pass
class bool: pass

class ellipsis: pass
def isinstance(x: object, t: Union[type, Tuple]) -> bool: pass
def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
class BaseException: pass