blob: eeb964e990ef7c59930ac84cd5f7bc21a0cf269f [file] [log] [blame] [edit]
[case testDecoratedClassLine]
def d(c): ...
@d
class C: ...
class C: ... # E: Name 'C' already defined on line 4
[case testDecoratedFunctionLine]
# flags: --disallow-untyped-defs
def d(f): ... # type: ignore
@d
def f(): ... # E: Function is missing a return type annotation \
# N: Use "-> None" if function does not return a value
[case testIgnoreDecoratedFunction1]
# flags: --disallow-untyped-defs --warn-unused-ignores
def d(f): ... # type: ignore
@d
# type: ignore
def f(): ... # type: ignore # E: unused 'type: ignore' comment
[case testIgnoreDecoratedFunction2]
# flags: --disallow-untyped-defs
def d(f): ... # type: ignore
@d
def f(): ... # type: ignore
[case testIgnoreScopeIssue1032]
def f(a: int): ...
f(
1,
2,
) # type: ignore
[case testIgnoreScopeNested1]
def g(x: str) -> str: ...
def f(a: int) -> int: ...
f(
f(
g(
"IGNORE"
) # type: ignore
)
)
[case testIgnoreScopeNested2]
[
"IGNORE" # type: ignore
&
"IGNORE",
]
[builtins fixtures/list.pyi]
[case testIgnoreScopeNested3]
{
"IGNORE"
| # type: ignore
"IGNORE",
}
[builtins fixtures/set.pyi]
[case testIgnoreScopeNested4]
{
None: "IGNORE"
^
"IGNORE", # type: ignore
}
[builtins fixtures/dict.pyi]
[case testIgnoreScopeNestedNonOverlapping]
def f(x: int): ...
def g(x: int): ...
(
f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
g("IGNORE"), # type: ignore
f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
)
[case testIgnoreScopeNestedOverlapping]
def f(x: int): ...
def g(x: int): ...
(
f("ERROR"), g( # E: Argument 1 to "f" has incompatible type "str"; expected "int"
"IGNORE" # type: ignore
), f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
)
[case testIgnoreScopeUnused1]
# flags: --warn-unused-ignores
( # type: ignore # E: unused 'type: ignore' comment
"IGNORE" # type: ignore # E: unused 'type: ignore' comment
+ # type: ignore # E: unused 'type: ignore' comment
0 # type: ignore
) # type: ignore # E: unused 'type: ignore' comment
[case testIgnoreScopeUnused2]
# flags: --warn-unused-ignores
( # type: ignore # E: unused 'type: ignore' comment
"IGNORE"
- # type: ignore
0 # type: ignore # E: unused 'type: ignore' comment
) # type: ignore # E: unused 'type: ignore' comment
[case testIgnoreScopeUnused3]
# flags: --warn-unused-ignores
( # type: ignore # E: unused 'type: ignore' comment
"IGNORE"
/
0 # type: ignore
) # type: ignore # E: unused 'type: ignore' comment
[case testPEP570ArgTypesMissing]
# flags: --disallow-untyped-defs
def f(arg, /) -> None: ... # E: Function is missing a type annotation for one or more arguments
[case testPEP570ArgTypesBadDefault]
def f(arg: int = "ERROR", /) -> None: ... # E: Incompatible default for argument "arg" (default has type "str", argument has type "int")
[case testPEP570ArgTypesDefault]
def f(arg: int = 0, /) -> None:
reveal_type(arg) # N: Revealed type is 'builtins.int'
[case testPEP570ArgTypesRequired]
def f(arg: int, /) -> None:
reveal_type(arg) # N: Revealed type is 'builtins.int'
[case testPEP570Required]
def f(arg: int, /) -> None: ... # N: "f" defined here
f(1)
f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int"
f(arg=1) # E: Unexpected keyword argument "arg" for "f"
f(arg="ERROR") # E: Unexpected keyword argument "arg" for "f"
[case testPEP570Default]
def f(arg: int = 0, /) -> None: ... # N: "f" defined here
f()
f(1)
f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int"
f(arg=1) # E: Unexpected keyword argument "arg" for "f"
f(arg="ERROR") # E: Unexpected keyword argument "arg" for "f"
[case testPEP570Calls]
def f(p, /, p_or_kw, *, kw) -> None: ... # N: "f" defined here
f(0, 0, 0) # E: Too many positional arguments for "f"
f(0, 0, kw=0)
f(0, p_or_kw=0, kw=0)
f(p=0, p_or_kw=0, kw=0) # E: Unexpected keyword argument "p" for "f"
[case testPEP570Signatures1]
def f(p1: bytes, p2: float, /, p_or_kw: int, *, kw: str) -> None:
reveal_type(p1) # N: Revealed type is 'builtins.bytes'
reveal_type(p2) # N: Revealed type is 'builtins.float'
reveal_type(p_or_kw) # N: Revealed type is 'builtins.int'
reveal_type(kw) # N: Revealed type is 'builtins.str'
[case testPEP570Signatures2]
def f(p1: bytes, p2: float = 0.0, /, p_or_kw: int = 0, *, kw: str) -> None:
reveal_type(p1) # N: Revealed type is 'builtins.bytes'
reveal_type(p2) # N: Revealed type is 'builtins.float'
reveal_type(p_or_kw) # N: Revealed type is 'builtins.int'
reveal_type(kw) # N: Revealed type is 'builtins.str'
[case testPEP570Signatures3]
def f(p1: bytes, p2: float = 0.0, /, *, kw: int) -> None:
reveal_type(p1) # N: Revealed type is 'builtins.bytes'
reveal_type(p2) # N: Revealed type is 'builtins.float'
reveal_type(kw) # N: Revealed type is 'builtins.int'
[case testPEP570Signatures4]
def f(p1: bytes, p2: int = 0, /) -> None:
reveal_type(p1) # N: Revealed type is 'builtins.bytes'
reveal_type(p2) # N: Revealed type is 'builtins.int'
[case testPEP570Signatures5]
def f(p1: bytes, p2: float, /, p_or_kw: int) -> None:
reveal_type(p1) # N: Revealed type is 'builtins.bytes'
reveal_type(p2) # N: Revealed type is 'builtins.float'
reveal_type(p_or_kw) # N: Revealed type is 'builtins.int'
[case testPEP570Signatures6]
def f(p1: bytes, p2: float, /) -> None:
reveal_type(p1) # N: Revealed type is 'builtins.bytes'
reveal_type(p2) # N: Revealed type is 'builtins.float'
[case testWalrus]
# flags: --strict-optional
from typing import NamedTuple, Optional
from typing_extensions import Final
if a := 2:
reveal_type(a) # N: Revealed type is 'builtins.int'
while b := "x":
reveal_type(b) # N: Revealed type is 'builtins.str'
def f(x: int = (c := 4)) -> int:
if a := 2:
reveal_type(a) # N: Revealed type is 'builtins.int'
while b := "x":
reveal_type(b) # N: Revealed type is 'builtins.str'
x = (y := 1) + (z := 2)
reveal_type(x) # N: Revealed type is 'builtins.int'
reveal_type(y) # N: Revealed type is 'builtins.int'
reveal_type(z) # N: Revealed type is 'builtins.int'
l = [y2 := 1, y2 + 2, y2 + 3]
reveal_type(y2) # N: Revealed type is 'builtins.int'
reveal_type(l) # N: Revealed type is 'builtins.list[builtins.int*]'
filtered_data = [y3 for x in l if (y3 := a) is not None]
reveal_type(filtered_data) # N: Revealed type is 'builtins.list[builtins.int*]'
reveal_type(y3) # N: Revealed type is 'builtins.int'
# https://www.python.org/dev/peps/pep-0572/#exceptional-cases
(y4 := 3)
reveal_type(y4) # N: Revealed type is 'builtins.int'
y5 = (y6 := 3)
reveal_type(y5) # N: Revealed type is 'builtins.int'
reveal_type(y6) # N: Revealed type is 'builtins.int'
f(x=(y7 := 3))
reveal_type(y7) # N: Revealed type is 'builtins.int'
reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is 'builtins.int'
y8 # E: Name 'y8' is not defined
y7 = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
if y7 := "x": # E: Incompatible types in assignment (expression has type "str", variable has type "int")
pass
# Just make sure we don't crash on this sort of thing.
if NT := NamedTuple("NT", [("x", int)]): # E: "int" not callable
z2: NT # E: Variable "NT" is not valid as a type
if Alias := int:
z3: Alias # E: Variable "Alias" is not valid as a type
return (y9 := 3) + y9
reveal_type(c) # N: Revealed type is 'builtins.int'
def check_final() -> None:
x: Final = 3
if x := 4: # E: Cannot assign to final name "x"
pass
def check_binder(x: Optional[int], y: Optional[int], z: Optional[int], a: Optional[int],
b: Optional[int]) -> None:
reveal_type(x) # N: Revealed type is 'Union[builtins.int, None]'
(x := 1)
reveal_type(x) # N: Revealed type is 'builtins.int'
if x or (y := 1):
reveal_type(y) # N: Revealed type is 'Union[builtins.int, None]'
if x and (y := 1):
# TODO should just be int
# This is because in check_boolean_op in checkexpr.py we accept the right conditional
# within a binder frame context, so the types assigned in it are lost later. Perhaps
# we need to make find_isinstance_check() walrus-aware.
reveal_type(y) # N: Revealed type is 'Union[builtins.int, None]'
if (a := 1) and x:
reveal_type(a) # N: Revealed type is 'builtins.int'
if (b := 1) or x:
reveal_type(b) # N: Revealed type is 'builtins.int'
if z := 1:
reveal_type(z) # N: Revealed type is 'builtins.int'
def check_partial() -> None:
x = None
if bool() and (x := 2):
pass
reveal_type(x) # N: Revealed type is 'Union[builtins.int, None]'
def check_partial_list() -> None:
if (x := []):
x.append(3)
reveal_type(x) # N: Revealed type is 'builtins.list[builtins.int]'
[builtins fixtures/f_string.pyi]