-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Reject callable assignment when dest keyword form is incompatible #11654
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # This sample tests that a callable with *args/**kwargs is not assignable | ||
| # to a callable whose positional-or-keyword parameter cannot be passed | ||
| # by keyword to the source (typing spec callable assignability). | ||
|
|
||
| from typing import Protocol | ||
|
|
||
|
|
||
| class AcceptsArgsKwargs(Protocol): | ||
| def __call__(self, *args: int, **kwargs: bool) -> None: ... | ||
|
|
||
|
|
||
| class AcceptsKeywordOrPositional(Protocol): | ||
| def __call__(self, a: int) -> None: ... | ||
|
|
||
|
|
||
| def func1(cb: AcceptsArgsKwargs): | ||
| # This should generate an error because AcceptsKeywordOrPositional | ||
| # can be called as cb(a=10), which is not valid for **kwargs: bool. | ||
| x: AcceptsKeywordOrPositional = cb | ||
|
|
||
|
|
||
| class AcceptsKeywordOnly(Protocol): | ||
| def __call__(self, *args: int, a: bool = False) -> None: ... | ||
|
|
||
|
|
||
| def func2(cb: AcceptsKeywordOnly): | ||
| # This should generate an error because the keyword form of | ||
| # parameter "a" has type int on the dest and bool on the source. | ||
| y: AcceptsKeywordOrPositional = cb | ||
|
|
||
|
|
||
| def ok_cb(a: int) -> None: | ||
| pass | ||
|
|
||
|
|
||
| def func3(cb: AcceptsKeywordOrPositional): | ||
| z: AcceptsKeywordOrPositional = ok_cb | ||
|
Collaborator
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. Could we add positive cases that exercise each newly accepted path? Assigning |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check needs to account for overload-overlap mode. For example,
(a: int) -> intand(*args: int, **kwargs: bool) -> strstill overlap for the positional callf(1), even though their keyword forms do not. DuringPartialOverloadOverlap, this unconditional failure makesassignFunctionreturn false and suppresses the incompatible-return diagnostic. Could we model the positional and keyword paths disjunctively (or otherwise preserve an overlap once a shared positional call exists) and add a regression case tooverloadOverlap1.py?