Skip to content

Refuse inlining methods decorated with abc.abstractmethod - #968

Open
Aschenbath wants to merge 1 commit into
python-rope:masterfrom
Aschenbath:fix/rope-744-20261122-7c2d-30
Open

Aschenbath wants to merge 1 commit into
python-rope:masterfrom
Aschenbath:fix/rope-744-20261122-7c2d-30

Conversation

@Aschenbath

Copy link
Copy Markdown

Inlining an abstract method can remove the abstract contract and substitute its placeholder body for a concrete implementation. Reject this refactoring before collecting changes, resolving the decorator so aliases and qualified imports are recognized while unrelated decorators with the same name still work.

Closes #744.

All 105 inline tests and 3 decorator subtests pass. The abstract-method regression fails on unchanged source. Black and git diff --check pass.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.45%. Comparing base (ff8a306) to head (faec8e6).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #968   +/-   ##
=======================================
  Coverage   95.44%   95.45%           
=======================================
  Files         134      134           
  Lines       26910    26929   +19     
=======================================
+ Hits        25685    25704   +19     
  Misses       1225     1225           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mcepl

mcepl commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Nice clean fix for #744. I particularly like that the decorator is resolved properly via evaluate.eval_node rather than matching names textually — handling @abstractmethod, @abc.abstractmethod and @abstractmethod as X aliases, while still allowing a user-defined same-name decorator to be inlined, is exactly the right approach. The tests are solid (several import forms × different offsets, plus the negative case) and raising before anything is collected means the source is left untouched on failure.

I have a few objections; all of them are stylistic issues (nits), none should block this:

  1. Only abstractmethod is matched. The other abc abstract decorators — abstractproperty, abstractclassmethod, abstractstaticmethod (deprecated since 3.3, but still found in the wild) — slip through the isinstance(..., pyobjects.PyFunction) + name check and remain inlineable. A small set-membership check alongside "abstractmethod" would cover them.

  2. The guard also fires for module-level functions, not just methods (e.g. a bare @abstractmethod on a free function, where the decorator is meaningless anyway), because the check lives in InlineMethod.__init__ rather than being scoped to methods. Harmless overreach, just worth noting.

  3. Micro style: scope = self.pyfunction.parent.get_scope() is computed unconditionally even when self.pyfunction.decorators is empty; it could sit inside the loop, or the whole check be guarded with if self.pyfunction.decorators:. Also the error wording could hint at why: something like "inlining an abstract method would remove the abstract contract" — that helps users figure out what to do next.

None of the above is a dealbreaker — feel free to address or ignore them. Happy to see this merged as-is.

@mcepl mcepl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

approve with small objections in a separate comment

@mcepl
mcepl requested a review from lieryan November 23, 2026 22:46

This branch has not been deployed

No deployments
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.

Inline Method refactoring is allowed in abstract methods.

2 participants