Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `string-or-bytes-too-long` (`PYI053`)

```toml
[lint]
select = ["PYI053"]
```

## Long name in `__all__`

Strings in `__all__` correspond to exported names and should be exempt from the rule.

```pyi
__all__ = [
"aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeef",
]
```
64 changes: 36 additions & 28 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2792,34 +2792,7 @@ impl<'a> Checker<'a> {
_ => {}
}

let scope = self.semantic.current_scope();

if scope.kind.is_module()
&& match parent {
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
if let Some(Expr::Name(ast::ExprName { id, .. })) = targets.first() {
id == "__all__"
} else {
false
}
}
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
id == "__all__"
} else {
false
}
}
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
id == "__all__"
} else {
false
}
}
_ => false,
}
{
if self.in_dunder_all_assignment(parent) {
let (all_names, all_flags) = self.semantic.extract_dunder_all_names(parent);

if all_flags.intersects(DunderAllFlags::INVALID_OBJECT) {
Expand Down Expand Up @@ -3285,6 +3258,41 @@ impl<'a> Checker<'a> {

self.semantic.restore(snapshot);
}

/// Report whether a module-level `__all__` assignment is being visited.
///
/// This differs from [`SemanticModel::in_dunder_all_definition`], which is set only while
/// adding bindings for the entries in `__all__`.
pub(crate) fn in_dunder_all_assignment(&self, parent: &Stmt) -> bool {
if !self.semantic.current_scope().kind.is_module() {
return false;
}

match parent {
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
if let Some(Expr::Name(ast::ExprName { id, .. })) = targets.first() {
id == "__all__"
} else {
false
}
}
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
id == "__all__"
} else {
false
}
}
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
id == "__all__"
} else {
false
}
}
_ => false,
}
}
}

struct ParsedAnnotationsCache<'a> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use crate::{AlwaysFixableViolation, Edit, Fix};
/// checkers, the primary consumers of stub files. Replace very long constants
/// with ellipses (`...`) to simplify the stub.
///
/// The rule does not apply to long entries in `__all__`, which are assumed to
/// be outside the stub author's control.
///
/// ## Example
///
/// ```pyi
Expand Down Expand Up @@ -51,8 +54,10 @@ impl AlwaysFixableViolation for StringOrBytesTooLong {
pub(crate) fn string_or_bytes_too_long(checker: &Checker, string: StringLike) {
let semantic = checker.semantic();

let parent = semantic.current_statement();

// Ignore docstrings.
if is_docstring_stmt(semantic.current_statement()) {
if is_docstring_stmt(parent) {
return;
}

Expand All @@ -64,6 +69,10 @@ pub(crate) fn string_or_bytes_too_long(checker: &Checker, string: StringLike) {
return;
}

if checker.in_dunder_all_assignment(parent) {
return;
}

let length = match string {
StringLike::String(ast::ExprStringLiteral { value, .. }) => value.chars().count(),
StringLike::Bytes(ast::ExprBytesLiteral { value, .. }) => value.len(),
Expand Down
Loading