From 9cf812abeb731954ecf610e0661f2486230ff5be Mon Sep 17 00:00:00 2001 From: root Date: Sun, 1 Jun 2025 11:26:29 -0300 Subject: [PATCH 1/3] refactor: combine_function_signatures by reducing line count while preserving original comments --- mypy/checkexpr.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index ace8f09bee48..23407ad63e91 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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[]] if the functions do not have # the exact same signature. The only exception is if one arg is optional and @@ -3160,18 +3160,14 @@ 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: - continue - elif new_kind.is_positional() and target_kind.is_positional(): + for i, (k1, k2) in enumerate(zip(new_kinds, target.arg_kinds)): + if k1 == k2: continue + if k1.is_positional() and k2.is_positional(): new_kinds[i] = ARG_POS else: too_complex = True break - - if too_complex: - break # outer loop - + if too_complex: break for i, arg in enumerate(target.arg_types): new_args[i].append(arg) new_returns.append(target.ret_type) @@ -3188,13 +3184,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], arg_kinds=new_kinds, ret_type=union_return, variables=variables, From 13267051451b3c2403b91af9fa0a0c8e34ebda2b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Jun 2025 14:33:32 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/checkexpr.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 23407ad63e91..a0ffb1fa2aad 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -3161,13 +3161,15 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call too_complex = True break for i, (k1, k2) in enumerate(zip(new_kinds, target.arg_kinds)): - if k1 == k2: continue + if k1 == k2: + continue if k1.is_positional() and k2.is_positional(): new_kinds[i] = ARG_POS else: too_complex = True break - if too_complex: break + if too_complex: + break for i, arg in enumerate(target.arg_types): new_args[i].append(arg) new_returns.append(target.ret_type) From 9c84f8b4d4d645d9858c3c3ddc7603c133b16a8d Mon Sep 17 00:00:00 2001 From: root Date: Sun, 1 Jun 2025 18:49:11 -0300 Subject: [PATCH 3/3] refactor: combine_function_signatures improve clarity per review feedback --- mypy/checkexpr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index a0ffb1fa2aad..98cb7bd2b8dc 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -3148,7 +3148,8 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call callables, variables = merge_typevars_in_callables_by_name(callables) new_args: list[list[Type]] = [[] for _ in callables[0].arg_types] - new_kinds, new_returns = list(callables[0].arg_kinds), [] + new_kinds = list(callables[0].arg_kinds) + new_returns: list[Type] = [] too_complex = False for target in callables: @@ -3160,10 +3161,10 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call if len(new_kinds) != len(target.arg_kinds): too_complex = True break - for i, (k1, k2) in enumerate(zip(new_kinds, target.arg_kinds)): - if k1 == k2: + for i, (new_kind, target_kind) in enumerate(zip(new_kinds, target.arg_kinds)): + if new_kind == target_kind: continue - if k1.is_positional() and k2.is_positional(): + if new_kind.is_positional() and target_kind.is_positional(): new_kinds[i] = ARG_POS else: too_complex = True