| [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 # E: Unused "type: ignore" comment | |
| def f(): ... # type: ignore | |
| [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" | |
| ) | |
| [builtins fixtures/tuple.pyi] | |
| [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" | |
| ) | |
| [builtins fixtures/tuple.pyi] | |
| [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 | |
| [builtins fixtures/primitives.pyi] | |
| [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 testPEP570ParamTypesMissing] | |
| # flags: --disallow-untyped-defs | |
| def f(x, /) -> None: ... # E: Function is missing a type annotation for one or more parameters | |
| [case testPEP570ParamTypesBadDefault] | |
| def f(x: int = "ERROR", /) -> None: ... # E: Incompatible default for parameter "x" (default has type "str", parameter has type "int") | |
| [case testPEP570ParamTypesDefault] | |
| def f(x: int = 0, /) -> None: | |
| reveal_type(x) # N: Revealed type is "builtins.int" | |
| [case testPEP570ParamTypesRequired] | |
| def f(x: int, /) -> None: | |
| reveal_type(x) # N: Revealed type is "builtins.int" | |
| [case testPEP570Required] | |
| def f(x: int, /) -> None: ... | |
| f(1) | |
| f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int" | |
| f(x=1) # E: Unexpected keyword argument "x" for "f" | |
| f(x="ERROR") # E: Unexpected keyword argument "x" for "f" | |
| [case testPEP570Default] | |
| def f(x: int = 0, /) -> None: ... | |
| f() | |
| f(1) | |
| f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int" | |
| f(x=1) # E: Unexpected keyword argument "x" for "f" | |
| f(x="ERROR") # E: Unexpected keyword argument "x" for "f" | |
| [case testPEP570Calls] | |
| # flags: --no-strict-optional | |
| from typing import Any, Dict | |
| def f(p, /, p_or_kw, *, kw) -> None: ... | |
| d = None # type: Dict[Any, Any] | |
| 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" | |
| f(0, **d) | |
| f(**d) # E: Missing positional argument "p_or_kw" in call to "f" | |
| [builtins fixtures/dict.pyi] | |
| [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 testPEP570Unannotated] | |
| def f(arg, /): ... | |
| g = lambda arg, /: arg | |
| def h(arg=0, /): ... | |
| i = lambda arg=0, /: arg | |
| f(1) | |
| reveal_type(g(1)) # N: Revealed type is "Any" | |
| h() | |
| h(1) | |
| reveal_type(i()) # N: Revealed type is "Any" | |
| reveal_type(i(1)) # N: Revealed type is "Any" | |
| f(arg=0) # E: Unexpected keyword argument "arg" for "f" | |
| g(arg=0) # E: Unexpected keyword argument "arg" | |
| h(arg=0) # E: Unexpected keyword argument "arg" for "h" | |
| i(arg=0) # E: Unexpected keyword argument "arg" | |
| [case testWalrus] | |
| from typing import Final, NamedTuple, Optional, List | |
| if a := 2: | |
| reveal_type(a) # N: Revealed type is "builtins.int" | |
| while b := "x": | |
| reveal_type(b) # N: Revealed type is "builtins.str" | |
| 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" | |
| d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2} | |
| reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | |
| reveal_type(a2) # N: Revealed type is "builtins.int" | |
| d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2} | |
| reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | |
| reveal_type(prefix) # N: Revealed type is "builtins.str" | |
| reveal_type(start_val) # N: Revealed type is "builtins.int" | |
| filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2} | |
| reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | |
| reveal_type(new_v) # N: Revealed type is "builtins.int" | |
| 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" | |
| d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2} | |
| reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | |
| reveal_type(a2) # N: Revealed type is "builtins.int" | |
| d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2} | |
| reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | |
| reveal_type(prefix) # N: Revealed type is "builtins.str" | |
| reveal_type(start_val) # N: Revealed type is "builtins.int" | |
| filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2} | |
| reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | |
| reveal_type(new_v) # 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 \ | |
| # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases | |
| if Alias := int: # E: Function "Alias" could always be true in boolean context | |
| z3: Alias # E: Variable "Alias" is not valid as a type \ | |
| # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases | |
| if (reveal_type(y9 := 3) and # N: Revealed type is "Literal[3]?" | |
| reveal_type(y9)): # N: Revealed type is "builtins.int" | |
| reveal_type(y9) # N: Revealed type is "builtins.int" | |
| return (y10 := 3) + y10 | |
| 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 "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 "builtins.int | None" | |
| if x and (y := 1): | |
| reveal_type(y) # N: Revealed type is "builtins.int" | |
| 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 "builtins.int | None" | |
| def check_narrow(x: Optional[int], s: List[int]) -> None: | |
| if (y := x): | |
| reveal_type(y) # N: Revealed type is "builtins.int" | |
| if (y := x) is not None: | |
| reveal_type(y) # N: Revealed type is "builtins.int" | |
| if (y := x) == 10: | |
| reveal_type(y) # N: Revealed type is "builtins.int" | |
| if (y := x) in s: | |
| reveal_type(y) # N: Revealed type is "builtins.int" | |
| if isinstance((y := x), int): | |
| reveal_type(y) # N: Revealed type is "builtins.int" | |
| class AssignmentExpressionsClass: | |
| x = (y := 1) + (z := 2) | |
| reveal_type(z) # N: Revealed type is "builtins.int" | |
| l = [x2 := 1, 2, 3] | |
| reveal_type(x2) # N: Revealed type is "builtins.int" | |
| def __init__(self) -> None: | |
| reveal_type(self.z) # N: Revealed type is "builtins.int" | |
| l = [z2 := 1, z2 + 2, z2 + 3] | |
| reveal_type(z2) # N: Revealed type is "builtins.int" | |
| reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]" | |
| filtered_data = [z3 for x in l if (z3 := 1) is not None] | |
| reveal_type(filtered_data) # N: Revealed type is "builtins.list[builtins.int]" | |
| reveal_type(z3) # N: Revealed type is "builtins.int" | |
| # Assignment expressions from inside the class should not escape the class scope. | |
| reveal_type(x2) # E: Name "x2" is not defined # N: Revealed type is "Any" | |
| reveal_type(z2) # E: Name "z2" is not defined # N: Revealed type is "Any" | |
| [builtins fixtures/isinstancelist.pyi] | |
| [case testWalrusConditionalTypeBinder] | |
| from typing import Literal, Tuple, Union | |
| class Good: | |
| @property | |
| def is_good(self) -> Literal[True]: ... | |
| class Bad: | |
| @property | |
| def is_good(self) -> Literal[False]: ... | |
| def get_thing() -> Union[Good, Bad]: ... | |
| if (thing := get_thing()).is_good: | |
| reveal_type(thing) # N: Revealed type is "__main__.Good" | |
| else: | |
| reveal_type(thing) # N: Revealed type is "__main__.Bad" | |
| def get_things() -> Union[Tuple[Good], Tuple[Bad]]: ... | |
| if (things := get_things())[0].is_good: | |
| reveal_type(things) # N: Revealed type is "tuple[__main__.Good]" | |
| else: | |
| reveal_type(things) # N: Revealed type is "tuple[__main__.Bad]" | |
| [builtins fixtures/list.pyi] | |
| [case testWalrusConditionalTypeCheck] | |
| from typing import Optional | |
| maybe_str: Optional[str] | |
| if (is_str := maybe_str is not None): | |
| reveal_type(is_str) # N: Revealed type is "Literal[True]" | |
| reveal_type(maybe_str) # N: Revealed type is "builtins.str" | |
| else: | |
| reveal_type(is_str) # N: Revealed type is "Literal[False]" | |
| reveal_type(maybe_str) # N: Revealed type is "None" | |
| reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" | |
| [builtins fixtures/bool.pyi] | |
| [case testWalrusConditionalTypeCheck2] | |
| from typing import Optional | |
| maybe_str: Optional[str] | |
| if (x := maybe_str) is not None: | |
| reveal_type(x) # N: Revealed type is "builtins.str" | |
| reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" | |
| else: | |
| reveal_type(x) # N: Revealed type is "None" | |
| reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" | |
| reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" | |
| [builtins fixtures/bool.pyi] | |
| [case testWalrusPartialTypes] | |
| from typing import List | |
| def check_partial_list() -> None: | |
| if (x := []): # E: Need type annotation for "x" (hint: "x: list[<type>] = ...") | |
| pass | |
| y: List[str] | |
| if (y := []): | |
| pass | |
| if (z := []): | |
| z.append(3) | |
| reveal_type(z) # N: Revealed type is "builtins.list[builtins.int]" | |
| [builtins fixtures/list.pyi] | |
| [case testWalrusAssignmentAndConditionScopeForLiteral] | |
| # flags: --warn-unreachable | |
| if (x := 0): | |
| reveal_type(x) # E: Statement is unreachable | |
| else: | |
| reveal_type(x) # N: Revealed type is "Literal[0]" | |
| reveal_type(x) # N: Revealed type is "Literal[0]" | |
| [case testWalrusAssignmentAndConditionScopeForProperty] | |
| # flags: --warn-unreachable | |
| from typing import Literal | |
| class PropertyWrapper: | |
| @property | |
| def f(self) -> str: ... | |
| @property | |
| def always_false(self) -> Literal[False]: ... | |
| wrapper = PropertyWrapper() | |
| if x := wrapper.f: | |
| reveal_type(x) # N: Revealed type is "builtins.str" | |
| else: | |
| reveal_type(x) # N: Revealed type is "Literal['']" | |
| reveal_type(x) # N: Revealed type is "builtins.str" | |
| if y := wrapper.always_false: | |
| reveal_type(y) # E: Statement is unreachable | |
| else: | |
| reveal_type(y) # N: Revealed type is "Literal[False]" | |
| reveal_type(y) # N: Revealed type is "Literal[False]" | |
| [builtins fixtures/property.pyi] | |
| [case testWalrusAssignmentAndConditionScopeForFunction] | |
| # flags: --warn-unreachable | |
| from typing import Literal | |
| def f() -> str: ... | |
| if x := f(): | |
| reveal_type(x) # N: Revealed type is "builtins.str" | |
| else: | |
| reveal_type(x) # N: Revealed type is "Literal['']" | |
| reveal_type(x) # N: Revealed type is "builtins.str" | |
| def always_false() -> Literal[False]: ... | |
| if y := always_false(): | |
| reveal_type(y) # E: Statement is unreachable | |
| else: | |
| reveal_type(y) # N: Revealed type is "Literal[False]" | |
| reveal_type(y) # N: Revealed type is "Literal[False]" | |
| def always_false_with_parameter(x: int) -> Literal[False]: ... | |
| if z := always_false_with_parameter(5): | |
| reveal_type(z) # E: Statement is unreachable | |
| else: | |
| reveal_type(z) # N: Revealed type is "Literal[False]" | |
| reveal_type(z) # N: Revealed type is "Literal[False]" | |
| [builtins fixtures/tuple.pyi] | |
| [case testWalrusExpr] | |
| def func() -> None: | |
| foo = Foo() | |
| if x := foo.x: | |
| pass | |
| class Foo: | |
| def __init__(self) -> None: | |
| self.x = 123 | |
| [case testWalrusTypeGuard] | |
| from typing_extensions import TypeGuard | |
| def is_float(a: object) -> TypeGuard[float]: pass | |
| def main(a: object) -> None: | |
| if is_float(x := a): | |
| reveal_type(x) # N: Revealed type is "builtins.float" | |
| reveal_type(a) # N: Revealed type is "builtins.object" | |
| [builtins fixtures/tuple.pyi] | |
| [case testWalrusRedefined] | |
| def foo() -> None: | |
| x = 0 | |
| [x := x + y for y in [1, 2, 3]] | |
| [builtins fixtures/dict.pyi] | |
| [case testWalrusUsedBeforeDef] | |
| class C: | |
| def f(self, c: 'C') -> None: pass | |
| (x := C()).f(y) # E: Cannot determine type of "y" # E: Name "y" is used before definition | |
| (y := C()).f(y) | |
| [case testOverloadWithPositionalOnlySelf] | |
| from typing import overload, Optional | |
| class Foo: | |
| @overload | |
| def f(self, a: str, /) -> None: ... | |
| @overload | |
| def f(self, *, b: bool = False) -> None: ... | |
| def f(self, a: Optional[str] = None, /, *, b: bool = False) -> None: # E: Overloaded function implementation does not accept all possible parameters of signature 2 | |
| ... | |
| class Bar: | |
| @overload | |
| def f(self, a: str, /) -> None: ... | |
| @overload # Notice `/` in sig below: | |
| def f(self, /, *, b: bool = False) -> None: ... | |
| def f(self, a: Optional[str] = None, /, *, b: bool = False) -> None: | |
| ... | |
| [builtins fixtures/bool.pyi] | |
| [case testOverloadPositionalOnlyErrorMessage] | |
| from typing import overload | |
| @overload | |
| def foo(a: int, /): ... | |
| @overload | |
| def foo(a: str): ... | |
| def foo(a): ... | |
| foo(a=1) | |
| [out] | |
| main:9: error: No overload variant of "foo" matches argument type "int" | |
| main:9: note: Possible overload variants: | |
| main:9: note: def foo(int, /) -> Any | |
| main:9: note: def foo(a: str) -> Any | |
| [case testOverloadPositionalOnlyErrorMessageAllTypes] | |
| from typing import overload | |
| @overload | |
| def foo(a: int, /, b: int, *, c: int): ... | |
| @overload | |
| def foo(a: str, b: int, *, c: int): ... | |
| def foo(a, b, *, c): ... | |
| foo(a=1) | |
| [out] | |
| main:9: error: No overload variant of "foo" matches argument type "int" | |
| main:9: note: Possible overload variants: | |
| main:9: note: def foo(int, /, b: int, *, c: int) -> Any | |
| main:9: note: def foo(a: str, b: int, *, c: int) -> Any | |
| [case testOverloadPositionalOnlyErrorMessageMultiplePosArgs] | |
| from typing import overload | |
| @overload | |
| def foo(a: int, b: int, c: int, /, d: str): ... | |
| @overload | |
| def foo(a: str, b: int, c: int, d: str): ... | |
| def foo(a, b, c, d): ... | |
| foo(a=1) | |
| [out] | |
| main:9: error: No overload variant of "foo" matches argument type "int" | |
| main:9: note: Possible overload variants: | |
| main:9: note: def foo(int, int, int, /, d: str) -> Any | |
| main:9: note: def foo(a: str, b: int, c: int, d: str) -> Any | |
| [case testOverloadPositionalOnlyErrorMessageMethod] | |
| from typing import overload | |
| class Some: | |
| @overload | |
| def foo(self, __a: int): ... | |
| @overload | |
| def foo(self, a: float, /): ... | |
| @overload | |
| def foo(self, a: str): ... | |
| def foo(self, a): ... | |
| Some().foo(a=1) | |
| [out] | |
| main:12: error: No overload variant of "foo" of "Some" matches argument type "int" | |
| main:12: note: Possible overload variants: | |
| main:12: note: def foo(self, int, /) -> Any | |
| main:12: note: def foo(self, float, /) -> Any | |
| main:12: note: def foo(self, a: str) -> Any | |
| [case testOverloadPositionalOnlyErrorMessageClassMethod] | |
| from typing import overload | |
| class Some: | |
| @overload | |
| @classmethod | |
| def foo(cls, __a: int): ... | |
| @overload | |
| @classmethod | |
| def foo(cls, a: float, /): ... | |
| @overload | |
| @classmethod | |
| def foo(cls, a: str): ... | |
| @classmethod | |
| def foo(cls, a): ... | |
| Some.foo(a=1) | |
| [builtins fixtures/classmethod.pyi] | |
| [out] | |
| main:16: error: No overload variant of "foo" of "Some" matches argument type "int" | |
| main:16: note: Possible overload variants: | |
| main:16: note: def foo(cls, int, /) -> Any | |
| main:16: note: def foo(cls, float, /) -> Any | |
| main:16: note: def foo(cls, a: str) -> Any | |
| [case testUnpackWithDuplicateNamePositionalOnly] | |
| from typing import TypedDict | |
| from typing_extensions import Unpack | |
| class Person(TypedDict): | |
| name: str | |
| age: int | |
| def foo(name: str, /, **kwargs: Unpack[Person]) -> None: # Allowed | |
| ... | |
| [builtins fixtures/dict.pyi] | |
| [typing fixtures/typing-typeddict.pyi] | |
| [case testPossiblyUndefinedWithAssignmentExpr] | |
| # flags: --enable-error-code possibly-undefined | |
| def f1() -> None: | |
| d = {0: 1} | |
| if int(): | |
| x = 1 | |
| if (x := d[x]) is None: # E: Name "x" may be undefined | |
| y = x | |
| z = x | |
| [builtins fixtures/dict.pyi] | |
| [case testNarrowOnSelfInGeneric] | |
| from typing import Generic, TypeVar, Optional | |
| T = TypeVar("T", int, str) | |
| class C(Generic[T]): | |
| x: Optional[T] | |
| def meth(self) -> Optional[T]: | |
| if (y := self.x) is not None: | |
| reveal_type(y) | |
| return None | |
| [out] | |
| main:9: note: Revealed type is "builtins.int" | |
| main:9: note: Revealed type is "builtins.str" | |
| [case testTypeGuardWithPositionalOnlyArg] | |
| from typing_extensions import TypeGuard | |
| def typeguard(x: object, /) -> TypeGuard[int]: | |
| ... | |
| n: object | |
| if typeguard(n): | |
| reveal_type(n) | |
| [builtins fixtures/tuple.pyi] | |
| [out] | |
| main:8: note: Revealed type is "builtins.int" | |
| [case testTypeGuardKeywordFollowingWalrus] | |
| from typing import cast | |
| from typing_extensions import TypeGuard | |
| def typeguard(x: object) -> TypeGuard[int]: | |
| ... | |
| if typeguard(x=(n := cast(object, "hi"))): | |
| reveal_type(n) | |
| [builtins fixtures/tuple.pyi] | |
| [out] | |
| main:8: note: Revealed type is "builtins.int" | |
| [case testWalrusAssignmentAcrossCallArguments] | |
| def f(notifications: str, initial_reference: str) -> int: | |
| return 42 | |
| f( | |
| initial_reference=(request_reference := "abc"), | |
| notifications=reveal_type(request_reference), # N: Revealed type is "builtins.str" | |
| ) | |
| [case testNoCrashOnAssignmentExprClass] | |
| class C: | |
| [(j := i) for i in [1, 2, 3]] # E: Assignment expression within a comprehension cannot be used in a class body | |
| [builtins fixtures/list.pyi] | |
| [case testNarrowedVariableInNestedModifiedInWalrus] | |
| from typing import Optional | |
| def walrus_with_nested_error(x: Optional[str]) -> None: | |
| if x is None: | |
| x = "a" | |
| def nested() -> str: | |
| return x # E: Incompatible return value type (got "str | None", expected "str") | |
| if x := None: | |
| pass | |
| nested() | |
| def walrus_with_nested_ok(x: Optional[str]) -> None: | |
| if x is None: | |
| x = "a" | |
| def nested() -> str: | |
| return x | |
| if y := x: | |
| pass | |
| nested() | |
| [case testIgnoreWholeModule] | |
| # flags: --warn-unused-ignores | |
| # type: ignore | |
| IGNORE # type: ignore | |
| [case testUnusedIgnoreVersionCheck] | |
| # flags: --warn-unused-ignores | |
| import sys | |
| if sys.version_info < (3, 6): | |
| 42 # type: ignore | |
| else: | |
| 42 # type: ignore # E: Unused "type: ignore" comment | |
| [builtins fixtures/ops.pyi] | |
| [case testDictExpressionErrorLocations] | |
| # flags: --pretty | |
| from typing import Dict | |
| other: Dict[str, str] | |
| dct: Dict[str, int] = {"a": "b", **other} | |
| [builtins fixtures/dict.pyi] | |
| [out] | |
| main:5: error: Dict entry 0 has incompatible type "str": "str"; expected "str": "int" | |
| dct: Dict[str, int] = {"a": "b", **other} | |
| ^~~~~~~~ | |
| main:5: error: Unpacked dict entry 1 has incompatible type "dict[str, str]"; expected "SupportsKeysAndGetItem[str, int]" | |
| dct: Dict[str, int] = {"a": "b", **other} | |
| ^~~~~ | |
| [case testWalrusAssignmentEmptyCollection] | |
| from typing import List | |
| y: List[int] | |
| if (y := []): | |
| reveal_type(y) # N: Revealed type is "builtins.list[builtins.int]" | |
| [builtins fixtures/list.pyi] | |
| [case testAssignToOptionalTupleWalrus] | |
| from typing import Optional | |
| def condition() -> bool: return False | |
| i: Optional[int] = 0 if condition() else None | |
| x: Optional[tuple[int, int]] = (i, (i := 1)) # E: Incompatible types in assignment (expression has type "tuple[int | None, int]", variable has type "tuple[int, int] | None") | |
| [builtins fixtures/tuple.pyi] | |
| [case testReturnTupleOptionalWalrus] | |
| from typing import Optional | |
| def condition() -> bool: return False | |
| def fn() -> tuple[int, int]: | |
| i: Optional[int] = 0 if condition() else None | |
| return (i, (i := i + 1)) # E: Incompatible return value type (got "tuple[int | None, int]", expected "tuple[int, int]") \ | |
| # E: Unsupported operand types for + ("None" and "int") \ | |
| # N: Left operand is of type "int | None" | |
| [builtins fixtures/dict.pyi] | |
| [case testPartialTypeResolvedByWalrusInAssignmentRvalueList] | |
| errors = [] | |
| errors = [ | |
| i for i in [1, 2, 3] if (errors := [1]) | |
| ] | |
| reveal_type(errors) # N: Revealed type is "builtins.list[builtins.int]" | |
| [builtins fixtures/list.pyi] | |
| [case testPartialTypeResolvedByCallInAssignmentRvalueDict] | |
| errors = {} | |
| errors["42"] = 1 if int() else errors.update({"43": 1}) # E: Incompatible types in assignment (expression has type "int | None", target has type "int") \ | |
| # E: "update" of "dict" does not return a value (it only ever returns None) | |
| reveal_type(errors) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | |
| [builtins fixtures/dict.pyi] |