Skip to content

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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
25 changes: 9 additions & 16 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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), []
Copy link
Collaborator

@sterliakov sterliakov Jun 1, 2025

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?

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
Expand All @@ -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)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think new_ and target_ prefixes are much clearer than k1 and k2...

if k1 == k2:
continue
elif new_kind.is_positional() and target_kind.is_positional():
if k1.is_positional() and k2.is_positional():
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)
Expand All @@ -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],
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
Expand Down