From fc69b59ef73b3f8e633f36ac515941d400bd8175 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 26 Jun 2024 11:24:01 -0400 Subject: [PATCH] Stabilize allowance of os.environ modifications between imports (#12047) ## Summary See: https://github.com/astral-sh/ruff/pull/10066. --- crates/ruff_linter/src/checkers/ast/mod.rs | 3 +-- crates/ruff_linter/src/rules/pycodestyle/mod.rs | 1 - .../rules/module_import_not_at_top_of_file.rs | 10 +++------- ...ter__rules__pycodestyle__tests__E402_E402_2.py.snap | 8 -------- ...s__pycodestyle__tests__preview__E402_E402_2.py.snap | 4 ---- 5 files changed, 4 insertions(+), 22 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__E402_E402_2.py.snap diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index 4c3fc040faf6f..88d42fddee366 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -462,8 +462,7 @@ impl<'a> Visitor<'a> for Checker<'a> { || helpers::in_nested_block(self.semantic.current_statements()) || imports::is_matplotlib_activation(stmt, self.semantic()) || imports::is_sys_path_modification(stmt, self.semantic()) - || (self.settings.preview.is_enabled() - && imports::is_os_environ_modification(stmt, self.semantic()))) + || imports::is_os_environ_modification(stmt, self.semantic())) { self.semantic.flags |= SemanticModelFlags::IMPORT_BOUNDARY; } diff --git a/crates/ruff_linter/src/rules/pycodestyle/mod.rs b/crates/ruff_linter/src/rules/pycodestyle/mod.rs index 3528436baa508..08b7a4d3a3456 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/mod.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/mod.rs @@ -69,7 +69,6 @@ mod tests { } #[test_case(Rule::IsLiteral, Path::new("constant_literals.py"))] - #[test_case(Rule::ModuleImportNotAtTopOfFile, Path::new("E402_2.py"))] #[test_case(Rule::RedundantBackslash, Path::new("E502.py"))] #[test_case(Rule::TooManyNewlinesAtEndOfFile, Path::new("W391_0.py"))] #[test_case(Rule::TooManyNewlinesAtEndOfFile, Path::new("W391_1.py"))] diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/module_import_not_at_top_of_file.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/module_import_not_at_top_of_file.rs index 9d2a9bbd61bef..db5b213bc9b17 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/module_import_not_at_top_of_file.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/module_import_not_at_top_of_file.rs @@ -13,12 +13,9 @@ use crate::checkers::ast::Checker; /// According to [PEP 8], "imports are always put at the top of the file, just after any /// module comments and docstrings, and before module globals and constants." /// -/// This rule makes an exception for `sys.path` modifications, allowing for -/// `sys.path.insert`, `sys.path.append`, and similar modifications between import -/// statements. -/// -/// In [preview], this rule also allows `os.environ` modifications between import -/// statements. +/// This rule makes an exception for both `sys.path` modifications (allowing for +/// `sys.path.insert`, `sys.path.append`, etc.) and `os.environ` modifications +/// between imports. /// /// ## Example /// ```python @@ -40,7 +37,6 @@ use crate::checkers::ast::Checker; /// ``` /// /// [PEP 8]: https://peps.python.org/pep-0008/#imports -/// [preview]: https://docs.astral.sh/ruff/preview/ #[violation] pub struct ModuleImportNotAtTopOfFile { source_type: PySourceType, diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E402_E402_2.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E402_E402_2.py.snap index 59c85e39a83e4..6dcc4546f11f9 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E402_E402_2.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E402_E402_2.py.snap @@ -1,12 +1,4 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E402_2.py:7:1: E402 Module level import not at top of file - | -5 | del os.environ["WORLD_SIZE"] -6 | -7 | import torch - | ^^^^^^^^^^^^ E402 - | - diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__E402_E402_2.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__E402_E402_2.py.snap deleted file mode 100644 index 6dcc4546f11f9..0000000000000 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__E402_E402_2.py.snap +++ /dev/null @@ -1,4 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/pycodestyle/mod.rs ---- -