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

New analyzer: add more tests for order of processing #7173

Merged
merged 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion mypy/newsemanal/semanal_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ def process_functions(graph: 'Graph', scc: List[str], patches: Patches) -> None:
# but _methods_ must be processed in the order they are defined,
# because some features (most notably partial types) depend on
# order of definitions on self.
targets = sorted(get_all_leaf_targets(tree), key=lambda x: x[1].line)
#
# There can be multiple generated methods per line. Use target
# name as the second sort key to get a repeatable sort order on
# Python 3.5, which doesn't preserve dictionary order.
targets = sorted(get_all_leaf_targets(tree), key=lambda x: (x[1].line, x[0]))
for target, node, active_type in targets:
assert isinstance(node, (FuncDef, OverloadedFuncDef, Decorator))
process_top_level_function(analyzer,
Expand Down
1 change: 1 addition & 0 deletions mypy/newsemanal/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ def add_method(funcname: str,
v._fullname = func._fullname
func.is_decorated = True
dec = Decorator(func, [NameExpr('classmethod')], v)
dec.line = line
sym = SymbolTableNode(MDEF, dec)
else:
sym = SymbolTableNode(MDEF, func)
Expand Down
4 changes: 3 additions & 1 deletion mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def run_case_once(self, testcase: DataDrivenTestCase,
expected = testcase.expected_fine_grained_targets.get(incremental_step + 1)
actual = res.manager.processed_targets
# Skip the initial builtin cycle.
actual = [t for t in actual if not any(t.startswith(mod) for mod in core_modules)]
actual = [t for t in actual
if not any(t.startswith(mod)
for mod in core_modules + ['mypy_extensions'])]
if expected is not None:
assert_target_equivalence(name, expected, actual)
if incremental_step > 1:
Expand Down
9 changes: 6 additions & 3 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ a.foo(B())
class A:
def foo(self, x: 'B') -> None: pass
class B(A): pass
[targets __main__, __main__, __main__.A.foo]
[out]
main:3: error: Argument 1 to "foo" of "A" has incompatible type "A"; expected "B"

Expand Down Expand Up @@ -189,7 +190,7 @@ class A:
class B(A):
def f(self) -> None:
self.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[out]
[targets __main__, __main__.A.f, __main__.B.f]

[case testAssignmentToAttributeInMultipleMethods]
import typing
Expand Down Expand Up @@ -898,6 +899,7 @@ import typing
class A:
class B: pass
A.B = None # E: Cannot assign to a type
[targets __main__]

[case testAccessingClassAttributeWithTypeInferenceIssue]
x = C.x # E: Cannot determine type of 'x'
Expand All @@ -921,6 +923,7 @@ class A(Generic[T]):
x = None # type: T
A.x # E: Access to generic instance variables via class is ambiguous
A[int].x # E: Access to generic instance variables via class is ambiguous
[targets __main__]

[case testAccessingNestedGenericClassAttribute]
from typing import Generic, List, TypeVar, Union
Expand All @@ -938,14 +941,13 @@ A[int, int].x # E: Access to generic instance variables via class is ambiguous


[case testClassWithinFunction]

def f() -> None:
class A:
def g(self) -> None: pass
a = None # type: A
a.g()
a.g(a) # E: Too many arguments for "g" of "A"
[out]
[targets __main__, __main__.f]

[case testConstructNestedClass]
import typing
Expand Down Expand Up @@ -1119,6 +1121,7 @@ A().f(1)
A.f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
[builtins fixtures/classmethod.pyi]
[targets __main__, __main__.A.f]

[case testBuiltinClassMethod]
import typing
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ i = n.b # type: int # E: Incompatible types in assignment (expression has type
x, y = n
if int():
x = y # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[targets __main__, __main__.N.__new__, __main__.N._asdict, __main__.N._make, __main__.N._replace]


[case testNamedTupleWithTupleFieldNamesWithItemTypes]
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,8 @@ class C: pass
import a
from b import C

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

[case testNewAnalyzerImportOverExistingInCycle]
import a
[file a.py]
Expand Down Expand Up @@ -3089,3 +3091,23 @@ import a

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

[case testNewAnalyzerForwardReferenceInFunction]
def f(x: 'A') -> 'A':
return A()

class A:
pass
[targets __main__, __main__.f]

[case testNewAnalyzerSimpleImportStarNoDeferral]
from m import *

x: A
f()

[file m.py]
class A: pass
def f() -> None: pass

[targets m, m.f, __main__]
3 changes: 2 additions & 1 deletion test-data/unit/check-newtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ num = id + 1

reveal_type(id) # N: Revealed type is '__main__.UserId'
reveal_type(num) # N: Revealed type is 'builtins.int'
[out]

[targets __main__, __main__.UserId.__init__, __main__.name_by_id]

[case testNewTypePEP484Example2]
from typing import NewType
Expand Down
6 changes: 5 additions & 1 deletion test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ i = int
def f(x: i) -> None: pass
f(1)
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
[targets __main__, __main__.f]

[case testUnionTypeAlias]
from typing import Union
Expand All @@ -12,20 +13,23 @@ def f(x: U) -> None: pass
f(1)
f('')
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Union[int, str]"
[targets __main__, __main__.f]

[case testTupleTypeAlias]
from typing import Tuple
T = Tuple[int, str]
def f(x: T) -> None: pass
f((1, 'x'))
f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[int, str]"
[targets __main__, __main__.f]

[case testCallableTypeAlias]
from typing import Callable
A = Callable[[int], None]
f = None # type: A
f(1)
f('') # E: Argument 1 has incompatible type "str"; expected "int"
[targets __main__]

[case testListTypeAlias]
from typing import List
Expand All @@ -34,7 +38,7 @@ def f(x: A) -> None: pass
f([1])
f(['x']) # E: List item 0 has incompatible type "str"; expected "int"
[builtins fixtures/list.pyi]
[out]
[targets __main__, __main__.f]

[case testAnyTypeAlias]
from typing import Any
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ reveal_type(p) # N: Revealed type is 'TypedDict('__main__.Point', {'x': builtin
reveal_type(p.values()) # N: Revealed type is 'typing.Iterable[builtins.object*]'
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[targets sys, __main__]

[case testCanCreateTypedDictInstanceWithDictCall]
from mypy_extensions import TypedDict
Expand Down