-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86cdaea
commit 8f891cf
Showing
8 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
crates/ruff_linter/resources/test/fixtures/pylint/import_outside_top_level.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
# Verify that statements nested in conditionals (such as top-level type-checking blocks) | ||
# are still considered top-level | ||
if TYPE_CHECKING: | ||
import string | ||
|
||
|
||
def import_in_function(): | ||
import symtable # [import-outside-toplevel] | ||
import os, sys # [import-outside-toplevel] | ||
import time as thyme # [import-outside-toplevel] | ||
import random as rand, socket as sock # [import-outside-toplevel] | ||
from collections import defaultdict # [import-outside-toplevel] | ||
from math import sin as sign, cos as cosplay # [import-outside-toplevel] | ||
|
||
|
||
class ClassWithImports: | ||
import tokenize # [import-outside-toplevel] | ||
|
||
def __init__(self): | ||
import trace # [import-outside-toplevel] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
crates/ruff_linter/src/rules/pylint/rules/import_outside_top_level.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::Stmt; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `import` statements outside of a module's top-level scope, such | ||
/// as within a function or class definition. | ||
/// | ||
/// ## Why is this bad? | ||
/// [PEP 8] recommends placing imports not only at the top-level of a module, | ||
/// but at the very top of the file, "just after any module comments and | ||
/// docstrings, and before module globals and constants." | ||
/// | ||
/// `import` statements have effects that are global in scope; defining them at | ||
/// the top level has a number of benefits. For example, it makes it easier to | ||
/// identify the dependencies of a module, and ensures that any invalid imports | ||
/// are caught regardless of whether a specific function is called or class is | ||
/// instantiated. | ||
/// | ||
/// An import statement would typically be placed within a function only to | ||
/// avoid a circular dependency, to defer a costly module load, or to avoid | ||
/// loading a dependency altogether in a certain runtime environment. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// def print_python_version(): | ||
/// import platform | ||
/// | ||
/// print(python.python_version()) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import platform | ||
/// | ||
/// | ||
/// def print_python_version(): | ||
/// print(python.python_version()) | ||
/// ``` | ||
/// | ||
/// [PEP 8]: https://peps.python.org/pep-0008/#imports | ||
#[violation] | ||
pub struct ImportOutsideTopLevel; | ||
|
||
impl Violation for ImportOutsideTopLevel { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`import` should be at the top-level of a file") | ||
} | ||
} | ||
|
||
/// C0415 | ||
pub(crate) fn import_outside_top_level(checker: &mut Checker, stmt: &Stmt) { | ||
if !checker.semantic().current_scope().kind.is_module() { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(ImportOutsideTopLevel, stmt.range())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...int/snapshots/ruff_linter__rules__pylint__tests__PLC0415_import_outside_top_level.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
import_outside_top_level.py:10:5: PLC0415 `import` should be at the top-level of a file | ||
| | ||
9 | def import_in_function(): | ||
10 | import symtable # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^^^^ PLC0415 | ||
11 | import os, sys # [import-outside-toplevel] | ||
12 | import time as thyme # [import-outside-toplevel] | ||
| | ||
|
||
import_outside_top_level.py:11:5: PLC0415 `import` should be at the top-level of a file | ||
| | ||
9 | def import_in_function(): | ||
10 | import symtable # [import-outside-toplevel] | ||
11 | import os, sys # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^^^ PLC0415 | ||
12 | import time as thyme # [import-outside-toplevel] | ||
13 | import random as rand, socket as sock # [import-outside-toplevel] | ||
| | ||
|
||
import_outside_top_level.py:12:5: PLC0415 `import` should be at the top-level of a file | ||
| | ||
10 | import symtable # [import-outside-toplevel] | ||
11 | import os, sys # [import-outside-toplevel] | ||
12 | import time as thyme # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^^^^^^^^^ PLC0415 | ||
13 | import random as rand, socket as sock # [import-outside-toplevel] | ||
14 | from collections import defaultdict # [import-outside-toplevel] | ||
| | ||
|
||
import_outside_top_level.py:13:5: PLC0415 `import` should be at the top-level of a file | ||
| | ||
11 | import os, sys # [import-outside-toplevel] | ||
12 | import time as thyme # [import-outside-toplevel] | ||
13 | import random as rand, socket as sock # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLC0415 | ||
14 | from collections import defaultdict # [import-outside-toplevel] | ||
15 | from math import sin as sign, cos as cosplay # [import-outside-toplevel] | ||
| | ||
|
||
import_outside_top_level.py:14:5: PLC0415 `import` should be at the top-level of a file | ||
| | ||
12 | import time as thyme # [import-outside-toplevel] | ||
13 | import random as rand, socket as sock # [import-outside-toplevel] | ||
14 | from collections import defaultdict # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLC0415 | ||
15 | from math import sin as sign, cos as cosplay # [import-outside-toplevel] | ||
| | ||
|
||
import_outside_top_level.py:15:5: PLC0415 `import` should be at the top-level of a file | ||
| | ||
13 | import random as rand, socket as sock # [import-outside-toplevel] | ||
14 | from collections import defaultdict # [import-outside-toplevel] | ||
15 | from math import sin as sign, cos as cosplay # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLC0415 | ||
| | ||
|
||
import_outside_top_level.py:19:5: PLC0415 `import` should be at the top-level of a file | ||
| | ||
18 | class ClassWithImports: | ||
19 | import tokenize # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^^^^ PLC0415 | ||
20 | | ||
21 | def __init__(self): | ||
| | ||
|
||
import_outside_top_level.py:22:9: PLC0415 `import` should be at the top-level of a file | ||
| | ||
21 | def __init__(self): | ||
22 | import trace # [import-outside-toplevel] | ||
| ^^^^^^^^^^^^ PLC0415 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.