diff --git a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.py b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.py index 15631d15f2acd..8b2811eb63467 100644 --- a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.py +++ b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.py @@ -36,3 +36,11 @@ def f8(x: bytes = b"50 character byte stringgggggggggggggggggggggggggg\xff") -> baz: bytes = b"50 character byte stringgggggggggggggggggggggggggg" qux: bytes = b"51 character byte stringggggggggggggggggggggggggggg\xff" + + +class Demo: + """Docstrings are excluded from this rule. Some padding.""" + + +def func() -> None: + """Docstrings are excluded from this rule. Some padding.""" diff --git a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.pyi b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.pyi index d2f55531a2660..71064d9bdbd02 100644 --- a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.pyi +++ b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.pyi @@ -28,3 +28,9 @@ bar: str = "51 character stringgggggggggggggggggggggggggggggggg" # Error: PYI05 baz: bytes = b"50 character byte stringgggggggggggggggggggggggggg" # OK qux: bytes = b"51 character byte stringggggggggggggggggggggggggggg\xff" # Error: PYI053 + +class Demo: + """Docstrings are excluded from this rule. Some padding.""" # OK + +def func() -> None: + """Docstrings are excluded from this rule. Some padding.""" # OK diff --git a/crates/ruff/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs b/crates/ruff/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs index cecaed2685707..ff5ab572e88bd 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs @@ -2,6 +2,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Ranged}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::helpers::is_docstring_stmt; use crate::checkers::ast::Checker; use crate::registry::AsRule; @@ -41,6 +42,11 @@ impl AlwaysAutofixableViolation for StringOrBytesTooLong { /// PYI053 pub(crate) fn string_or_bytes_too_long(checker: &mut Checker, expr: &Expr) { + // Ignore docstrings. + if is_docstring_stmt(checker.semantic().stmt()) { + return; + } + let length = match expr { Expr::Constant(ast::ExprConstant { value: Constant::Str(s), diff --git a/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap b/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap index 275d11d8b435d..951ca3acb183b 100644 --- a/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap +++ b/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap @@ -89,6 +89,8 @@ PYI053.pyi:30:14: PYI053 [*] String and bytes literals longer than 50 characters 29 | 30 | qux: bytes = b"51 character byte stringggggggggggggggggggggggggggg\xff" # Error: PYI053 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 +31 | +32 | class Demo: | = help: Replace with `...` @@ -98,5 +100,8 @@ PYI053.pyi:30:14: PYI053 [*] String and bytes literals longer than 50 characters 29 29 | 30 |-qux: bytes = b"51 character byte stringggggggggggggggggggggggggggg\xff" # Error: PYI053 30 |+qux: bytes = ... # Error: PYI053 +31 31 | +32 32 | class Demo: +33 33 | """Docstrings are excluded from this rule. Some padding.""" # OK