-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix crashes and fails in forward references #3952
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
Changes from 1 commit
45e5931
cb4caa5
1cdc980
260ef02
a58a217
950a022
411b24d
b9b8528
48d6de4
ec45441
ac32ed4
3fb3019
f9b1320
cf014b8
665236b
9a318aa
757fbd9
4502ce2
3b39d40
c8b28fe
9f92b0f
9779103
b914bdb
3568fdb
54d9331
b9ddacc
5bfe9ca
a2912e9
10c65b8
21dfbfe
f2ddbcd
03597ee
649ef32
83f8907
13c7176
79b10d6
321a809
076c909
c1a63ec
97e6f47
8f52654
6edd078
514b8bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4670,6 +4670,9 @@ def visit_any(self, t: AnyType) -> Type: | |
|
||
|
||
class TypeReplacer(TypeTranslator): | ||
"""This is very similar TypeTranslator but tracks visited nodes to avoid | ||
infinite recursion on potentially circular (self- or mutually-referential) types. | ||
""" | ||
def __init__(self): | ||
self.seen = [] # type: List[Type] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually does not work, I get some weird failures, when I change this to a set. I am not sure why, but my guess is that Python reuses |
||
|
||
|
@@ -4736,13 +4739,30 @@ def visit_type_type(self, t: TypeType) -> Type: | |
|
||
|
||
class ForwardRefRemover(TypeReplacer): | ||
"""This visitor tracks situations like this: | ||
|
||
x: A # this type is not yet known and therefore wrapped in ForwardRef | ||
# it's content is updated in ThirdPass, now we need to unwrap this type. | ||
A = NewType('A', int) | ||
""" | ||
def visit_forwardref_type(self, t: ForwardRef) -> Type: | ||
return t.link.accept(self) | ||
|
||
|
||
class SyntheticReplacer(ForwardRefRemover): | ||
"""This visitor tracks situations like this: | ||
|
||
x: A # when analyzing this type we will get an Instance from FirstPass | ||
# now we need to update this to actual analyzed TupleType. | ||
class A(NamedTuple): | ||
attr: str | ||
""" | ||
def visit_instance(self, t: Instance) -> Type: | ||
info = t.type | ||
# Special case, analyzed bases transformed the type into TupleType. | ||
if info.tuple_type and not self.seen: | ||
return info.tuple_type.copy_modified(fallback=Instance(info, [])) | ||
# Update forward Instance's to corresponding analyzed types. | ||
if info.replaced and info.replaced.tuple_type: | ||
tp = info.replaced.tuple_type | ||
if any((s is tp) or (s is t) for s in self.seen): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to
ForwardReferenceResolver
or something? The current name doesn't quite capture what this does. The docstring could mention that this does two things:Any
types. (Recursive type references can be thought of as a subset of forward references.)Style nit: I prefer multi-line docstrings to be formatted like this: