Skip to content

Update tests to prepare for variable redefinition with new type #6095

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
Dec 21, 2018
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
67 changes: 44 additions & 23 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ a = None # type: A
b = None # type: B
c = None # type: C

def f(): i, j, a, b, c # Prevent redefinition

j = c # E: Incompatible types in assignment (expression has type "C", variable has type "J")
a = i # E: Incompatible types in assignment (expression has type "I", variable has type "A")
a = j # E: Incompatible types in assignment (expression has type "J", variable has type "A")
Expand Down Expand Up @@ -46,6 +48,8 @@ j = None # type: J
a = None # type: A
o = None # type: object

def f(): i, j, a, o # Prevent redefinition

j = i # E: Incompatible types in assignment (expression has type "I", variable has type "J")
a = i # E: Incompatible types in assignment (expression has type "I", variable has type "A")
a = j # E: Incompatible types in assignment (expression has type "J", variable has type "A")
Expand All @@ -65,18 +69,21 @@ class J(I): pass
class A(J): pass

[case testInheritingAbstractClassInSubclass]

from abc import abstractmethod, ABCMeta

i = None # type: I
a = None # type: A
b = None # type: B

i = a # E: Incompatible types in assignment (expression has type "A", variable has type "I")
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
i = a # E: Incompatible types in assignment (expression has type "A", variable has type "I")
if int():
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")

a = b
i = b
if int():
a = b
if int():
i = b

class I(metaclass=ABCMeta):
@abstractmethod
Expand Down Expand Up @@ -116,12 +123,17 @@ class B: pass
i, a, b = None, None, None # type: (I, A, B)
o = None # type: object

a = cast(I, o) # E: Incompatible types in assignment (expression has type "I", variable has type "A")
b = cast(B, i) # Ok; a subclass of B might inherit I
i = cast(I, b) # Ok; a subclass of B might inherit I
if int():
a = cast(I, o) # E: Incompatible types in assignment (expression has type "I", variable has type "A")
if int():
b = cast(B, i) # Ok; a subclass of B might inherit I
if int():
i = cast(I, b) # Ok; a subclass of B might inherit I

i = cast(I, o)
i = cast(I, a)
if int():
i = cast(I, o)
if int():
i = cast(I, a)

[case testInstantiatingClassThatImplementsAbstractMethod]
from abc import abstractmethod, ABCMeta
Expand Down Expand Up @@ -218,15 +230,21 @@ class C(B):

var: Type[A]
var()
var = A # E: Can only assign concrete classes to a variable of type "Type[A]"
var = B # E: Can only assign concrete classes to a variable of type "Type[A]"
var = C # OK
if int():
var = A # E: Can only assign concrete classes to a variable of type "Type[A]"
if int():
var = B # E: Can only assign concrete classes to a variable of type "Type[A]"
if int():
var = C # OK

var_old = None # type: Type[A] # Old syntax for variable annotations
var_old()
var_old = A # E: Can only assign concrete classes to a variable of type "Type[A]"
var_old = B # E: Can only assign concrete classes to a variable of type "Type[A]"
var_old = C # OK
if int():
var_old = A # E: Can only assign concrete classes to a variable of type "Type[A]"
if int():
var_old = B # E: Can only assign concrete classes to a variable of type "Type[A]"
if int():
var_old = C # OK
[out]

[case testInstantiationAbstractsInTypeForClassMethods]
Expand Down Expand Up @@ -362,7 +380,6 @@ main:11: error: Return type of "h" incompatible with supertype "I"


[case testAccessingAbstractMethod]

from abc import abstractmethod, ABCMeta

class I(metaclass=ABCMeta):
Expand All @@ -371,14 +388,16 @@ class I(metaclass=ABCMeta):

i, a, b = None, None, None # type: (I, int, str)

a = i.f(a) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
b = i.f(b) # E: Argument 1 to "f" of "I" has incompatible type "str"; expected "int"
if int():
a = i.f(a) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
b = i.f(b) # E: Argument 1 to "f" of "I" has incompatible type "str"; expected "int"
i.g() # E: "I" has no attribute "g"

b = i.f(a)
if int():
b = i.f(a)

[case testAccessingInheritedAbstractMethod]

from abc import abstractmethod, ABCMeta

class J(metaclass=ABCMeta):
Expand All @@ -388,8 +407,10 @@ class I(J): pass

i, a, b = None, None, None # type: (I, int, str)

a = i.f(1) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
b = i.f(1)
if int():
a = i.f(1) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
b = i.f(1)


-- Any (dynamic) types
Expand Down
87 changes: 40 additions & 47 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,43 @@
[out]

[case testAssignmentAndVarDef]

a = None # type: A
b = None # type: B
a = a
a = b # Fail
if int():
a = a
if int():
a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A: pass
class B: pass
[out]
main:5: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testConstructionAndAssignment]

x = None # type: A
x = A()
x = B()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A:
def __init__(self): pass
class B:
def __init__(self): pass
[out]
main:4: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testInheritInitFromObject]

x = None # type: A
x = A()
x = B()
if int():
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A(object): pass
class B(object): pass
[out]
main:4: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testImplicitInheritInitFromObject]

x = None # type: A
o = None # type: object
x = o # E: Incompatible types in assignment (expression has type "object", variable has type "A")
x = A()
o = x
if int():
x = o # E: Incompatible types in assignment (expression has type "object", variable has type "A")
if int():
x = A()
if int():
o = x
class A: pass
class B: pass
[out]
Expand Down Expand Up @@ -71,8 +69,10 @@ main:3: error: Incompatible types in assignment (expression has type "A", variab
[case testDeclaredVariableInParentheses]

(x) = None # type: int
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x = 1
if int():
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
x = 1


-- Simple functions and calling
Expand Down Expand Up @@ -122,40 +122,34 @@ main:4: error: Too many arguments for "f"


[case testLocalVariables]

def f() -> None:
x = None # type: A
y = None # type: B
x = x
x = y # Fail
if int():
x = x
x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A: pass
class B: pass
[out]
main:6: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testLocalVariableScope]

def f() -> None:
x = None # type: A
x: A
x = A()
def g() -> None:
x = None # type: B
x = A() # Fail
x: B
x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
class A: pass
class B: pass
[out]
main:7: error: Incompatible types in assignment (expression has type "A", variable has type "B")

[case testFunctionArguments]
import typing
def f(x: 'A', y: 'B') -> None:
x = y # Fail
x = x
y = B()
if int():
x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
x = x
y = B()
class A: pass
class B: pass
[out]
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testLocalVariableInitialization]
import typing
Expand Down Expand Up @@ -245,29 +239,28 @@ a = __file__ # type: int # E: Incompatible types in assignment (expression has
[case testLocalVariableShadowing]

a = None # type: A
a = B() # Fail
a = A()
if int():
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = A()
def f() -> None:
a = None # type: B
a = A() # Fail
a = B()
a = B() # Fail
if int():
a = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = B()
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = A()

class A: pass
class B: pass
[out]
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")
main:7: error: Incompatible types in assignment (expression has type "A", variable has type "B")
main:9: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testGlobalDefinedInBlockWithType]

class A: pass
while A:
a = None # type: A
a = A()
a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
if int():
a = A()
a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")


-- # type: signatures
Expand Down
17 changes: 8 additions & 9 deletions test-data/unit/check-bound.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ T = TypeVar('T', bound=A)
U = TypeVar('U')
def f(x: T) -> T: pass
def g(x: U) -> U:
return f(x) # Fail
return f(x) # E: Value of type variable "T" of "f" cannot be "U"

f(A())
f(B())
f(D()) # Fail
f(D()) # E: Value of type variable "T" of "f" cannot be "D"

b = B()
b = f(b)
b = f(C()) # Fail
[out]
main:12: error: Value of type variable "T" of "f" cannot be "U"
main:16: error: Value of type variable "T" of "f" cannot be "D"
main:20: error: Incompatible types in assignment (expression has type "C", variable has type "B")
if int():
b = f(b)
if int():
b = f(C()) # E: Incompatible types in assignment (expression has type "C", variable has type "B")


[case testBoundOnGenericClass]
Expand Down Expand Up @@ -200,6 +198,7 @@ def foo(x: int) -> int:

a = 1
b = foo(a)
b = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
b = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
twice(a) # E: Value of type variable "T" of "twice" cannot be "int"
[builtins fixtures/args.pyi]
Loading