Allow list comprehensions for __all__ assignment (#2326)

This commit is contained in:
Charlie Marsh
2023-01-29 14:26:54 -05:00
committed by GitHub
parent f41796d559
commit 2ad29089af
7 changed files with 46 additions and 9 deletions

View File

@@ -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]

View File

@@ -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"]]

View File

@@ -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;
}

View File

@@ -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(

View File

@@ -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: ~