Skip to content

New analyzer: reverse order of modules within SCC #7163

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 6 commits into from
Jul 8, 2019
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
7 changes: 7 additions & 0 deletions mypy/newsemanal/semanal_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def semantic_analysis_for_scc(graph: 'Graph', scc: List[str], errors: Errors) ->
"""Perform semantic analysis for all modules in a SCC (import cycle).

Assume that reachability analysis has already been performed.

The scc will be processed roughly in the order the modules are included
in the list.
"""
patches = [] # type: Patches
# Note that functions can't define new module-level attributes
Expand Down Expand Up @@ -152,6 +155,10 @@ def restore_saved_attrs(saved_attrs: SavedAttributes) -> None:
def process_top_levels(graph: 'Graph', scc: List[str], patches: Patches) -> None:
# Process top levels until everything has been bound.

# Reverse order of the scc so the first modules in the original list will be
# be processed first. This helps with performance.
scc = list(reversed(scc))

# Initialize ASTs and symbol tables.
for id in scc:
state = graph[id]
Expand Down
4 changes: 3 additions & 1 deletion test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,9 @@ C(0).total = 1 # E: Property "total" defined in "C" is read-only
import lib
[file lib.py]
import attr
from other import *
MYPY = False
if MYPY: # Force deferral
from other import *

@attr.s
class C:
Expand Down
46 changes: 46 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -5925,3 +5925,49 @@ class B:

class C(A, B): pass
[out]

[case testAttributeDefOrder1]
import a

[file a.py]
from b import C

class D(C):
def g(self) -> None:
self.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")

def f(self) -> None:
reveal_type(self.x) # N: Revealed type is 'builtins.int'


[file b.py]
import a

class C:
def __init__(self) -> None:
self.x = 0

[targets b, a, b.C.__init__, a.D.g, a.D.f, __main__]

[case testAttributeDefOrder2]
class D(C):
def g(self) -> None:
self.x = ''

def f(self) -> None:
# https://github.com/python/mypy/issues/7162
reveal_type(self.x) # N: Revealed type is 'builtins.str'


class C:
def __init__(self) -> None:
self.x = 0

class E(C):
def g(self) -> None:
self.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")

def f(self) -> None:
reveal_type(self.x) # N: Revealed type is 'builtins.int'

[targets __main__, __main__, __main__.D.g, __main__.D.f, __main__.C.__init__, __main__.E.g, __main__.E.f]
4 changes: 3 additions & 1 deletion test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@ c.x = 1 # E: Property "x" defined in "C" is read-only
import lib
[file lib.py]
from dataclasses import dataclass
from other import *
MYPY = False
if MYPY: # Force deferral
from other import *

@dataclass
class C:
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -4521,12 +4521,12 @@ B = List[A]

[builtins fixtures/list.pyi]
[out]
tmp/other.pyi:2: error: Module 'lib' has no attribute 'A'
tmp/lib.pyi:4: error: Module 'other' has no attribute 'B'
tmp/other.pyi:3: error: Cannot resolve name "B" (possible cyclic definition)
[out2]
tmp/other.pyi:2: error: Module 'lib' has no attribute 'A'
tmp/lib.pyi:4: error: Module 'other' has no attribute 'B'
tmp/other.pyi:3: error: Cannot resolve name "B" (possible cyclic definition)
tmp/a.py:3: note: Revealed type is 'builtins.list[builtins.list[Any]]'
tmp/a.py:3: note: Revealed type is 'builtins.list[Any]'

[case testRecursiveNamedTupleTypedDict]
# https://github.com/python/mypy/issues/7125
Expand Down
21 changes: 14 additions & 7 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ from a import bad2 # E: Module 'a' has no attribute 'bad2'; maybe "bad"?
[case testNewAnalyzerTypeAnnotationCycle4]
import b
[file a.py]
# TODO: Could we generate an error here as well?
from b import bad
from b import bad # E: Module 'b' has no attribute 'bad'
[file b.py]
from a import bad # E: Module 'a' has no attribute 'bad'
# TODO: Could we generate an error here as well?
from a import bad
[targets a, b, a, b, a, b, a, b, __main__]

[case testNewAnalyzerExportedValuesInImportAll]
from m import *
Expand Down Expand Up @@ -225,6 +226,8 @@ class A:
class C(B):
c: int

[targets b, a, b, a, __main__]

[case testNewAnalyzerTypedDictClass]
from mypy_extensions import TypedDict
import a
Expand Down Expand Up @@ -298,6 +301,8 @@ from a import x

def f(): pass

[targets a, b, a, a.y, b.f, __main__]

[case testNewAnalyzerRedefinitionAndDeferral1b]
import a

Expand All @@ -323,6 +328,8 @@ if MYPY: # Tweak processing order

def f(): pass

[targets b, a, b, a, b.f, a.y, __main__]

[case testNewAnalyzerRedefinitionAndDeferral2a]
import a

Expand Down Expand Up @@ -436,11 +443,11 @@ def main() -> None:
import b
[file a.py]
import b
x = b.x # E: Cannot determine type of 'x'
x = b.x # E: Cannot resolve attribute "x" (possible cyclic definition) \
# E: Module has no attribute "x"
[file b.py]
import a
x = a.x # E: Cannot resolve attribute "x" (possible cyclic definition) \
# E: Module has no attribute "x"
x = a.x
[builtins fixtures/module.pyi]

[case testNewAnalyzerMutuallyRecursiveOverloadedFunctions]
Expand Down Expand Up @@ -3081,4 +3088,4 @@ class Yes: ...
import a

def func() -> int: ...
[targets a, b, a, b.func, a.func, __main__]
[targets b, a, a, b.func, a.func, __main__]