Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ linter.flake8_self.ignore_names = [
linter.flake8_tidy_imports.ban_relative_imports = "parents"
linter.flake8_tidy_imports.banned_api = {}
linter.flake8_tidy_imports.banned_module_level_imports = []
linter.flake8_tidy_imports.banned_eager_imports = []
linter.flake8_type_checking.strict = false
linter.flake8_type_checking.exempt_modules = [
typing,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

import os
import typing as t
lazy import pathlib

from foo import bar
lazy from foo import baz
from starry import *

with manager():
import email

if True:
from bar import qux

try:
import collections
except Exception:
pass

def func():
import fractions

class Example:
import decimal

x = 1; import pkg.submodule
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import os
from foo import bar
6 changes: 6 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.is_rule_enabled(Rule::BannedModuleLevelImports) {
flake8_tidy_imports::rules::banned_module_level_imports(checker, stmt);
}
if checker.is_rule_enabled(Rule::BannedEagerImports) {
flake8_tidy_imports::rules::banned_eager_imports(checker, stmt);
}

for alias in names {
if checker.is_rule_enabled(Rule::NonAsciiImportName) {
Expand Down Expand Up @@ -786,6 +789,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.is_rule_enabled(Rule::BannedModuleLevelImports) {
flake8_tidy_imports::rules::banned_module_level_imports(checker, stmt);
}
if checker.is_rule_enabled(Rule::BannedEagerImports) {
flake8_tidy_imports::rules::banned_eager_imports(checker, stmt);
}

if checker.is_rule_enabled(Rule::PytestIncorrectPytestImport) {
flake8_pytest_style::rules::import_from(checker, stmt, module, level);
Expand Down
44 changes: 24 additions & 20 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,29 @@ impl<'a> Checker<'a> {
Generator::new(self.stylist.indentation(), self.stylist.line_ending())
}

pub(crate) fn lazy_import_context(&self) -> Option<LazyImportContext> {
match self.semantic.current_scope().kind {
// Possible, but invalid positions.
ScopeKind::Function(_) => return Some(LazyImportContext::Function),
ScopeKind::Class(_) => return Some(LazyImportContext::Class),
// Valid position.
ScopeKind::Module => {}
// Impossible positions because lambdas and comprehensions can't contain statements.
ScopeKind::Lambda(_)
| ScopeKind::Generator { .. }
| ScopeKind::Type
| ScopeKind::DunderClassCell => {}
}

for statement in self.semantic.current_statements().skip(1) {
if matches!(statement, Stmt::Try(_)) {
return Some(LazyImportContext::TryExceptBlocks);
}
}

None
}

/// Return the preferred quote for a generated `StringLiteral` node, given where we are in the
/// AST.
fn preferred_quote(&self) -> Quote {
Expand Down Expand Up @@ -803,26 +826,7 @@ impl SemanticSyntaxContext for Checker<'_> {
}

fn lazy_import_context(&self) -> Option<LazyImportContext> {
match self.semantic.current_scope().kind {
// Possible, but invalid positions.
ScopeKind::Function(_) => return Some(LazyImportContext::Function),
ScopeKind::Class(_) => return Some(LazyImportContext::Class),
// Valid position.
ScopeKind::Module => {}
// Impossible positions because lambdas and comprehensions can't contain statements.
ScopeKind::Lambda(_)
| ScopeKind::Generator { .. }
| ScopeKind::Type
| ScopeKind::DunderClassCell => {}
}

for statement in self.semantic.current_statements().skip(1) {
if matches!(statement, Stmt::Try(_)) {
return Some(LazyImportContext::TryExceptBlocks);
}
}

None
self.lazy_import_context()
}

fn in_async_context(&self) -> bool {
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Flake8TidyImports, "251") => rules::flake8_tidy_imports::rules::BannedApi,
(Flake8TidyImports, "252") => rules::flake8_tidy_imports::rules::RelativeImports,
(Flake8TidyImports, "253") => rules::flake8_tidy_imports::rules::BannedModuleLevelImports,
(Flake8TidyImports, "254") => rules::flake8_tidy_imports::rules::BannedEagerImports,

// flake8-return
(Flake8Return, "501") => rules::flake8_return::rules::UnnecessaryReturnNone,
Expand Down
Loading
Loading