Skip to content

[ruff] Add RUF074 incorrect-decorator-order #23461

Open
anishgirianish wants to merge 10 commits intoastral-sh:mainfrom
anishgirianish:ruf071-incorrect-decorator-order
Open

[ruff] Add RUF074 incorrect-decorator-order #23461
anishgirianish wants to merge 10 commits intoastral-sh:mainfrom
anishgirianish:ruf071-incorrect-decorator-order

Conversation

@anishgirianish
Copy link
Copy Markdown
Contributor

@anishgirianish anishgirianish commented Feb 21, 2026

Summary

Implements detection of incorrect decorator ordering on functions and methods (RUF071).

Decorators are applied bottom-up, so certain stdlib decorator combinations cause runtime errors or silent wrong behavior when placed in the wrong order. For example, @abstractmethod above @property results in AttributeError: __isabstractmethod__.

Detects known-bad pairs involving:

  • abc.abstractmethod (+ deprecated abstractclassmethod, abstractstaticmethod, abstractproperty)
  • builtins.property, builtins.classmethod, builtins.staticmethod
  • contextlib.contextmanager, contextlib.asynccontextmanager
  • functools.cache, functools.cached_property, functools.lru_cache

Test Plan

cargo nextest run -p ruff_linter -E 'test(preview_rules::rule_incorrectdecoratororder)'

Fixture covers:

  • All known-bad pairs (core + functools)
  • Qualified imports (abc.abstractmethod, functools.lru_cache)
  • Deprecated abc forms (abstractproperty, abstractclassmethod, abstractstaticmethod)
  • Decorator call forms (@lru_cache(), @lru_cache(maxsize=128))
  • Interleaved unknown decorators
  • Multiple violations on the same function
  • Correct orderings, single decorators, unrelated decorators (no false positives)

Closes #6697

@anishgirianish anishgirianish changed the title [ruff] Implement incorrect-decorator-order (RUF071) [ruff] Add RUF071 incorrect-decorator-order Feb 21, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Feb 21, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Feb 23, 2026
@ntBre
Copy link
Copy Markdown
Contributor

ntBre commented Feb 23, 2026

Could you check the ecosystem results? cached_property and abstractmethod seem to be working okay in that order, so I don't think we should emit a diagnostic.

@anishgirianish
Copy link
Copy Markdown
Contributor Author

Could you check the ecosystem results? cached_property and abstractmethod seem to be working okay in that order, so I don't think we should emit a diagnostic.

sure checking now thanks

Copy link
Copy Markdown
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks, this looks reasonable to me overall (although I still need to go through all the test cases). I mainly had a question about the multi-decorator case and a nit about Display.

@anishgirianish
Copy link
Copy Markdown
Contributor Author

Hi @ntBre @amyreese, I've addressed the review comments. Would love another look whenever you have time. Thanks so much!

Copy link
Copy Markdown
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! Just a couple more questions about @property.setter and possible references for the chosen pairs.

@anishgirianish
Copy link
Copy Markdown
Contributor Author

@ntBre @amyreese Addressed both comments and pushed the changes. Ready for another look whenever you get a chance!

@anishgirianish anishgirianish requested a review from ntBre February 28, 2026 19:10
@anishgirianish anishgirianish force-pushed the ruf071-incorrect-decorator-order branch from 7b99afd to ced38ec Compare March 14, 2026 08:37
Bump IncorrectDecoratorOrder from RUF071 to RUF072 since RUF071
was taken by OsPathCommonprefix upstream.
@anishgirianish anishgirianish force-pushed the ruf071-incorrect-decorator-order branch from ced38ec to 41d5164 Compare March 14, 2026 08:45
@anishgirianish anishgirianish changed the title [ruff] Add RUF071 incorrect-decorator-order [ruff] Add RUF072 incorrect-decorator-order Mar 14, 2026
…corator-order

# Conflicts:
#	crates/ruff_linter/resources/test/fixtures/ruff/RUF072.py
#	crates/ruff_linter/src/codes.rs
#	crates/ruff_linter/src/rules/ruff/mod.rs
#	crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF072_RUF072.py.snap
#	ruff.schema.json
@anishgirianish anishgirianish changed the title [ruff] Add RUF072 incorrect-decorator-order [ruff] Add RUF074 incorrect-decorator-order Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new rule - detect when decorators are in the wrong order

3 participants