Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Merge branch 'master' into paramspec-class-constructor
  • Loading branch information
hauntsaninja authored Nov 28, 2022
commit 91e3a352b2ac8ee640483c52a7f62ea1bda10a36
141 changes: 141 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1315,3 +1315,144 @@ func(SomeClass, constructor)
func(SomeClass, wrong_constructor) # E: Argument 1 to "func" has incompatible type "Type[SomeClass]"; expected "Callable[[VarArg(<nothing>), KwArg(<nothing>)], SomeClass]" \
# E: Argument 2 to "func" has incompatible type "Callable[[bool], SomeClass]"; expected "Callable[[VarArg(<nothing>), KwArg(<nothing>)], SomeClass]"
[builtins fixtures/paramspec.pyi]

[case testParamSpecInTypeAliasBasic]
from typing import ParamSpec, Callable

P = ParamSpec("P")
C = Callable[P, int]
def f(n: C[P]) -> C[P]: ...

@f
def bar(x: int) -> int: ...
@f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "Callable[[int], int]"
def foo(x: int) -> str: ...

x: C[[int, str]]
reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
y: C[int, str]
reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
[builtins fixtures/paramspec.pyi]

[case testParamSpecInTypeAliasConcatenate]
from typing import ParamSpec, Callable
from typing_extensions import Concatenate

P = ParamSpec("P")
C = Callable[Concatenate[int, P], int]
def f(n: C[P]) -> C[P]: ...

@f # E: Argument 1 to "f" has incompatible type "Callable[[], int]"; expected "Callable[[int], int]"
def bad() -> int: ...

@f
def bar(x: int) -> int: ...

@f
def bar2(x: int, y: str) -> int: ...
reveal_type(bar2) # N: Revealed type is "def (builtins.int, y: builtins.str) -> builtins.int"

@f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "Callable[[int], int]" \
# N: This is likely because "foo" has named arguments: "x". Consider marking them positional-only
def foo(x: int) -> str: ...

@f # E: Argument 1 to "f" has incompatible type "Callable[[str, int], int]"; expected "Callable[[int, int], int]" \
# N: This is likely because "foo2" has named arguments: "x". Consider marking them positional-only
def foo2(x: str, y: int) -> int: ...

x: C[[int, str]]
reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str) -> builtins.int"
y: C[int, str]
reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str) -> builtins.int"
[builtins fixtures/paramspec.pyi]

[case testParamSpecInTypeAliasRecursive]
from typing import ParamSpec, Callable, Union

P = ParamSpec("P")
C = Callable[P, Union[int, C[P]]]
def f(n: C[P]) -> C[P]: ...

@f
def bar(x: int) -> int: ...

@f
def bar2(__x: int) -> Callable[[int], int]: ...

@f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "C[[int]]"
def foo(x: int) -> str: ...

@f # E: Argument 1 to "f" has incompatible type "Callable[[int], Callable[[int], str]]"; expected "C[[int]]"
def foo2(__x: int) -> Callable[[int], str]: ...

x: C[[int, str]]
reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> Union[builtins.int, ...]"
y: C[int, str]
reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> Union[builtins.int, ...]"
[builtins fixtures/paramspec.pyi]

[case testParamSpecAliasInRuntimeContext]
from typing import ParamSpec, Generic

P = ParamSpec("P")
class C(Generic[P]): ...

c = C[int, str]()
reveal_type(c) # N: Revealed type is "__main__.C[[builtins.int, builtins.str]]"

A = C[P]
a = A[int, str]()
reveal_type(a) # N: Revealed type is "__main__.C[[builtins.int, builtins.str]]"
[builtins fixtures/paramspec.pyi]

[case testParamSpecAliasInvalidLocations]
from typing import ParamSpec, Generic, List, TypeVar, Callable

P = ParamSpec("P")
T = TypeVar("T")
A = List[T]
def f(x: A[[int, str]]) -> None: ... # E: Bracketed expression "[...]" is not valid as a type \
# N: Did you mean "List[...]"?
def g(x: A[P]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'

C = Callable[P, T]
x: C[int] # E: Bad number of arguments for type alias, expected: 2, given: 1
y: C[int, str] # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
z: C[int, str, bytes] # E: Bad number of arguments for type alias, expected: 2, given: 3
[builtins fixtures/paramspec.pyi]

[case testTrivialParametersHandledCorrectly]
from typing import ParamSpec, Generic, TypeVar, Callable, Any
from typing_extensions import Concatenate

P = ParamSpec("P")
T = TypeVar("T")
S = TypeVar("S")

class C(Generic[S, P, T]): ...

def foo(f: Callable[P, int]) -> None:
x: C[Any, ..., Any]
x1: C[int, Concatenate[int, str, P], str]
x = x1 # OK
[builtins fixtures/paramspec.pyi]

[case testParamSpecAliasNested]
from typing import ParamSpec, Callable, List, TypeVar, Generic
from typing_extensions import Concatenate

P = ParamSpec("P")
A = List[Callable[P, None]]
B = List[Callable[Concatenate[int, P], None]]

fs: A[int, str]
reveal_type(fs) # N: Revealed type is "builtins.list[def (builtins.int, builtins.str)]"
gs: B[int, str]
reveal_type(gs) # N: Revealed type is "builtins.list[def (builtins.int, builtins.int, builtins.str)]"

T = TypeVar("T")
class C(Generic[T]): ...
C[Callable[P, int]]() # E: The first argument to Callable must be a list of types, parameter specification, or "..." \
# N: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas
[builtins fixtures/paramspec.pyi]
You are viewing a condensed version of this merge commit. You can view the full changes here.