blob: 283500f25a7de27c25e1bb7702c1f94ae8d8a237 [file] [log] [blame] [edit]
[case testTotalOrderingEqLt]
from functools import total_ordering
@total_ordering
class Ord:
def __eq__(self, other: object) -> bool:
return False
def __lt__(self, other: "Ord") -> bool:
return False
reveal_type(Ord() < Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() <= Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() == Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() > Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() >= Ord()) # N: Revealed type is "builtins.bool"
Ord() < 1 # E: Unsupported operand types for < ("Ord" and "int")
Ord() <= 1 # E: Unsupported operand types for <= ("Ord" and "int")
Ord() == 1
Ord() > 1 # E: Unsupported operand types for > ("Ord" and "int")
Ord() >= 1 # E: Unsupported operand types for >= ("Ord" and "int")
[builtins fixtures/dict.pyi]
[case testTotalOrderingLambda]
from functools import total_ordering
from typing import Any, Callable, ClassVar
@total_ordering
class Ord:
__eq__: Callable[[Any, object], bool] = lambda self, other: False
__lt__: Callable[[Any, "Ord"], bool] = lambda self, other: False
reveal_type(Ord() < Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() <= Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() == Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() > Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() >= Ord()) # N: Revealed type is "builtins.bool"
Ord() < 1 # E: Argument 1 has incompatible type "int"; expected "Ord"
Ord() <= 1 # E: Unsupported operand types for <= ("Ord" and "int")
Ord() == 1
Ord() > 1 # E: Unsupported operand types for > ("Ord" and "int")
Ord() >= 1 # E: Unsupported operand types for >= ("Ord" and "int")
[builtins fixtures/dict.pyi]
[case testTotalOrderingNonCallable]
from functools import total_ordering
@total_ordering
class Ord(object):
def __eq__(self, other: object) -> bool:
return False
__lt__ = 5
Ord() <= Ord() # E: Unsupported left operand type for <= ("Ord")
Ord() > Ord() # E: "int" not callable
Ord() >= Ord() # E: Unsupported left operand type for >= ("Ord")
[builtins fixtures/dict.pyi]
[case testTotalOrderingReturnNotBool]
from functools import total_ordering
@total_ordering
class Ord:
def __eq__(self, other: object) -> bool:
return False
def __lt__(self, other: "Ord") -> str:
return "blah"
reveal_type(Ord() < Ord()) # N: Revealed type is "builtins.str"
reveal_type(Ord() <= Ord()) # N: Revealed type is "Any"
reveal_type(Ord() == Ord()) # N: Revealed type is "builtins.bool"
reveal_type(Ord() > Ord()) # N: Revealed type is "Any"
reveal_type(Ord() >= Ord()) # N: Revealed type is "Any"
[builtins fixtures/dict.pyi]
[case testTotalOrderingAllowsAny]
from functools import total_ordering
@total_ordering
class Ord:
def __eq__(self, other):
return False
def __gt__(self, other):
return False
reveal_type(Ord() < Ord()) # N: Revealed type is "Any"
Ord() <= Ord() # E: Unsupported left operand type for <= ("Ord")
reveal_type(Ord() == Ord()) # N: Revealed type is "Any"
reveal_type(Ord() > Ord()) # N: Revealed type is "Any"
Ord() >= Ord() # E: Unsupported left operand type for >= ("Ord")
Ord() < 1 # E: Unsupported left operand type for < ("Ord")
Ord() <= 1 # E: Unsupported left operand type for <= ("Ord")
Ord() == 1
Ord() > 1
Ord() >= 1 # E: Unsupported left operand type for >= ("Ord")
[builtins fixtures/dict.pyi]
[case testCachedProperty]
from functools import cached_property
class Parent:
@property
def f(self) -> str: pass
class Child(Parent):
@cached_property
def f(self) -> str: pass
@cached_property
def g(self) -> int: pass
@cached_property # E: Too many arguments for property
def h(self, arg) -> int: pass
reveal_type(Parent().f) # N: Revealed type is "builtins.str"
reveal_type(Child().f) # N: Revealed type is "builtins.str"
reveal_type(Child().g) # N: Revealed type is "builtins.int"
Child().f = "Hello World"
Child().g = "invalid" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file functools.pyi]
import sys
from typing import TypeVar, Generic
_T = TypeVar('_T')
class cached_property(Generic[_T]): ...
[builtins fixtures/property.pyi]
[case testTotalOrderingWithForwardReference]
from typing import Generic, Any, TypeVar
import functools
T = TypeVar("T", bound="C")
@functools.total_ordering
class D(Generic[T]):
def __lt__(self, other: Any) -> bool:
...
class C:
pass
def f(d: D[C]) -> None:
reveal_type(d.__gt__) # N: Revealed type is "def (other: Any) -> builtins.bool"
d: D[int] # E: Type argument "int" of "D" must be a subtype of "C"
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialBasic]
from typing import Callable
import functools
def foo(a: int, b: str, c: int = 5) -> int: ... # N: "foo" defined here
p1 = functools.partial(foo)
p1(1, "a", 3) # OK
p1(1, "a", c=3) # OK
p1(1, b="a", c=3) # OK
reveal_type(p1) # N: Revealed type is "functools.partial[builtins.int]"
def takes_callable_int(f: Callable[..., int]) -> None: ...
def takes_callable_str(f: Callable[..., str]) -> None: ...
takes_callable_int(p1)
takes_callable_str(p1) # E: Argument 1 to "takes_callable_str" has incompatible type "partial[int]"; expected "Callable[..., str]" \
# N: "partial[int].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], int]"
p2 = functools.partial(foo, 1)
p2("a") # OK
p2("a", 3) # OK
p2("a", c=3) # OK
p2(1, 3) # E: Argument 1 to "foo" has incompatible type "int"; expected "str"
p2(1, "a", 3) # E: Too many arguments for "foo" \
# E: Argument 1 to "foo" has incompatible type "int"; expected "str" \
# E: Argument 2 to "foo" has incompatible type "str"; expected "int"
p2(a=1, b="a", c=3) # E: Unexpected keyword argument "a" for "foo"
p3 = functools.partial(foo, b="a")
p3(1) # OK
p3(1, c=3) # OK
p3(a=1) # OK
p3(1, b="a", c=3) # OK, keywords can be clobbered
p3(1, 3) # E: Too many positional arguments for "foo" \
# E: Argument 2 to "foo" has incompatible type "int"; expected "str"
functools.partial(foo, "a") # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
functools.partial(foo, b=1) # E: Argument "b" to "foo" has incompatible type "int"; expected "str"
functools.partial(foo, a=1, b=2, c=3) # E: Argument "b" to "foo" has incompatible type "int"; expected "str"
functools.partial(1) # E: "int" not callable \
# E: Argument 1 to "partial" has incompatible type "int"; expected "Callable[..., Never]"
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialStar]
import functools
def foo(a: int, b: str, *args: int, d: str, **kwargs: int) -> int: ...
p1 = functools.partial(foo, 1, d="a", x=9)
p1("a", 2, 3, 4) # OK
p1("a", 2, 3, 4, d="a") # OK
p1("a", 2, 3, 4, "a") # E: Argument 5 to "foo" has incompatible type "str"; expected "int"
p1("a", 2, 3, 4, x="a") # E: Argument "x" to "foo" has incompatible type "str"; expected "int"
p2 = functools.partial(foo, 1, "a")
p2(2, 3, 4, d="a") # OK
p2("a") # E: Missing named argument "d" for "foo" \
# E: Argument 1 to "foo" has incompatible type "str"; expected "int"
p2(2, 3, 4) # E: Missing named argument "d" for "foo"
functools.partial(foo, 1, "a", "b", "c", d="a") # E: Argument 3 to "foo" has incompatible type "str"; expected "int" \
# E: Argument 4 to "foo" has incompatible type "str"; expected "int"
def bar(*a: bytes, **k: int):
p1("a", 2, 3, 4, d="a", **k)
p1("a", d="a", **k)
p1("a", **k) # E: Argument 2 to "foo" has incompatible type "**Dict[str, int]"; expected "str"
p1(**k) # E: Argument 1 to "foo" has incompatible type "**Dict[str, int]"; expected "str"
p1(*a) # E: List or tuple expected as variadic arguments
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialGeneric]
from typing import TypeVar
import functools
T = TypeVar("T")
U = TypeVar("U")
def foo(a: T, b: T) -> T: ...
p1 = functools.partial(foo, 1)
reveal_type(p1(2)) # N: Revealed type is "builtins.int"
p1("a") # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
p2 = functools.partial(foo, "a")
p2(1) # E: Argument 1 to "foo" has incompatible type "int"; expected "str"
reveal_type(p2("a")) # N: Revealed type is "builtins.str"
def bar(a: T, b: U) -> U: ...
p3 = functools.partial(bar, 1)
reveal_type(p3(2)) # N: Revealed type is "builtins.int"
reveal_type(p3("a")) # N: Revealed type is "builtins.str"
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialCallable]
from typing import Callable
import functools
def main1(f: Callable[[int, str], int]) -> None:
p = functools.partial(f, 1)
p("a") # OK
p(1) # E: Argument 1 has incompatible type "int"; expected "str"
functools.partial(f, a=1) # E: Unexpected keyword argument "a"
class CallbackProto:
def __call__(self, a: int, b: str) -> int: ...
def main2(f: CallbackProto) -> None:
p = functools.partial(f, b="a")
p(1) # OK
p("a") # E: Argument 1 to "__call__" of "CallbackProto" has incompatible type "str"; expected "int"
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialOverload]
from typing import overload
import functools
@overload
def foo(a: int, b: str) -> int: ...
@overload
def foo(a: str, b: int) -> str: ...
def foo(*a, **k): ...
p1 = functools.partial(foo)
reveal_type(p1(1, "a")) # N: Revealed type is "builtins.int"
reveal_type(p1("a", 1)) # N: Revealed type is "builtins.int"
p1(1, 2) # TODO: false negative
p1("a", "b") # TODO: false negative
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialTypeGuard]
import functools
from typing_extensions import TypeGuard
def is_str_list(val: list[object]) -> TypeGuard[list[str]]: ... # E: "list" is not subscriptable, use "typing.List" instead
reveal_type(functools.partial(is_str_list, [1, 2, 3])) # N: Revealed type is "functools.partial[builtins.bool]"
reveal_type(functools.partial(is_str_list, [1, 2, 3])()) # N: Revealed type is "builtins.bool"
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialType]
import functools
from typing import Type
class A:
def __init__(self, a: int, b: str) -> None: ... # N: "A" defined here
p = functools.partial(A, 1)
reveal_type(p) # N: Revealed type is "functools.partial[__main__.A]"
p("a") # OK
p(1) # E: Argument 1 to "A" has incompatible type "int"; expected "str"
p(z=1) # E: Unexpected keyword argument "z" for "A"
def main(t: Type[A]) -> None:
p = functools.partial(t, 1)
reveal_type(p) # N: Revealed type is "functools.partial[__main__.A]"
p("a") # OK
p(1) # E: Argument 1 to "A" has incompatible type "int"; expected "str"
p(z=1) # E: Unexpected keyword argument "z" for "A"
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialTypeVarTuple]
import functools
import typing
Ts = typing.TypeVarTuple("Ts")
def foo(fn: typing.Callable[[typing.Unpack[Ts]], None], /, *arg: typing.Unpack[Ts], kwarg: str) -> None: ...
p = functools.partial(foo, kwarg="asdf")
def bar(a: int, b: str, c: float) -> None: ...
p(bar, 1, "a", 3.0) # OK
p(bar, 1, "a", 3.0, kwarg="asdf") # OK
p(bar, 1, "a", "b") # E: Argument 1 to "foo" has incompatible type "Callable[[int, str, float], None]"; expected "Callable[[int, str, str], None]"
[builtins fixtures/dict.pyi]
[case testFunctoolsPartialUnion]
import functools
from typing import Any, Callable, Union
cls1: Any
cls2: Union[Any, Any]
reveal_type(functools.partial(cls1, 2)()) # N: Revealed type is "Any"
reveal_type(functools.partial(cls2, 2)()) # N: Revealed type is "Any"
fn1: Union[Callable[[int], int], Callable[[int], int]]
reveal_type(functools.partial(fn1, 2)()) # N: Revealed type is "builtins.int"
fn2: Union[Callable[[int], int], Callable[[int], str]]
reveal_type(functools.partial(fn2, 2)()) # N: Revealed type is "builtins.object"
fn3: Union[Callable[[int], int], str]
reveal_type(functools.partial(fn3, 2)()) # E: "str" not callable \
# E: "Union[Callable[[int], int], str]" not callable \
# N: Revealed type is "builtins.int" \
# E: Argument 1 to "partial" has incompatible type "Union[Callable[[int], int], str]"; expected "Callable[..., int]"
[builtins fixtures/tuple.pyi]
[case testFunctoolsPartialTypeObject]
import functools
from typing import Type, Generic, TypeVar
class A:
def __init__(self, val: int) -> None: ...
cls1: Type[A]
reveal_type(functools.partial(cls1, 2)()) # N: Revealed type is "__main__.A"
functools.partial(cls1, "asdf") # E: Argument 1 to "A" has incompatible type "str"; expected "int"
T = TypeVar("T")
class B(Generic[T]):
def __init__(self, val: T) -> None: ...
cls2: Type[B[int]]
reveal_type(functools.partial(cls2, 2)()) # N: Revealed type is "__main__.B[builtins.int]"
functools.partial(cls2, "asdf") # E: Argument 1 to "B" has incompatible type "str"; expected "int"
def foo(cls3: Type[B[T]]):
reveal_type(functools.partial(cls3, "asdf")) # N: Revealed type is "functools.partial[__main__.B[T`-1]]" \
# E: Argument 1 to "B" has incompatible type "str"; expected "T"
reveal_type(functools.partial(cls3, 2)()) # N: Revealed type is "__main__.B[T`-1]" \
# E: Argument 1 to "B" has incompatible type "int"; expected "T"
[builtins fixtures/tuple.pyi]