From 2ad29089af4b86797d8012bc82fbae3b3a26175b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 29 Jan 2023 14:26:54 -0500 Subject: [PATCH] Allow list comprehensions for __all__ assignment (#2326) --- resources/test/fixtures/pylint/PLE0605.py | 7 ------- .../fixtures/pylint/invalid_all_format.py | 19 ++++++++++++++++++ .../{PLE0604.py => invalid_all_object.py} | 0 src/ast/operations.rs | 5 +++++ src/rules/pylint/mod.rs | 4 ++-- ...tests__PLE0604_invalid_all_object.py.snap} | 0 ...tests__PLE0605_invalid_all_format.py.snap} | 20 +++++++++++++++++++ 7 files changed, 46 insertions(+), 9 deletions(-) delete mode 100644 resources/test/fixtures/pylint/PLE0605.py create mode 100644 resources/test/fixtures/pylint/invalid_all_format.py rename resources/test/fixtures/pylint/{PLE0604.py => invalid_all_object.py} (100%) rename src/rules/pylint/snapshots/{ruff__rules__pylint__tests__PLE0604_PLE0604.py.snap => ruff__rules__pylint__tests__PLE0604_invalid_all_object.py.snap} (100%) rename src/rules/pylint/snapshots/{ruff__rules__pylint__tests__PLE0605_PLE0605.py.snap => ruff__rules__pylint__tests__PLE0605_invalid_all_format.py.snap} (69%) diff --git a/resources/test/fixtures/pylint/PLE0605.py b/resources/test/fixtures/pylint/PLE0605.py deleted file mode 100644 index 71acce9e96..0000000000 --- a/resources/test/fixtures/pylint/PLE0605.py +++ /dev/null @@ -1,7 +0,0 @@ -__all__ = ("CONST") # [invalid-all-format] - -__all__ = ["Hello"] + {"world"} # [invalid-all-format] - -__all__ += {"world"} # [invalid-all-format] - -__all__ = {"world"} + ["Hello"] # [invalid-all-format] diff --git a/resources/test/fixtures/pylint/invalid_all_format.py b/resources/test/fixtures/pylint/invalid_all_format.py new file mode 100644 index 0000000000..192d6aa42b --- /dev/null +++ b/resources/test/fixtures/pylint/invalid_all_format.py @@ -0,0 +1,19 @@ +__all__ = "CONST" # [invalid-all-format] + +__all__ = ["Hello"] + {"world"} # [invalid-all-format] + +__all__ += {"world"} # [invalid-all-format] + +__all__ = {"world"} + ["Hello"] # [invalid-all-format] + +__all__ = (x for x in ["Hello", "world"]) # [invalid-all-format] + +__all__ = {x for x in ["Hello", "world"]} # [invalid-all-format] + +__all__ = ["Hello"] + +__all__ = ("Hello",) + +__all__ = ["Hello"] + ("world",) + +__all__ = [x for x in ["Hello", "world"]] diff --git a/resources/test/fixtures/pylint/PLE0604.py b/resources/test/fixtures/pylint/invalid_all_object.py similarity index 100% rename from resources/test/fixtures/pylint/PLE0604.py rename to resources/test/fixtures/pylint/invalid_all_object.py diff --git a/src/ast/operations.rs b/src/ast/operations.rs index 6d5870ff1b..7021ef614f 100644 --- a/src/ast/operations.rs +++ b/src/ast/operations.rs @@ -88,6 +88,11 @@ pub fn extract_all_names( } } } + ExprKind::ListComp { .. } => { + // Allow list comprehensions, even though we can't statically analyze them. + // TODO(charlie): Allow `list()` and `tuple()` calls too, and extract the members + // from them (even if, e.g., it's `list({...})`). + } _ => { flags |= AllNamesFlags::INVALID_FORMAT; } diff --git a/src/rules/pylint/mod.rs b/src/rules/pylint/mod.rs index 3071e1ee42..16f41f2b4b 100644 --- a/src/rules/pylint/mod.rs +++ b/src/rules/pylint/mod.rs @@ -34,8 +34,8 @@ mod tests { #[test_case(Rule::MagicValueComparison, Path::new("magic_value_comparison.py"); "PLR2004")] #[test_case(Rule::UselessElseOnLoop, Path::new("useless_else_on_loop.py"); "PLW0120")] #[test_case(Rule::GlobalVariableNotAssigned, Path::new("global_variable_not_assigned.py"); "PLW0602")] - #[test_case(Rule::InvalidAllFormat, Path::new("PLE0605.py"); "PLE0605")] - #[test_case(Rule::InvalidAllObject, Path::new("PLE0604.py"); "PLE0604")] + #[test_case(Rule::InvalidAllFormat, Path::new("invalid_all_format.py"); "PLE0605")] + #[test_case(Rule::InvalidAllObject, Path::new("invalid_all_object.py"); "PLE0604")] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let diagnostics = test_path( diff --git a/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0604_PLE0604.py.snap b/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0604_invalid_all_object.py.snap similarity index 100% rename from src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0604_PLE0604.py.snap rename to src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0604_invalid_all_object.py.snap diff --git a/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0605_PLE0605.py.snap b/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0605_invalid_all_format.py.snap similarity index 69% rename from src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0605_PLE0605.py.snap rename to src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0605_invalid_all_format.py.snap index daf58a12ba..c0a9fd1def 100644 --- a/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0605_PLE0605.py.snap +++ b/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0605_invalid_all_format.py.snap @@ -42,4 +42,24 @@ expression: diagnostics column: 7 fix: ~ parent: ~ +- kind: + InvalidAllFormat: ~ + location: + row: 9 + column: 0 + end_location: + row: 9 + column: 7 + fix: ~ + parent: ~ +- kind: + InvalidAllFormat: ~ + location: + row: 11 + column: 0 + end_location: + row: 11 + column: 7 + fix: ~ + parent: ~