| [case testFastParseSyntaxError] | |
| 1 + # E: Expected an expression | |
| [case testFastParseTypeCommentSyntaxError] | |
| x = None # type: a : b # E: Syntax error in type comment "a : b" \ | |
| # E: Invalid type comment or annotation | |
| [case testFastParseInvalidTypeComment] | |
| x = None # type: a + b # E: Invalid type comment or annotation | |
| -- Function type comments are attributed to the function def line. | |
| -- This happens in both parsers. | |
| [case testFastParseFunctionAnnotationSyntaxError] | |
| def f(): # E: Syntax error in type comment "None -> None" | |
| # type: None -> None | |
| pass | |
| [case testFastParseFunctionAnnotationSyntaxErrorSpaces] | |
| def f(): # E: Syntax error in type comment "None -> None" | |
| # type: None -> None | |
| pass | |
| [case testFastParseInvalidFunctionAnnotation] | |
| def f(x): # E: Invalid type comment or annotation | |
| # type: (a + b) -> None | |
| pass | |
| [case testFastParseInvalidTypes3] | |
| # All of these should not crash | |
| from typing import Callable, Tuple, Iterable | |
| x: Tuple[int, str].x # E: Invalid type comment or annotation | |
| a: Iterable[x].x # E: Invalid type comment or annotation | |
| b: Tuple[x][x] # E: Invalid type comment or annotation | |
| c: Iterable[x][x] # E: Invalid type comment or annotation | |
| d: Callable[..., int][x] # E: Invalid type comment or annotation | |
| e: Callable[..., int].x # E: Invalid type comment or annotation | |
| f = None # type: Tuple[int, str].x # E: Invalid type comment or annotation | |
| g = None # type: Iterable[x].x # E: Invalid type comment or annotation | |
| h = None # type: Tuple[x][x] # E: Invalid type comment or annotation | |
| i = None # type: Iterable[x][x] # E: Invalid type comment or annotation | |
| j = None # type: Callable[..., int][x] # E: Invalid type comment or annotation | |
| k = None # type: Callable[..., int].x # E: Invalid type comment or annotation | |
| def f1(x: Tuple[int, str].x) -> None: pass # E: Invalid type comment or annotation | |
| def f2(x: Iterable[x].x) -> None: pass # E: Invalid type comment or annotation | |
| def f3(x: Tuple[x][x]) -> None: pass # E: Invalid type comment or annotation | |
| def f4(x: Iterable[x][x]) -> None: pass # E: Invalid type comment or annotation | |
| def f5(x: Callable[..., int][x]) -> None: pass # E: Invalid type comment or annotation | |
| def f6(x: Callable[..., int].x) -> None: pass # E: Invalid type comment or annotation | |
| [case testFastParseTypeWithIgnore] | |
| def f(x, # type: x # type: ignore | |
| ): | |
| # type: (...) -> None | |
| pass | |
| [case testFastParseVariableTypeWithIgnore] | |
| x = 1 # type: str # type: ignore | |
| [case testFastParseVariableTypeWithIgnoreNoSpace] | |
| x = 1 # type: str #type:ignore | |
| [case testFastParseVariableTypeWithIgnoreAndComment] | |
| x = 1 # type: str # type: ignore # comment | |
| [case testFastParseTypeWithIgnoreWithStmt] | |
| with open('test', 'r') as f: # type: int # type: ignore | |
| pass | |
| [case testFastParseTypeWithIgnoreForStmt] | |
| for i in (1, 2, 3, 100): # type: str # type: ignore | |
| pass | |
| [builtins fixtures/tuple.pyi] | |
| [case testFastParseVariableCommentThenIgnore] | |
| a="test" # type: int #comment # type: ignore # E: Incompatible types in assignment (expression has type "str", variable has type "int") | |
| [case testFastParseProperty] | |
| class C: | |
| @property | |
| def x(self) -> str: pass | |
| @x.setter | |
| def x(self, value: str) -> None: pass | |
| [builtins fixtures/property.pyi] | |
| [case testFastParseConditionalProperty] | |
| class C: | |
| if bool(): | |
| @property | |
| def x(self) -> str: pass | |
| @x.setter | |
| def x(self, value: str) -> None: pass | |
| [builtins fixtures/property.pyi] | |
| [case testFastParsePerArgumentAnnotations] | |
| # flags: --implicit-optional | |
| class A: pass | |
| class B: pass | |
| class C: pass | |
| class D: pass | |
| class E: pass | |
| class F: pass | |
| def f(a, # type: A | |
| b = None, # type: B | |
| *args, # type: C | |
| d = None, # type: D | |
| e, # type: E | |
| **kwargs # type: F | |
| ): | |
| reveal_type(a) # N: Revealed type is "__main__.A" | |
| reveal_type(b) # N: Revealed type is "__main__.B | None" | |
| reveal_type(args) # N: Revealed type is "builtins.tuple[__main__.C, ...]" | |
| reveal_type(d) # N: Revealed type is "__main__.D | None" | |
| reveal_type(e) # N: Revealed type is "__main__.E" | |
| reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]" | |
| [builtins fixtures/dict.pyi] | |
| [out] | |
| [case testFastParsePerArgumentAnnotationsWithReturn] | |
| # flags: --implicit-optional | |
| class A: pass | |
| class B: pass | |
| class C: pass | |
| class D: pass | |
| class E: pass | |
| class F: pass | |
| def f(a, # type: A | |
| b = None, # type: B | |
| *args, # type: C | |
| d = None, # type: D | |
| e, # type: E | |
| **kwargs # type: F | |
| ): | |
| # type: (...) -> int | |
| reveal_type(a) # N: Revealed type is "__main__.A" | |
| reveal_type(b) # N: Revealed type is "__main__.B | None" | |
| reveal_type(args) # N: Revealed type is "builtins.tuple[__main__.C, ...]" | |
| reveal_type(d) # N: Revealed type is "__main__.D | None" | |
| reveal_type(e) # N: Revealed type is "__main__.E" | |
| reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]" | |
| return "not an int" # E: Incompatible return value type (got "str", expected "int") | |
| [builtins fixtures/dict.pyi] | |
| [out] | |
| -- New parser ignores this instead of failing loudly. | |
| [case testFastParsePerArgumentAnnotationsWithAnnotatedBareStar_old_parser] | |
| def f(*, # type: int # E: Bare * has associated type comment | |
| x # type: str | |
| ): | |
| # type: (...) -> int | |
| pass | |
| [builtins fixtures/dict.pyi] | |
| [out] | |
| [case testFastParsePerArgumentAnnotationsWithReturnAndBareStar] | |
| def f(*, | |
| x # type: str | |
| ): | |
| # type: (...) -> int | |
| reveal_type(x) # N: Revealed type is "builtins.str" | |
| return "not an int" # E: Incompatible return value type (got "str", expected "int") | |
| [builtins fixtures/dict.pyi] | |
| [out] | |
| [case testFasterParseTooManyArgumentsAnnotation] | |
| def f(): # E: Type signature has too many parameters | |
| # type: (int) -> None | |
| pass | |
| f() | |
| f(1) # E: Too many arguments for "f" | |
| [case testFasterParseTooFewArgumentsAnnotation] | |
| def f(x, y): # E: Type signature has too few parameters | |
| # type: (int) -> None | |
| x() | |
| y() | |
| f(1, 2) | |
| f(1) # E: Missing positional argument "y" in call to "f" | |
| [case testFasterParseTypeErrorCustom] | |
| from typing import TypeVar, Generic | |
| T = TypeVar('T') | |
| class Foo(Generic[T]): | |
| pass | |
| def f(a: Foo(int)) -> int: | |
| pass | |
| [out] | |
| main:7: error: Invalid type comment or annotation | |
| main:7: note: Suggestion: use Foo[...] instead of Foo(...) | |
| [case testFasterParseCallWithKeywordArgs] | |
| def Foo(sort: bool) -> type: | |
| return int | |
| def f(a: Foo(sort=True)) -> int: | |
| pass | |
| [out] | |
| main:5: error: Invalid type comment or annotation | |
| main:5: note: Cannot use a function call in a type annotation | |
| [case testFastParseMatMul] | |
| from typing import Any | |
| x = None # type: Any | |
| x @ 1 | |
| x @= 1 | |
| [case testFastParserShowsMultipleErrors] | |
| def f(x): # E: Type signature has too few parameters | |
| # type: () -> None | |
| pass | |
| def g(): # E: Type signature has too many parameters | |
| # type: (int) -> None | |
| pass | |
| [case testFastParseMalformedAssert] | |
| assert 1, 2 | |
| assert (1, 2) # E: Assertion is always true, perhaps remove parentheses? | |
| assert (1, 2), 3 # E: Assertion is always true, perhaps remove parentheses? | |
| assert (1,) # E: Assertion is always true, perhaps remove parentheses? | |
| assert () | |
| [builtins fixtures/tuple.pyi] | |
| [case testFastParseAssertMessage] | |
| assert 1 | |
| assert 1, 2 | |
| assert 1, 1+2 | |
| assert 1, 1+'test' # E: Unsupported operand types for + ("int" and "str") | |
| assert 1, f() # E: Name "f" is not defined | |
| [case testFastParserConsistentFunctionTypes] | |
| def f1(x, y, z): | |
| # type: (int, int, int) -> int | |
| pass | |
| def f2(x, # type: int # E: Function has duplicate type signatures | |
| y, # type: int | |
| z # type: int | |
| ): | |
| # type: (int, int, int) -> int | |
| pass | |
| def f3(x, # type: int | |
| y, # type: int | |
| z # type: int | |
| ): | |
| # type: (...) -> int | |
| pass | |
| def f4(x, y, z): | |
| # type: (int, int, int) -> int | |
| pass | |
| def f5(x) -> int: # E: Function has duplicate type signatures | |
| # type: (int) -> int | |
| pass | |
| def f6(x: int, y: int, z: int): | |
| # type: (...) -> int | |
| pass | |
| def f7(x: int): # E: Function has duplicate type signatures | |
| # type: (int) -> int | |
| pass | |
| [case testFastParserDuplicateNames] | |
| def f(x, y, z): | |
| pass | |
| def g(x, y, x): # E: Duplicate parameter "x" in function definition | |
| pass | |
| def h(x, y, *x): # E: Duplicate parameter "x" in function definition | |
| pass | |
| def i(x, y, *z, **z): # E: Duplicate parameter "z" in function definition | |
| pass | |
| def j(x: int, y: int, *, x: int = 3): # E: Duplicate parameter "x" in function definition | |
| pass | |
| def k(*, y, z, y): # E: Duplicate parameter "y" in function definition | |
| pass | |
| lambda x, y, x: ... # E: Duplicate parameter "x" in function definition | |
| [builtins fixtures/tuple.pyi] | |
| [case testNoCrashOnImportFromStar] | |
| from pack import * | |
| [file pack/__init__.py] | |
| from . import * | |
| [case testNoCrashOnImportFromStarNested] | |
| import blamodule | |
| [file blamodule/__init__.py] | |
| from . import command | |
| from . import backends | |
| [file blamodule/backends/__init__.py] | |
| from .Bla import Bla | |
| Bla().method() | |
| [file blamodule/backends/Bla.py] | |
| from .. import * | |
| class Bla: | |
| def method(self) -> str: | |
| return command.call() | |
| [file blamodule/command.py] | |
| def call() -> str: pass | |
| [builtins fixtures/module.pyi] | |
| [case testInvalidEscapeSequenceWarningsSuppressed] | |
| # flags: --python-version 3.15 | |
| # Test that SyntaxWarnings for invalid escape sequences are suppressed | |
| # when parsing potential type expressions containing regex patterns or | |
| # similar strings. Callable arguments are always potential type expressions. | |
| from typing import TypeForm | |
| def identity(typx: TypeForm) -> TypeForm: | |
| return typx | |
| # This should not generate SyntaxWarning despite invalid escape sequence | |
| identity(r"re\.match") # E: Argument 1 to "identity" has incompatible type "str"; expected "TypeForm[Any]" | |
| [builtins fixtures/tuple.pyi] | |
| [typing fixtures/typing-full.pyi] |