-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Further improvements to functools.partial handling #17425
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
Merged
hauntsaninja
merged 7 commits into
python:master
from
hauntsaninja:functool-partial-more
Jul 1, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ad24b3
Improvements to functools.partial
hauntsaninja 8550007
.
hauntsaninja e1ee5bb
Merge branch 'master' into functool-partial-more
hauntsaninja a249c5a
address code review
hauntsaninja 754621c
.
hauntsaninja 0706ef1
Merge remote-tracking branch 'upstream/master' into functool-partial-…
hauntsaninja 1f70ddd
.
hauntsaninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,11 +245,14 @@ def partial_new_callback(ctx: mypy.plugin.FunctionContext) -> Type: | |
partial_kinds.append(fn_type.arg_kinds[i]) | ||
partial_types.append(arg_type) | ||
partial_names.append(fn_type.arg_names[i]) | ||
elif actuals: | ||
if any(actual_arg_kinds[j] == ArgKind.ARG_POS for j in actuals): | ||
else: | ||
assert actuals | ||
if any(actual_arg_kinds[j] in (ArgKind.ARG_POS, ArgKind.ARG_STAR) for j in actuals): | ||
# Don't add params for arguments passed positionally | ||
continue | ||
# Add defaulted params for arguments passed via keyword | ||
kind = actual_arg_kinds[actuals[0]] | ||
if kind == ArgKind.ARG_NAMED: | ||
if kind == ArgKind.ARG_NAMED or kind == ArgKind.ARG_STAR2: | ||
kind = ArgKind.ARG_NAMED_OPT | ||
partial_kinds.append(kind) | ||
hauntsaninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
partial_types.append(arg_type) | ||
|
@@ -286,15 +289,25 @@ def partial_call_callback(ctx: mypy.plugin.MethodContext) -> Type: | |
if len(ctx.arg_types) != 2: # *args, **kwargs | ||
return ctx.default_return_type | ||
|
||
args = [a for param in ctx.args for a in param] | ||
arg_kinds = [a for param in ctx.arg_kinds for a in param] | ||
arg_names = [a for param in ctx.arg_names for a in param] | ||
# See comments for similar actual to formal code above | ||
actual_args = [] | ||
actual_arg_kinds = [] | ||
actual_arg_names = [] | ||
seen_args = set() | ||
for i, param in enumerate(ctx.args): | ||
for j, a in enumerate(param): | ||
if a in seen_args: | ||
continue | ||
seen_args.add(a) | ||
actual_args.append(a) | ||
actual_arg_kinds.append(ctx.arg_kinds[i][j]) | ||
actual_arg_names.append(ctx.arg_names[i][j]) | ||
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. Looking at this it seems to me a better strategy for the call site may be using def foo(fn: Callable[[int, str], int]) -> None: ...
fn = partial(some_other_fn, 1, 2)
foo(fn) |
||
|
||
result = ctx.api.expr_checker.check_call( | ||
callee=partial_type, | ||
args=args, | ||
arg_kinds=arg_kinds, | ||
arg_names=arg_names, | ||
args=actual_args, | ||
arg_kinds=actual_arg_kinds, | ||
arg_names=actual_arg_names, | ||
context=ctx.context, | ||
) | ||
return result[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.