Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow named expressions in __all__ assignments #7673

Merged
merged 1 commit into from
Sep 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

__all__ = foo["bar"] # [invalid-all-format]

__all__ = (foo := bar) # [invalid-all-format]

__all__ = ["Hello"]

__all__ = ("Hello",)
Expand All @@ -41,3 +43,7 @@
__all__ = __all__ + multiprocessing.__all__

__all__ = list[str](["Hello", "world"])

__all__ = list[str](foo())

__all__ = (foo := ["Hello", "world"])
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,27 @@ invalid_all_format.py:21:1: PLE0605 Invalid format for `__all__`, must be `tuple
21 | __all__ = foo["bar"] # [invalid-all-format]
| ^^^^^^^ PLE0605
22 |
23 | __all__ = ["Hello"]
23 | __all__ = (foo := bar) # [invalid-all-format]
|

invalid_all_format.py:23:1: PLE0605 Invalid format for `__all__`, must be `tuple` or `list`
|
21 | __all__ = foo["bar"] # [invalid-all-format]
22 |
23 | __all__ = (foo := bar) # [invalid-all-format]
| ^^^^^^^ PLE0605
24 |
25 | __all__ = ["Hello"]
|

invalid_all_format.py:23:12: PLE0605 Invalid format for `__all__`, must be `tuple` or `list`
|
21 | __all__ = foo["bar"] # [invalid-all-format]
22 |
23 | __all__ = (foo := bar) # [invalid-all-format]
| ^^^ PLE0605
24 |
25 | __all__ = ["Hello"]
|


12 changes: 8 additions & 4 deletions crates/ruff_python_ast/src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,21 @@ where
| Expr::Tuple(ast::ExprTuple { elts, .. }) => {
return (Some(elts), DunderAllFlags::empty());
}
Expr::ListComp(_) | Expr::SetComp(_) | Expr::GeneratorExp(_) => {
// Allow comprehensions, even though we can't statically analyze
// them.
_ => {
// We can't analyze other expressions, but they must be
// valid, since the `list` or `tuple` call will ultimately
// evaluate to a list or tuple.
return (None, DunderAllFlags::empty());
}
_ => {}
}
}
}
}
}
Expr::NamedExpr(ast::ExprNamedExpr { value, .. }) => {
// Allow, e.g., `__all__ += (value := ["A", "B"])`.
return extract_elts(value, is_builtin);
}
_ => {}
}
(None, DunderAllFlags::INVALID_FORMAT)
Expand Down