blob: 87cc0aed4ccea2b59ab9fb26cfe7dcfdc7d615ca [file] [log] [blame]
-- Type checking after an error during semantic analysis
-- -----------------------------------------------------
--
-- This tests both the semantic analyzer (that it does not generate
-- corrupt state on error) and the type checker (that it can deal with
-- whatever state the semantic analyzer sets up).
-- TODO:
-- - invalid type in annotation
-- - invalid function comment type annotation
-- - invalid multiple assignment type annotation
-- - using a type variable as a value
-- - using special names defined in typing as values
[case testMissingModuleImport1]
import m # E
m.foo()
m.x = m.y
1() # E
[out]
main:1: error: Cannot find implementation or library stub for module named 'm'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: "int" not callable
[case testMissingModuleImport2]
from m import x # E
x.foo()
x.a = x.b
1() # E
[out]
main:1: error: Cannot find implementation or library stub for module named 'm'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: "int" not callable
[case testMissingModuleImport3]
from m import * # E
x # E
1() # E
[out]
main:1: error: Cannot find implementation or library stub for module named 'm'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Name 'x' is not defined
main:3: error: "int" not callable
[case testInvalidBaseClass1]
class A(X): # E: Name 'X' is not defined
x = 1
A().foo(1)
A().x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testInvalidBaseClass2]
X = 1
class A(X): # E
x = 1
A().foo(1)
A().x = '' # E
[out]
main:3: error: Variable "__main__.X" is not valid as a type
main:3: error: Invalid base class "X"
main:6: error: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testInvalidNumberOfTypeArgs]
from typing import TypeVar
T = TypeVar('T')
class C: # Forgot to add type params here
def __init__(self, t: T) -> None: pass
c = C(t=3) # type: C[int] # E: "C" expects no type arguments, but 1 given
[case testBreakOutsideLoop]
break # E: 'break' outside loop
[case testContinueOutsideLoop]
continue # E: 'continue' outside loop
[case testYieldOutsideFunction]
yield # E: 'yield' outside function
[case testYieldFromOutsideFunction]
x = 1
yield from x # E: 'yield from' outside function
[case testImportFuncDup]
import m
def m() -> None: ... # E: Name 'm' already defined (by an import)
[file m.py]
[out]
[case testIgnoredImportDup]
import m # type: ignore
from m import f # type: ignore
def m() -> None: ... # E: Name 'm' already defined (possibly by an import)
def f() -> None: ... # E: Name 'f' already defined (possibly by an import)
[out]
[case testRuntimeProtoTwoBases]
from typing_extensions import Protocol, runtime_checkable
from typing import TypeVar, Generic
T = TypeVar('T')
@runtime_checkable
class P(Protocol, Generic[T]):
attr: T
class C:
attr: int
x: P[int] = C()
[out]