[ruff] Add incorrect-decorator-order (RUF074)#23461
Conversation
incorrect-decorator-order (RUF071)RUF071 incorrect-decorator-order
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF074 | 2 | 2 | 0 | 0 | 0 |
|
Could you check the ecosystem results? |
sure checking now thanks |
ntBre
left a comment
There was a problem hiding this comment.
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.
ntBre
left a comment
There was a problem hiding this comment.
Thanks! Just a couple more questions about @property.setter and possible references for the chosen pairs.
7b99afd to
ced38ec
Compare
Bump IncorrectDecoratorOrder from RUF071 to RUF072 since RUF071 was taken by OsPathCommonprefix upstream.
ced38ec to
41d5164
Compare
RUF071 incorrect-decorator-order RUF072 incorrect-decorator-order
…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
RUF072 incorrect-decorator-order RUF074 incorrect-decorator-order
|
Sorry for the delay! The past couple of weeks have been quite busy, but this is back in my queue. I'll try to get to it this week :) |
ntBre
left a comment
There was a problem hiding this comment.
Thanks! And apologies again for the delay.
I took a closer look at some of the flagged pairs today, and I don't think we're quite aligned with CPython. Unfortunately, it also looks like some of the errors depend on the Python version being tested.
I think what would be really helpful is if you could prepare a series of code snippets that test every pair of decorators the rule lints against, like the examples I shared in my inline comments, that way we can easily compare against CPython itself. I think a REPL session would actually be a nice way to share this because we should be able to use doctest to run this against multiple Python versions.
The test fixture is close to this, it's just hard to use directly because pasting the whole thing into the REPL stops at the first error.
|
Thanks @ntBre! Put together a REPL recording (attached as python -m doctest -o ELLIPSIS ruf074_repl.txtI have addressed all the feedback and fixes that were spotted during REPL session. I have update the pr with the changes. Would like to request you for your re-review whenever you get a chance. Thank you so much. |
ntBre
left a comment
There was a problem hiding this comment.
Thank you, this is awesome! The doctests are really polished and very, very helpful.
I just had one last suggestion for handling the version-dependent error, which I think might be doable, but this is otherwise ready to go.
|
Hi @ntBre thank you so much for the re-review, I have addressed the recent feedback and updated the pr. Would like to request you for your another look whenever you get a chance thank you |
RUF074 incorrect-decorator-order ruff] Add incorrect-decorator-order (RUF074)
|
Nice, it looks like two ecosystem diagnostics are the |
Resolve conflicts caused by upstream landing RUF074 (`incorrect-decorator-order`, astral-sh#23461). Renumber the in-progress `FallibleContextManager` rule from RUF074 to RUF075: - codes.rs: keep `IncorrectDecoratorOrder` at 074 (landed), add `FallibleContextManager` at 075 - mod.rs: keep both `test_case` entries; FallibleContextManager now references `RUF075.py` - Rename fixture: HEAD's `RUF074.py` content moves to new `RUF075.py` with `# RUF074` annotations updated to `# RUF075` - Rename snapshot: HEAD's snapshot becomes `RUF075_RUF075.py.snap` with `RUF074` references rewritten to `RUF075` - fallible_context_manager.rs: rule-id comment updated to RUF075 - ruff.schema.json regenerated via `cargo dev generate-all`
## 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 astral-sh#6697
## 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 astral-sh#6697
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,
@abstractmethodabove@propertyresults inAttributeError: __isabstractmethod__.Detects known-bad pairs involving:
abc.abstractmethod(+ deprecatedabstractclassmethod,abstractstaticmethod,abstractproperty)builtins.property,builtins.classmethod,builtins.staticmethodcontextlib.contextmanager,contextlib.asynccontextmanagerfunctools.cache,functools.cached_property,functools.lru_cacheTest Plan
cargo nextest run -p ruff_linter -E 'test(preview_rules::rule_incorrectdecoratororder)'Fixture covers:
abc.abstractmethod,functools.lru_cache)abstractproperty,abstractclassmethod,abstractstaticmethod)@lru_cache(),@lru_cache(maxsize=128))Closes #6697