Skip to content

Conversation

MeGaGiGaGon
Copy link
Contributor

Summary

This PR fixes #7172 by suppressing the fixes for docstring-missing-returns (DOC201) / docstring-extraneous-returns (DOC202) if there is a surrounding line continuation character \ that would make the fix cause a syntax error.

To do this, the lints are changed from AlwaysFixableViolation to Violation with FixAvailability::Sometimes.

In the case of DOC201, the fix is not given if the non-break line ends in a line continuation character \. Note that lines are iterated in reverse from the docstring to the function definition.

In the case of DOC202, the fix is not given if the docstring ends with a line continuation character \.

Test Plan

Added a test case.

Copy link
Contributor

github-actions bot commented Jul 9, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
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. Could the same happen with classes? I noticed blank_before_after_class.rs in the same directory, and the implementation looks similar.

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Jul 10, 2025
@ntBre
Copy link
Contributor

ntBre commented Jul 10, 2025

I do wonder if something like iterating over the LogicalLines could be a better solution here:

impl<'a> LogicalLines<'a> {
pub(crate) fn from_tokens(tokens: &Tokens, locator: &'a Locator<'a>) -> Self {

but I'm not totally sure if that's how it works. That might also skip the diagnostic entirely, which could also be reasonable.

@MeGaGiGaGon
Copy link
Contributor Author

Thanks, this looks reasonable to me. Could the same happen with classes? I noticed blank_before_after_class.rs in the same directory, and the implementation looks similar.

It looks like the same can happen with the before check (D211) since it works the same, but not the after check since it enforces 1 blank line https://play.ruff.rs/a2fac120-588a-4a1d-9287-0663cd0242e3

@ntBre
Copy link
Contributor

ntBre commented Jul 14, 2025

Sorry I left this in kind of an ambiguous state. I'll merge this now and we can follow up on classes and the LogicalLines idea separately (if at all, especially in the LogicalLines case). Thanks again!

@ntBre ntBre merged commit 966fd6f into astral-sh:main Jul 14, 2025
37 checks passed
@MeGaGiGaGon MeGaGiGaGon deleted the fix-D201-D202-fix-error branch July 14, 2025 19:01
dcreager added a commit that referenced this pull request Jul 15, 2025
* main:
  [`pylint`] Extend invalid string character rules to include t-strings (#19355)
  Make TC010 docs example more realistic (#19356)
  Move RDJSON rendering to `ruff_db` (#19293)
  [`flake8-use-pathlib`] Skip single dots for `invalid-pathlib-with-suffix` (`PTH210`) on versions >= 3.14 (#19331)
  [`ruff`] Allow `strict` kwarg when checking for `starmap-zip` (`RUF058`) in Python 3.14+ (#19333)
  [ty] Reduce false positives for `TypedDict` types (#19354)
  [ty] Remove `ConnectionInitializer` (#19353)
  [ty] Use `Type::string_literal()` more (#19352)
  [ty] Add ecosystem-report workflow (#19349)
  [ty] Make use of salsa `Lookup` when interning values (#19347)
  [ty] Sync vendored typeshed stubs (#19345)
  [`pylint`] Make example error out-of-the-box (`PLE2502`) (#19272)
  [`pydoclint`] Fix `SyntaxError` from fixes with line continuations (`D201`, `D202`) (#19246)
dcreager added a commit that referenced this pull request Jul 15, 2025
* dcreager/merge-arguments:
  add types iterator
  add asserting constructor
  debug assert lengths
  remove unused From
  use FromIterator
  [`pylint`] Extend invalid string character rules to include t-strings (#19355)
  Make TC010 docs example more realistic (#19356)
  Move RDJSON rendering to `ruff_db` (#19293)
  [`flake8-use-pathlib`] Skip single dots for `invalid-pathlib-with-suffix` (`PTH210`) on versions >= 3.14 (#19331)
  [`ruff`] Allow `strict` kwarg when checking for `starmap-zip` (`RUF058`) in Python 3.14+ (#19333)
  [ty] Reduce false positives for `TypedDict` types (#19354)
  [ty] Remove `ConnectionInitializer` (#19353)
  [ty] Use `Type::string_literal()` more (#19352)
  [ty] Add ecosystem-report workflow (#19349)
  [ty] Make use of salsa `Lookup` when interning values (#19347)
  [ty] Sync vendored typeshed stubs (#19345)
  [`pylint`] Make example error out-of-the-box (`PLE2502`) (#19272)
  [`pydoclint`] Fix `SyntaxError` from fixes with line continuations (`D201`, `D202`) (#19246)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule D202 cause autofix error
2 participants