blob: 2993113f5426c4af9273196bc05d43b1dc9752a0 [file]
-- Test cases for type checker related to super().
-- Supertype member reference
-- --------------------------
[case testAccessingSupertypeMethod]
class B:
def f(self) -> 'B': pass
class A(B):
def f(self) -> 'A':
a, b = None, None # type: (A, B)
a = super().f() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = super().g() # E: "g" undefined in superclass
b = super().f()
[out]
[case testAccessingSuperTypeMethodWithArgs]
from typing import Any
class B:
def f(self, y: 'A') -> None: pass
class A(B):
def f(self, y: Any) -> None:
a, b = None, None # type: (A, B)
super().f(b) # E: Argument 1 to "f" of "B" has incompatible type "B"; expected "A"
super().f(a)
self.f(b)
self.f(a)
[out]
[case testAccessingSuperInit]
import typing
class B:
def __init__(self, x: A) -> None: pass
class A(B):
def __init__(self) -> None:
super().__init__(B(None)) # E: Argument 1 to "__init__" of "B" has incompatible type "B"; expected "A"
super().__init__() # E: Too few arguments for "__init__" of "B"
super().__init__(A())
[out]
[case testAccessingSuperMemberWithDeepHierarchy]
import typing
class C:
def f(self) -> None: pass
class B(C): pass
class A(B):
def f(self) -> None:
super().g() # E: "g" undefined in superclass
super().f()
[out]
[case testAssignToBaseClassMethod]
import typing
class A:
def f(self) -> None: pass
class B(A):
def g(self) -> None:
super().f = None
[out]
main:6: error: Invalid assignment target
[case testSuperWithMultipleInheritance]
import typing
class A:
def f(self) -> None: pass
class B:
def g(self, x: int) -> None: pass
class C(A, B):
def f(self) -> None:
super().f()
super().g(1)
super().f(1) # E: Too many arguments for "f" of "A"
super().g() # E: Too few arguments for "g" of "B"
super().not_there() # E: "not_there" undefined in superclass
[out]
[case testSuperWithNew]
class A:
def __new__(cls, x: int) -> 'A':
return object.__new__(cls)
class B(A):
def __new__(cls, x: int, y: str = '') -> 'A':
super().__new__(cls, 1)
super().__new__(cls, 1, '') # E: Too many arguments for "__new__" of "A"
B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
B(1)
B(1, 'x')
[builtins fixtures/__new__.pyi]
[case testSuperWithUnknownBase]
from typing import Any
B = None # type: Any
class C(B):
def __init__(self, arg=0):
super(C, self).__init__(arg, arg=arg)
[out]
[case testSuperSilentInDynamicFunction]
class A:
pass
class B(A):
def foo(self):
super(B, self).foo() # Not an error
[out]