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 all 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
18 changes: 6 additions & 12 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,11 @@ 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_args: list[list[Type]] = [[] for _ in callables[0].arg_types]
new_kinds = list(callables[0].arg_kinds)
new_returns: list[Type] = []

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 @@ -3163,15 +3164,13 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call
for i, (new_kind, target_kind) in enumerate(zip(new_kinds, target.arg_kinds)):
if new_kind == target_kind:
continue
elif new_kind.is_positional() and target_kind.is_positional():
if new_kind.is_positional() and target_kind.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 +3187,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