Skip to content

Commit

Permalink
Exclude docstrings from PYI053 (#5405)
Browse files Browse the repository at this point in the history
## Summary

The `Y053` rule of `flake8-pyi` ignores docstrings, it only triggers on
other string literals.

The separate `Y021/PYI021` rule exists to disallow docstrings.

## Test Plan

Added some `# OK` test cases to `PYI053.py(i)` files.
  • Loading branch information
intgr authored Jun 28, 2023
1 parent 56f73de commit 2c99b26
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI053.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 `...`

Expand All @@ -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


0 comments on commit 2c99b26

Please sign in to comment.