Skip to content
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

Fix crashes and fails in forward references #3952

Merged
merged 43 commits into from Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
45e5931
Add basic tests, more details will be added when they will not crash
ilevkivskyi Aug 31, 2017
cb4caa5
Correct tests
ilevkivskyi Sep 1, 2017
1cdc980
Implement ForwardRef type, wrap UnboundType, pass SecondPass to third…
ilevkivskyi Sep 11, 2017
260ef02
Add ForwardRefRemover
ilevkivskyi Sep 11, 2017
a58a217
Add elimination patches
ilevkivskyi Sep 11, 2017
950a022
Fix replacement logic; fix newtype error formatting
ilevkivskyi Sep 11, 2017
411b24d
Fix third pass (need to go deeper)
ilevkivskyi Sep 11, 2017
b9b8528
Implement syntethic replacer
ilevkivskyi Sep 11, 2017
48d6de4
Need to go deeper (as usual)
ilevkivskyi Sep 11, 2017
ec45441
Fix postponed fallback join
ilevkivskyi Sep 11, 2017
ac32ed4
Simplify some code and add annotations
ilevkivskyi Sep 11, 2017
3fb3019
Simplify traversal logic; add loads of tests
ilevkivskyi Sep 12, 2017
f9b1320
Take care about one more special case; add few tests and dcostrings
ilevkivskyi Sep 12, 2017
cf014b8
Unify visitors
ilevkivskyi Sep 12, 2017
665236b
Add some more comments and docstrings
ilevkivskyi Sep 12, 2017
9a318aa
Add recursive type warnings
ilevkivskyi Sep 12, 2017
757fbd9
Fix lint
ilevkivskyi Sep 12, 2017
4502ce2
Also clean-up bases; add more tests and allow some previously skipped
ilevkivskyi Sep 13, 2017
3b39d40
One more TypedDict test
ilevkivskyi Sep 13, 2017
c8b28fe
Add another simple self-referrential NamedTuple test
ilevkivskyi Sep 13, 2017
9f92b0f
Fix type_override; add tests for recursive aliases; fix Callable TODO…
Sep 13, 2017
9779103
Merge branch 'master' into fix-synthetic-crashes
ilevkivskyi Sep 14, 2017
b914bdb
Merge remote-tracking branch 'upstream/master' into fix-synthetic-cra…
ilevkivskyi Sep 19, 2017
3568fdb
Skip the whole ForwardRef dance in unchecked functions
ilevkivskyi Sep 19, 2017
54d9331
Simplify test
ilevkivskyi Sep 19, 2017
b9ddacc
Fix self-check
ilevkivskyi Sep 19, 2017
5bfe9ca
Fix cross-file forward references (+test)
ilevkivskyi Sep 19, 2017
a2912e9
More tests
ilevkivskyi Sep 19, 2017
10c65b8
Merge branch 'master' into fix-synthetic-crashes
ilevkivskyi Sep 20, 2017
21dfbfe
Fix situation when recursive namedtuple appears directly in base clas…
ilevkivskyi Sep 20, 2017
f2ddbcd
Merge branch 'fix-synthetic-crashes' of https://github.com/ilevkivsky…
ilevkivskyi Sep 20, 2017
03597ee
Clean-up PR: Remove unnecesary imports, outdated comment, unnecessary…
ilevkivskyi Sep 20, 2017
649ef32
Add tests for generic classes, enums, with statements and for statements
ilevkivskyi Sep 20, 2017
83f8907
Add processing for for and with statements (+more tests)
ilevkivskyi Sep 20, 2017
13c7176
Add support for generic types with forward references
ilevkivskyi Sep 20, 2017
79b10d6
Prohibit forward refs to type vars and subscripted forward refs to al…
ilevkivskyi Sep 21, 2017
321a809
Refactor code to avoid passing semantic analyzer to type analyzer, on…
ilevkivskyi Sep 21, 2017
076c909
Address the rest of the review comments
ilevkivskyi Sep 22, 2017
c1a63ec
Improve two tests
ilevkivskyi Sep 22, 2017
97e6f47
Add one more test as suggested in #3990
ilevkivskyi Sep 23, 2017
8f52654
Address latest review comments
ilevkivskyi Sep 26, 2017
6edd078
Improve tests; Fix one more crash on NewType MRO
ilevkivskyi Sep 27, 2017
514b8bd
Fix formatting in tests
ilevkivskyi Sep 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions mypy/semanal.py
Expand Up @@ -4230,6 +4230,14 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
self.analyze(node.type_override, node)
super().visit_assignment_stmt(s)

def visit_for_stmt(self, s: ForStmt) -> None:
self.analyze(s.index_type, s)
super().visit_for_stmt(s)

def visit_with_stmt(self, s: WithStmt) -> None:
self.analyze(s.target_type, s)
super().visit_with_stmt(s)

def visit_cast_expr(self, e: CastExpr) -> None:
self.analyze(e.type, e)
super().visit_cast_expr(e)
Expand All @@ -4247,6 +4255,14 @@ def visit_type_application(self, e: TypeApplication) -> None:
def perform_transform(self, node: Union[Node, SymbolTableNode],
transform: Callable[[Type], Type]) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is type_vars of ClassDef transformed somewhere? What about index_type of ForStmt and target_type of WithStmt? And TypeVarExpr?

Copy link
Member Author

Choose a reason for hiding this comment

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

type_vars and TypeVarExpr are probably not needed (I will double-check), but it looks like I just forgot about WithStmt and ForStmt. I will add processing.

"""Apply transform to all types associated with node."""
if isinstance(node, ForStmt):
node.index_type = transform(node.index_type)
self.transform_types(node.index, transform)
if isinstance(node, WithStmt):
node.target_type = transform(node.target_type)
for n in node.target:
if isinstance(n, NameExpr) and isinstance(n.node, Var) and n.node.type:
n.node.type = transform(n.node.type)
if isinstance(node, (FuncDef, CastExpr, AssignmentStmt, TypeAliasExpr, Var)):
node.type = transform(node.type)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about type_annotation of Argument? Should we process it?

Copy link
Member Author

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.

if isinstance(node, NewTypeExpr):
Expand Down Expand Up @@ -4275,6 +4291,15 @@ def perform_transform(self, node: Union[Node, SymbolTableNode],
new_bases.append(alt_base)
node.bases = new_bases

def transform_types(self, lvalue: Lvalue, transform: Callable[[Type], Type]) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The name of the method would be more informative as transform_types_in_lvalue or similar.

if isinstance(lvalue, RefExpr):
if isinstance(lvalue.node, Var):
var = lvalue.node
var.type = transform(var.type)
elif isinstance(lvalue, TupleExpr):
for item in lvalue.items:
self.transform_types(item, transform)

def analyze(self, type: Optional[Type], node: Union[Node, SymbolTableNode],
warn: bool = False) -> None:
# Recursive type warnings are only emitted on type definition 'node's, marked by 'warn'
Expand Down
36 changes: 31 additions & 5 deletions test-data/unit/check-statements.test
Expand Up @@ -1565,25 +1565,51 @@ reveal_type(d['weight0']) # E: Revealed type is 'builtins.float*'

[builtins fixtures/floatdict.pyi]

[case testForwardRefsInForStatement]
[case testForwardRefsInForStatementImplicit]
from typing import List, NamedTuple
lst: List[N]

for i in lst:
a: int = i.x
b: str = i[0] # E: Incompatible types in assignment (expression has type "int", variable has type "str")

N = NamedTuple('N', [('x', int)])
[builtins fixtures/list.pyi]
[out]

[case testForwardRefsInForStatement]
from typing import List, NamedTuple
lst: List[M]

for i in lst: # type: N
a: int = i.x
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, prefer reveal_type.

b: str = i[0]
b: str = i[0] # E: Incompatible types in assignment (expression has type "int", variable has type "str")

N = NamedTuple('N', [('x', int)])
class M(N): pass
[builtins fixtures/list.pyi]
[out]

[case testForwardRefsInWithStatement]
from typing import ContextManager
[case testForwardRefsInWithStatementImplicit]
from typing import ContextManager, Any
from mypy_extensions import TypedDict
cm: ContextManager[N]

with cm as g:
a: int = g['x']

N = TypedDict('N', {'x': int})
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[out]

[case testForwardRefsInWithStatement]
from typing import ContextManager, Any
from mypy_extensions import TypedDict
cm: ContextManager[Any]

with cm as g: # type: N
a: str = g['x']
a: str = g['x'] # E: Incompatible types in assignment (expression has type "int", variable has type "str")

N = TypedDict('N', {'x': int})
[builtins fixtures/dict.pyi]
Expand Down