Repository navigation
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 |
|---|---|---|
|
|
@@ -4049,12 +4049,14 @@ class ThirdPass(TraverserVisitor): | |
| straightforward type inference. | ||
| """ | ||
|
|
||
| def __init__(self, modules: Dict[str, MypyFile], errors: Errors, sem) -> None: | ||
| def __init__(self, modules: Dict[str, MypyFile], errors: Errors, | ||
| sem: SemanticAnalyzer) -> None: | ||
| self.modules = modules | ||
| self.errors = errors | ||
| self.sem = sem | ||
|
|
||
| def visit_file(self, file_node: MypyFile, fnam: str, options: Options, patches) -> None: | ||
| def visit_file(self, file_node: MypyFile, fnam: str, options: Options, | ||
| patches: List[Callable[[], None]]) -> None: | ||
| self.errors.set_file(fnam, file_node.fullname()) | ||
| self.options = options | ||
| self.sem.options = options | ||
|
|
@@ -4211,7 +4213,7 @@ def visit_type_application(self, e: TypeApplication) -> None: | |
|
|
||
| # Helpers | ||
|
|
||
| def perform_transform(self, node: Node, transform) -> None: | ||
| def perform_transform(self, node: Node, transform: Callable[[Type], Type]) -> None: | ||
| if isinstance(node, (FuncDef, CastExpr, AssignmentStmt, TypeAliasExpr, Var)): | ||
| node.type = transform(node.type) | ||
| if isinstance(node, NewTypeExpr): | ||
|
|
@@ -4225,14 +4227,6 @@ def perform_transform(self, node: Node, transform) -> None: | |
| if isinstance(node, TypeApplication): | ||
| node.types = [transform(t) for t in node.types] | ||
|
|
||
| def remove_forward_refs(self, tp: Type) -> Type: | ||
| tp = tp.accept(ForwardRefRemover()) | ||
| return tp | ||
|
|
||
| def replace_synthetic(self, tp: Type) -> Type: | ||
| tp = tp.accept(SyntheticReplacer()) | ||
| return tp | ||
|
|
||
| def analyze(self, type: Optional[Type], node: Optional[Node]) -> None: | ||
| indicator = {} # type: Dict[str, bool] | ||
| if type: | ||
|
|
@@ -4241,12 +4235,12 @@ def analyze(self, type: Optional[Type], node: Optional[Node]) -> None: | |
| type.accept(analyzer) | ||
| self.check_for_omitted_generics(type) | ||
| if indicator.get('forward') and node is not None: | ||
| def patch(): | ||
| self.perform_transform(node, self.remove_forward_refs) | ||
| def patch() -> None: | ||
| self.perform_transform(node, lambda tp: tp.accept(ForwardRefRemover())) | ||
| self.patches.append(patch) | ||
| if indicator.get('synthetic') and node is not None: | ||
| def patch(): | ||
| self.perform_transform(node, self.replace_synthetic) | ||
| def patch() -> None: | ||
| self.perform_transform(node, lambda tp: tp.accept(SyntheticReplacer())) | ||
| self.patches.append(patch) | ||
|
|
||
| def check_for_omitted_generics(self, typ: Type) -> None: | ||
|
|
@@ -4738,8 +4732,8 @@ def visit_type_type(self, t: TypeType) -> TypeType: | |
|
|
||
|
|
||
| class ForwardRefRemover(TypeReplacer): | ||
| def __init__(self): | ||
| self.seen = [] | ||
| def __init__(self) -> None: | ||
| self.seen = [] # type: List[Type] | ||
|
Collaborator
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
Member
Author
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 |
||
|
|
||
| def visit_forwardref_type(self, t: ForwardRef) -> Type: | ||
| if not any(s is t for s in self.seen): | ||
|
|
@@ -4749,8 +4743,8 @@ def visit_forwardref_type(self, t: ForwardRef) -> Type: | |
|
|
||
|
|
||
| class SyntheticReplacer(TypeReplacer): | ||
| def __init__(self): | ||
| self.seen = [] | ||
| def __init__(self) -> None: | ||
| self.seen = [] # type: List[Type] | ||
|
|
||
| def visit_tuple_type(self, t: TupleType) -> TupleType: | ||
| self.seen.append(t) | ||
|
|
||
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.
What about
type_annotationofArgument? Should we process it?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.
I was not able to trigger any errors with this, I will double check.