mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Refactor: compact and optimize infer_overload_return_type while preserving behavior and comments

Open victorletichevsky opened this issue 7 months ago • 1 comments

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

  • This is a pure refactor in line with issue https://github.com/python/mypy/issues/5917.
  • The logic has not been modified, only cleaned up for readability and maintainability.

victorletichevsky avatar Jun 01 '25 16:06 victorletichevsky

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)

github-actions[bot] avatar Jun 01 '25 16:06 github-actions[bot]