Skip to content

Refactor: compact and optimize infer_overload_return_type while preserving behavior and comments #19198

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 2 commits into
base: master
Choose a base branch
from

Conversation

victorletichevsky
Copy link

This pull request contributes to issue #5917, which encourages the decomposition and simplification of overly long methods to improve code clarity and maintainability.

Summary of changes

Refactored the infer_overload_return_type method to reduce line count and eliminate structural redundancy, while preserving all original logic and explanatory comments.

Key improvements:

  • Reduces the overall number of lines without removing any original inline documentation
  • Preserves all key comments explaining overload matching logic, especially around Any-based ambiguity
  • Replaces nested ifs with continue for early exits, minimizing indentation
  • Groups related variable declarations to improve readability
  • Avoids unnecessary allocations when possible (e.g. skipping data collection if early return applies)

Notes

Copy link
Contributor

github-actions bot commented Jun 1, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

typeshed-stats (https://github.com/AlexWaygood/typeshed-stats): 1.41x slower (65.1s -> 91.6s in single noisy sample)

nox (https://github.com/wntrblm/nox): 1.22x slower (23.7s -> 28.9s in single noisy sample)

materialize (https://github.com/MaterializeInc/materialize): 1.08x slower (129.8s -> 140.7s in single noisy sample)

discord.py (https://github.com/Rapptz/discord.py): 1.07x slower (275.7s -> 296.1s in single noisy sample)

if all_same_types(return_types):
self.chk.store_types(type_maps[0])
return return_types[0], inferred_types[0]
elif all_same_types([erase_type(typ) for typ in return_types]):
erased = [erase_type(t) for t in return_types]
if all_same_types(cast(list[Type], erased)):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why new cast?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants