-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Refactor: simplify combine_function_signatures by reducing line count and preserving comments #19196
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
Open
victorletichevsky
wants to merge
3
commits into
python:master
Choose a base branch
from
victorletichevsky:refactor-checkexpr-combine_function_signatures
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor: simplify combine_function_signatures by reducing line count and preserving comments #19196
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9cf812a
refactor: combine_function_signatures by reducing line count while pr…
victorletichevsky 1326705
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9c84f8b
refactor: combine_function_signatures improve clarity per review feed…
victorletichevsky 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 |
---|---|---|
|
@@ -3129,6 +3129,7 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call | |
assert types, "Trying to merge no callables" | ||
if not all(isinstance(c, CallableType) for c in types): | ||
return AnyType(TypeOfAny.special_form) | ||
|
||
callables = cast("list[CallableType]", types) | ||
if len(callables) == 1: | ||
return callables[0] | ||
|
@@ -3146,11 +3147,10 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call | |
# confusing and ought to be re-written anyways.) | ||
callables, variables = merge_typevars_in_callables_by_name(callables) | ||
|
||
new_args: list[list[Type]] = [[] for _ in range(len(callables[0].arg_types))] | ||
new_kinds = list(callables[0].arg_kinds) | ||
new_returns: list[Type] = [] | ||
|
||
new_args: list[list[Type]] = [[] for _ in callables[0].arg_types] | ||
new_kinds, new_returns = list(callables[0].arg_kinds), [] | ||
too_complex = False | ||
|
||
for target in callables: | ||
# We fall back to Callable[..., Union[<returns>]] if the functions do not have | ||
# the exact same signature. The only exception is if one arg is optional and | ||
|
@@ -3160,18 +3160,16 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call | |
if len(new_kinds) != len(target.arg_kinds): | ||
too_complex = True | ||
break | ||
for i, (new_kind, target_kind) in enumerate(zip(new_kinds, target.arg_kinds)): | ||
if new_kind == target_kind: | ||
for i, (k1, k2) in enumerate(zip(new_kinds, target.arg_kinds)): | ||
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 think |
||
if k1 == k2: | ||
continue | ||
elif new_kind.is_positional() and target_kind.is_positional(): | ||
if k1.is_positional() and k2.is_positional(): | ||
victorletichevsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new_kinds[i] = ARG_POS | ||
else: | ||
too_complex = True | ||
break | ||
|
||
if too_complex: | ||
break # outer loop | ||
|
||
break | ||
for i, arg in enumerate(target.arg_types): | ||
new_args[i].append(arg) | ||
new_returns.append(target.ret_type) | ||
|
@@ -3188,13 +3186,8 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call | |
implicit=True, | ||
) | ||
|
||
final_args = [] | ||
for args_list in new_args: | ||
new_type = make_simplified_union(args_list) | ||
final_args.append(new_type) | ||
|
||
return callables[0].copy_modified( | ||
arg_types=final_args, | ||
arg_types=[make_simplified_union(args) for args in new_args], | ||
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. This is definitely an improvement! |
||
arg_kinds=new_kinds, | ||
ret_type=union_return, | ||
variables=variables, | ||
|
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.
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 looks like a penalty to readability rather than refactoring, why merge two unrelated assignments?