-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Ignore position if imprecise arguments are matched by name #16471
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
Changes from 2 commits
d9cc152
61c72a4
3b2e30b
4595330
77f371d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1687,9 +1687,18 @@ P = ParamSpec("P") | |
T = TypeVar("T") | ||
|
||
def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> None: ... | ||
|
||
def test(x: int) -> int: ... | ||
apply(apply, test, x=42) # OK | ||
apply(apply, test, 42) # Also OK (but requires some special casing) | ||
apply(apply, test, "bad") # E: Argument 1 to "apply" has incompatible type "Callable[[Callable[P, T], **P], None]"; expected "Callable[[Callable[[int], int], str], None]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Off-topic for this PR, and I'm guessing this might be tricky to implement, but it would be really nice if mypy could complain about argument 2 in this error message rather than argument 1. In real-world code, the "mistake" in calls like this is generally passing the wrong kinds of object as arguments after the function, rather than passing in the wrong function altogether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is really hard. |
||
|
||
def test2(x: int, y: str) -> None: ... | ||
apply(apply, test2, 42, "yes") | ||
apply(apply, test2, "no", 42) # E: Argument 1 to "apply" has incompatible type "Callable[[Callable[P, T], **P], None]"; expected "Callable[[Callable[[int, str], None], str, int], None]" | ||
apply(apply, test2, x=42, y="yes") | ||
apply(apply, test2, y="yes", x=42) | ||
apply(apply, test2, y=42, x="no") # E: Argument 1 to "apply" has incompatible type "Callable[[Callable[P, T], **P], None]"; expected "Callable[[Callable[[int, str], None], int, str], None]" | ||
[builtins fixtures/paramspec.pyi] | ||
|
||
[case testParamSpecApplyPosVsNamedOptional] | ||
|
@@ -2086,3 +2095,59 @@ reveal_type(d(b, f1)) # E: Cannot infer type argument 1 of "d" \ | |
# N: Revealed type is "def (*Any, **Any)" | ||
reveal_type(d(b, f2)) # N: Revealed type is "def (builtins.int)" | ||
[builtins fixtures/paramspec.pyi] | ||
|
||
[case testParamSpecGenericWithNamedArg1] | ||
from typing import Callable, TypeVar | ||
from typing_extensions import ParamSpec | ||
|
||
T_Retval = TypeVar("T_Retval") | ||
P = ParamSpec("P") | ||
|
||
def run(func: Callable[[], T_Retval], *args: object, backend: str = "asyncio") -> T_Retval: | ||
... | ||
|
||
class Result: ... | ||
def run_portal() -> Result: ... | ||
|
||
def submit( | ||
func: Callable[P, T_Retval], | ||
/, | ||
*args: P.args, | ||
**kwargs: P.kwargs, | ||
) -> T_Retval: ... | ||
ilevkivskyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
reveal_type(submit( # N: Revealed type is "__main__.Result" | ||
run, | ||
run_portal, | ||
backend="asyncio", | ||
)) | ||
submit( | ||
run, # E: Argument 1 to "submit" has incompatible type "Callable[[Callable[[], T_Retval], VarArg(object), DefaultNamedArg(str, 'backend')], T_Retval]"; expected "Callable[[Callable[[], Result], int], Result]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also off-topic for this PR, but I find it unfortunate that we use these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually was thinking about the same thing myself as I was looking at this test. This is not hard, but tedious. It would be great if someone will do this. |
||
run_portal, | ||
backend=int(), | ||
) | ||
[builtins fixtures/paramspec.pyi] | ||
|
||
[case testParamSpecGenericWithNamedArg2] | ||
from typing import Callable, TypeVar, Type | ||
from typing_extensions import ParamSpec | ||
|
||
ParamsT = ParamSpec("ParamsT") | ||
T = TypeVar("T") | ||
|
||
def smoke_testable( | ||
*args: ParamsT.args, **kwargs: ParamsT.kwargs | ||
) -> Callable[[Callable[ParamsT, T]], Type[T]]: | ||
... | ||
ilevkivskyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@smoke_testable(name="bob", size=512, flt=0.5) | ||
class SomeClass: | ||
def __init__(self, size: int, name: str, flt: float) -> None: | ||
pass | ||
|
||
# Error message is confusing, but this is a known issue, see #4530. | ||
@smoke_testable(name=42, size="bad", flt=0.5) # E: Argument 1 has incompatible type "Type[OtherClass]"; expected "Callable[[int, str, float], OtherClass]" | ||
class OtherClass: | ||
def __init__(self, size: int, name: str, flt: float) -> None: | ||
pass | ||
[builtins fixtures/paramspec.pyi] |
Uh oh!
There was an error while loading. Please reload this page.