Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ParamSpec constraint for types as callable #14153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
arg_types=cactual.arg_types[prefix_len:],
arg_kinds=cactual.arg_kinds[prefix_len:],
arg_names=cactual.arg_names[prefix_len:],
ret_type=NoneType(),
ret_type=UninhabitedType(),
),
)
)
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,30 @@ class C(Generic[P]):
reveal_type(bar(C(fn=foo, x=1))) # N: Revealed type is "__main__.C[[x: builtins.int]]"
[builtins fixtures/paramspec.pyi]

[case testParamSpecClassConstructor]
# flags: --strict-optional
from typing import ParamSpec, Callable

P = ParamSpec("P")

class SomeClass:
def __init__(self, a: str) -> None:
pass

def func(t: Callable[P, SomeClass], val: Callable[P, SomeClass]) -> None:
pass

def constructor(a: str) -> SomeClass:
return SomeClass(a)

def wrong_constructor(a: bool) -> SomeClass:
return SomeClass("a")

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

Expand Down