Skip to content

Refactor: modularize long methods in Options and checkexpr #19010

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

Conversation

victorletichevsky
Copy link

This pull request addresses part of the long-term refactoring goal described in issue #5917, which proposes breaking up very large methods into smaller, more readable components.

Summary of changes

This PR includes two independent refactorings:

  • Options.__init__: extracted internal logic into _initialize_* methods, grouped by purpose (e.g., caching, build options, warnings).
    All original comments and default values were preserved for clarity.

  • check_op_reversible: decomposed into helper methods:

    • _lookup_operator
    • _lookup_definer
    • _determine_operator_order
    • _attempt_operator_applications
      This makes the logic easier to follow, particularly around Python's operator resolution rules.

Motivation

  • Improves readability and maintainability of complex methods.
  • Makes future changes easier and safer to test.
  • Helps contributors navigate and understand key parts of the codebase.

Notes

  • This is a non-functional refactor: no logic was changed.
  • All original comments and error messages are preserved.

Further refactorings aligned with issue #5917 may be submitted in future PRs.

@sterliakov
Copy link
Collaborator

Given that this refactoring touches completely unrelated files, could you please submit these changes separately?

@@ -3968,54 +3968,14 @@ def check_op_reversible(
right_expr: Expression,
context: Context,
) -> tuple[Type, Type]:
def lookup_operator(op_name: str, base_type: Type) -> Type | None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Extracting this helper to method is good and may improve performance of compiled mypy - mypyc isn't doing its best when there are nested functions.

for name, method, obj, arg in variants:
with self.msg.filter_errors(save_filtered_errors=True) as local_errors:
result = self.check_method_call(name, obj, method, [arg], [ARG_POS], context)
result = self.check_method_call(op_name, obj, method, [arg], [ARG_POS], context)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please wait until #18995 is merged - this will be a conflict (that PR fixes a bug, op_name shouldn't be used here)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ough, it is already merged, sorry. Then please don't introduce that bug back!

Copy link
Contributor

github-actions bot commented May 1, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@victorletichevsky
Copy link
Author

Given that this refactoring touches completely unrelated files, could you please submit these changes separately?

@sterliakov Thanks for the suggestion. I've split the changes as requested.

The refactoring of Options.__init__ has been moved to a separate pull request: #19011.

I'll also submit the check_op_reversible refactoring in its own PR shortly.

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