Commit Graph

2288 Commits

Author SHA1 Message Date
Charlie Marsh 9570d39f9b
Remove extra dot in rule documentation (#18871) 2025-06-23 00:33:21 +00:00
Victor Hugo Gomes 06a78d0bd0
[`pylint`] Fix `PLC1802` autofix creating a syntax error and mark autofix as unsafe if there's comments in the `len` call (#18836)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary
I've also found another bug while fixing this, where the diagnostic
would not trigger if the `len` call argument variable was shadowed. This
fixed a few false negatives in the test cases.
Example:
```python
fruits = []
fruits = []
if len(fruits):  # comment
    ...
```

Fixes #18811
Fixes #18812
<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan
Add regression test
<!-- How was it tested? -->

---------

Co-authored-by: Charlie Marsh <crmarsh416@gmail.com>
2025-06-23 00:32:57 +00:00
Charlie Marsh cfec89e8c3
Clarify PEP 8 relationship to whitespace-around-operator rules (#18870)
## Summary

See: https://github.com/astral-sh/ruff/issues/18868.
2025-06-22 20:30:34 -04:00
Victor Hugo Gomes 9089493263
[`refurb`] Mark `FURB129` autofix as unsafe if there's comments in the `readlines` call (#18858) 2025-06-22 08:51:37 +01:00
Victor Hugo Gomes f32ae94bc3
[`pylint`] Mark `PLE0241` autofix as unsafe if there's comments in the base classes (#18832)
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-06-21 17:20:39 +00:00
chiri da6cbeee60
[`flake8-raise`] Make fix unsafe if it deletes comments (`RSE102`) (#18788) 2025-06-21 19:09:40 +02:00
Victor Hugo Gomes d9266284df
Handle parenthesized arguments in `remove_argument` (#18805)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2025-06-20 21:24:41 +00:00
Dylan 2d224e6096
Unify helpers modules (#18835)
A little bit of cleanup for consistency's sake: we move all the helpers
modules to a consistent location, and update the import paths when
needed. In the case of `refurb` there were two helpers modules, so we
just merged them.

Happy to revert the last commit if people are okay with `super::super` I
just thought it looked a little silly.
2025-06-20 16:03:01 -05:00
GiGaGon ef785d2e74
Normalize some docs sections (#18831) 2025-06-20 21:56:11 +01:00
Robsdedude e36611c4d8
[`flake8_pyi`] Fix `PYI041`'s fix causing TypeError with `None | None | ...` (#18637)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary
Fix `PYI041`'s fix turning `None | int | None | float` into `None | None
| float`, which raises a `TypeError` when executed.

The fix consists of making sure that the merged super-type is inserted
where the first type that is merged was before.

## Test Plan
Tests have been expanded with examples from the issue.

## Related Issue
Fixes https://github.com/astral-sh/ruff/issues/18298
2025-06-20 15:04:51 -04:00
Hmvp 49763a7f7c
[`flake8-logging`] Avoid false positive for `exc_info=True` outside `logger.exception` (`LOG014`) (#18737)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

Fixes https://github.com/astral-sh/ruff/issues/18726 by also checking if
its a literal and not only that it is truthy. See also the first comment
in the issue.

It would have been nice to check for inheritance of BaseException but I
figured that is not possible yet...

## Test Plan

I added a few tests for valid input to exc_info
2025-06-20 14:43:08 -04:00
GiGaGon 2d25aaeaa2
[`flake8-pie`] Small docs fix to `PIE794` (#18829)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

I noticed this since my code for finding missing safety fix sections
flagged it, there is a missing `/` causing part of the new changes to be
a normal comment instead of a doc comment

## Test Plan

<!-- How was it tested? -->

N/A, no functionality/tests affected
2025-06-20 14:38:13 -04:00
Gideon 2910988b06
[`pylint`] Ignore __init__.py files in (PLC0414) (#18400)
## Summary

Ignore `__init__.py` files in `useless-import-alias` (PLC0414).
See discussion in #18365 and #6294: we want to allow redundant aliases
in `__init__.py` files, as they're almost always intentional explicit
re-exports.
Closes #18365
 Closes #6294

---------

Co-authored-by: Dylan <dylwil3@gmail.com>
2025-06-20 18:20:27 +00:00
Brent Westbrook 8cff77c82e
Avoid generating diagnostics with per-file ignores (#18801)
## Summary

This PR avoids one of the three calls to `NoqaCode::rule` from
https://github.com/astral-sh/ruff/pull/18391 by applying per-file
ignores in the `LintContext`. To help with this, it also replaces all
direct uses of `LinterSettings.rules.enabled` with a
`LintContext::enabled` (or `Checker::enabled`, which defers to its
context) method. There are still some direct accesses to
`settings.rules`, but as far as I can tell these are not in a part of
the code where we can really access a `LintContext`. I believe all of
the code reachable from `check_path`, where the replaced per-file ignore
code was, should be converted to the new methods.

## Test Plan

Existing tests, with a single snapshot updated for RUF100, which I think
actually shows a more accurate diagnostic message now.
2025-06-20 13:33:09 -04:00
Victor Hugo Gomes ffb09c84f2
[`flake8-simplify`] Fix false negatives for shadowed bindings (`SIM910`, `SIM911`) (#18794)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary
I also noticed that the tests for SIM911 were note being run, so I fixed
that.

Fixes #18777
<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan
Add regression test
<!-- How was it tested? -->
2025-06-20 13:25:36 -04:00
Yunchi Pang 073a71ca9a
[`pylint`] add fix safety section (`PLR1714`) (#18415)
parent #15584
fix was introduced in #7910

---------

Co-authored-by: dylwil3 <dylwil3@gmail.com>
2025-06-20 15:40:44 +00:00
GiGaGon 23a3b6ef23
[Perflint] Small docs improvement to `PERF401` (#18786)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

While reading the docs I noticed this paragraph on `PERF401`. It was
added in the same PR that the bug with `:=` was fixed, #15050, but don't
know why it was added. The fix should already take care of adding the
parenthesis, so having this paragraph in the docs is just confusing
since it sounds like the user has to do something.

## Test Plan

<!-- How was it tested? -->

N/A, no tests/functionality affected
2025-06-20 09:55:49 -05:00
Gene Parmesan Thomas 5b3a501fae
[`pylint`] Avoid flattening nested `min`/`max` when outer call has single argument (`PLW3301`) (#16885)
## Summary

Fixes false positives (and incorrect autofixes) in `nested-min-max`
(`PLW3301`) when the outer `min`/`max` call only has a single argument.
Previously the rule would flatten:

```python
min(min([2, 3], [4, 1]))
```

into `min([2, 3], [4, 1])`, changing the semantics. The rule now skips
any nested call when the outer call has only one positional argument.
The pylint fixture and snapshot were updated accordingly.

## Test Plan

Ran Ruff against the updated `nested_min_max.py` fixture:

```shell
cargo run -p ruff -- check crates/ruff_linter/resources/test/fixtures/pylint/nested_min_max.py --no-cache --select=PLW3301 --preview
```

to verify that `min(min([2, 3], [4, 1]))` and `max(max([2, 4], [3, 1]))`
are no longer flagged. Updated the fixture and snapshot; all other
existing warnings remain unchanged. The code compiles and the unit tests
pass.

---

This PR was generated by an AI system in collaboration with maintainers:
@carljm, @ntBre

Fixes #16163

---------

Signed-off-by: Gene Parmesan Thomas <201852096+gopoto@users.noreply.github.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2025-06-20 09:35:09 -04:00
Deric Crago e66f182045
[`ruff`] Added `cls.__dict__.get('__annotations__')` check (`RUF063`) (#18233)
Added `cls.__dict__.get('__annotations__')` check for Python 3.10+ and
Python < 3.10 with `typing-extensions` enabled.

Closes #17853 

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

Added `cls.__dict__.get('__annotations__')` check for Python 3.10+ and
Python < 3.10 with `typing-extensions` enabled.

## Test Plan

`cargo test`

---------

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-06-20 09:32:40 -04:00
Gene Parmesan Thomas 234d248730
docs: Correct collections-named-tuple example to use PascalCase assignment (#16884) 2025-06-20 13:30:46 +00:00
Andrej e180975226
unnecessary_dict_kwargs doc - a note on type checking benefits (#18666) 2025-06-20 08:27:51 +02:00
Victor Hugo Gomes 97819f8a37
[`flake8-pytest-style`] Mark autofix for `PT001` and `PT023` as unsafe if there's comments in the decorator (#18792) 2025-06-20 08:23:59 +02:00
GiGaGon 50bf3fa45a
[`flake8-pie`] Add fix safety section to `PIE794` (#18802)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

Part of #15584

This PR adds a fix safety section to `PIE794`

I could not track down when this rule was initially implemented/made
unsafe due how old it could be + multiple large refactors to `ruff`.

There is no comment/reasoning in the code given for the unsafety.

Here is a code example demonstrating why it should be unsafe, since
removing any of the assignments would change program behavior
[playground](https://play.ruff.rs/01004644-4259-4449-a581-5007cd59846a)
```py
class A:
    x = 1
    x = 2
    print(x)

class B:
    x = print(3)
    x = print(4)

class C:
    x = [1,2,3]
    y = x
    x = y[1]
```

## Test Plan

<!-- How was it tested? -->

N/A, no tests affected.

---------

Co-authored-by: Dylan <dylwil3@gmail.com>
2025-06-19 21:51:23 +00:00
GiGaGon 440635cbe6
[`pycodestyle`] Add fix safety section to `W291` and `W293` (#18800)
Part of #15584

This PR adds fix safety sections to `W291` and `W293`

The unsafe caveat was added in #10049


10a1d9f01e/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs (L92)

Code example demonstrating unsafety:
```
PS ~\Desktop\New_folder\ruff>Get-Content issue.py
```
```py
# W291
"""
1
"""

# W293
"""

"""
```
```
PS ~\Desktop\New_folder\ruff>Get-Escaped-Content issue.py
```
```
# W291\n"""\n1 \n"""\n\n# W293\n"""\n \n"""\r\n
```
```
PS ~\Desktop\New_folder\ruff>uvx ruff check issue.py --isolated --select W
```
```snap
issue.py:3:2: W291 Trailing whitespace
  |
1 | # W291
2 | """
3 | 1
  |  ^ W291
4 | """
  |
  = help: Remove trailing whitespace

issue.py:8:1: W293 Blank line contains whitespace
  |
6 | # W293
7 | """
8 |
  | ^ W293
9 | """
  |
  = help: Remove whitespace from blank line

Found 2 errors.
No fixes available (2 hidden fixes can be enabled with the `--unsafe-fixes` option).
```

## Test Plan

<!-- How was it tested? -->

N/A, no tests affected.
2025-06-19 16:48:02 -05:00
Dylan ce0a32aadb
[`flake8-comprehensions`] Handle template strings for comprehension fixes (#18710)
Essentially this PR ensures that when we do fixes like this:

```diff
- t"{set(f(x) for x in foo)}"
+ t"{ {f(x) for x in foo} }"
```
we are correctly adding whitespace around the braces. 

This logic is already in place for f-strings and just needed to be
generalized to interpolated strings.
2025-06-19 16:23:46 -05:00
Brent Westbrook 10a1d9f01e
Unify `OldDiagnostic` and `Message` (#18391)
Summary
--

This PR unifies the remaining differences between `OldDiagnostic` and
`Message` (`OldDiagnostic` was only missing an optional `noqa_offset`
field) and
replaces `Message` with `OldDiagnostic`.

The biggest functional difference is that the combined `OldDiagnostic`
kind no
longer implements `AsRule` for an infallible conversion to `Rule`. This
was
pretty easy to work around with `is_some_and` and `is_none_or` in the
few places
it was needed. In `LintContext::report_diagnostic_if_enabled` we can
just use
the new `Violation::rule` method, which takes care of most cases.

Most of the interesting changes are in [this
range](8156992540)
before I started renaming.

Test Plan
--

Existing tests

Future Work
--

I think it's time to start shifting some of these fields to the new
`Diagnostic`
kind. I believe we want `Fix` for sure, but I'm less sure about the
others. We
may want to keep a thin wrapper type here anyway to implement a `rule`
method,
so we could leave some of these fields on that too.
2025-06-19 09:37:58 -04:00
Robsdedude 4e83db4d40
[`pylint`] Detect more exotic NaN literals in `PLW0177` (#18630)
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-06-19 11:05:06 +00:00
Victor Hugo Gomes 136443b71b
[`flake8-async`] Mark autofix for `ASYNC115` as unsafe if the call expression contains comments (#18753)
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-06-19 13:01:33 +02:00
Victor Hugo Gomes f7a741a99e
[`flake8-bugbear`] Mark autofix for `B004` as unsafe if the `hasattr` call expr contains comments (#18755) 2025-06-19 10:46:53 +00:00
Charlie Marsh 4c8d612120
Enforce `pytest` import for decorators (#18779) 2025-06-19 09:49:34 +00:00
Victor Hugo Gomes 65b288b45b
[`flake8-comprehension`] Mark autofix for `C420` as unsafe if there's comments inside the dict comprehension (#18768) 2025-06-19 09:43:05 +00:00
chiri 06da2c808f
[flake8-async] fix detection for large integer sleep durations in `ASYNC116` rule (#18767)
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-06-19 09:37:20 +00:00
Frazer McLean f67ff33177
Add lint rule for calling chmod with non-octal integers (#18541)
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-06-19 11:30:29 +02:00
Nikolas Hearp dcf0a8d4d7
Mark `RET501` fix unsafe if comments are inside (#18780)
Co-authored-by: Charlie Marsh <crmarsh416@gmail.com>
2025-06-19 11:12:12 +02:00
Brent Westbrook e352a50b74
Use `LintContext::report_diagnostic_if_enabled` in `check_tokens` (#18769)
## Summary

This PR avoids the `Vec::retain` call in `check_tokens` by checking if
rules are enabled as their diagnostics are constructed.


2a425e43fd/crates/ruff_linter/src/checkers/tokens.rs (L174-L176)

Since `LintContext::report_diagnostic_if_enabled` required a
`LinterSettings`, I added a `settings` field to the context itself
instead of trying to pass it everywhere. This also turned
`LogicalLinesContext` into a trivial wrapper around `LintContext`, so I
just removed it in favor of using `LintContext` directly too.

The diff is a bit smaller with whitespace hidden since many blocks got
moved into something like this:

```rust
if let Some(mut diagnostic) = context.report_diagnostic.enabled(...) {
    // old code
}
```

## Test Plan

Existing tests
2025-06-18 17:05:36 -04:00
sobolevn 2a425e43fd
[UP008]: use `super()`, not `__super__` in error messages (#18743)
When I try to grep CPython with `__super__` I get 0 results:

```
(.venv) ~/Desktop/cpython  main ✔                                                    
» ag __super__ . 
                
```

That's how we can understand that the naming is not the best.
2025-06-18 13:57:57 -04:00
Brent Westbrook 87f0feb21a
Bump 0.12.0 (#18724)
- [x] Updated changelog
- [x] Updated breaking changes
2025-06-17 11:05:59 -04:00
Dylan a93992fa30 [`flake8-return`] Stabilize only add `return None` at the end when fixing `implicit-return` (`RET503`) (#18516)
This involved slightly more code changes than usual for a stabilization
- so maybe worth double-checking the logic!

I did verify by hand that the new stable behavior on the test fixture
matches the old preview behavior, even after the internal refactor.
2025-06-17 09:58:12 -04:00
Brent Westbrook 50f84808bc [`pyupgrade`] Stabilize `non-pep695-generic-function` (`UP047`) (#18524)
## Summary
- Stabilizes UP047 (non-pep695-generic-function) rule by changing it
from Preview to Stable

## Test plan
-  Rule is already tested in main test function, no migration needed
-  `make check` passes
-  `make test` passes

## Rule Documentation
- [Test
file](https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/rules/pyupgrade/mod.rs#L111)
- [Rule
documentation](https://docs.astral.sh/ruff/rules/non-pep695-generic-function/)
2025-06-17 09:58:12 -04:00
Brent Westbrook 6754e94abc [`pyupgrade`] Stabilize `non-pep695-generic-class` (`UP046`) (#18519)
## Summary
- Stabilizes UP046 (non-pep695-generic-class) rule by changing it from
Preview to Stable

## Test plan
-  Rule is already tested in main test function, no migration needed
-  `make check` passes
-  `make test` passes

## Rule Documentation
- [Test
file](https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/rules/pyupgrade/mod.rs#L109-L110)
- [Rule
documentation](https://docs.astral.sh/ruff/rules/non-pep695-generic-class/)
2025-06-17 09:58:12 -04:00
Brent Westbrook 33c8c7569d [`pandas-vet`] Deprecate `pandas-df-variable-name` (`PD901`) (#18618)
Summary
--

Deprecates PD901 as part of #7710. I don't feel particularly strongly
about this one, though I have certainly used `df` as a dataframe name in
the past, just going through the open issues in the 0.12 milestone.

Test Plan
--

N/a
2025-06-17 09:58:12 -04:00
Brent Westbrook 34dc8e0531 [`flake8-bandit`] Remove `suspicious-xmle-tree-usage` (`S320`) (#18617)
Summary
--

Closes #13707. The rule was deprecated in 0.10 (#16680) and slated for
removal in either this or the next release.

Test Plan
--

N/a
2025-06-17 09:58:12 -04:00
Dylan b01195b166 Stabilize `dataclass-enum` (`RUF049`) (#18570)
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2025-06-17 09:58:12 -04:00
Dylan ce176b1acf Stabilize `unnecessary-dict-index-lookup` (`PLR1733`) (#18571)
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2025-06-17 09:58:12 -04:00
Brent Westbrook 72c8dc006f Stabilize `starmap-zip` (`RUF058`) (#18525)
## Summary
- Stabilizes RUF058 (starmap-zip) rule by changing it from Preview to
Stable
- Migrates test cases from preview_rules to main rules function 
- Updates snapshots accordingly and removes old preview snapshots

## Test plan
-  Migrated tests from preview to main test function
-  `make check` passes
-  `make test` passes  
-  `make citest` passes (no leftover snapshots)

## Rule Documentation
- [Test
file](https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/rules/ruff/mod.rs#L103-L104)
- [Rule documentation](https://docs.astral.sh/ruff/rules/starmap-zip/)
2025-06-17 09:58:12 -04:00
Brent Westbrook ad9ae4e2b6 [`flake8-logging`] Stabilize `exc-info-outside-except-handler` (`LOG014`) (#18517)
## Summary
- Stabilizes LOG014 (exc-info-outside-except-handler) rule by changing
it from Preview to Stable

## Test plan
-  Rule is already tested in main test function, no migration needed
-  `make check` passes
-  `make test` passes

## Rule Documentation
- [Test
file](https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/rules/flake8_logging/mod.rs#L22-L23)
- [Rule
documentation](https://docs.astral.sh/ruff/rules/exc-info-outside-except-handler/)
2025-06-17 09:58:12 -04:00
Dylan de4fc5b171 [`pyupgrade`] Stabilize `non-pep604-annotation-optional` (`UP045`) and preview behavior for `non-pep604-annotation-union` (`UP007`) (#18505) 2025-06-17 09:58:12 -04:00
Dylan 56f2aaaebc Stabilize `pytest-warns-too-broad` (`PT030`) (#18568) 2025-06-17 09:58:12 -04:00
Dylan ebd2a27559 Stabilize `for-loop-writes` (`FURB122`) (#18565) 2025-06-17 09:58:12 -04:00
Dylan 1278e3442a Stabilize `pytest-warns-with-multiple-statements` (`PT031`) (#18569) 2025-06-17 09:58:12 -04:00