Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-tidy-imports] Allow banning global symbols (TID251) #15736

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -34,3 +34,17 @@

# relative imports are respected
from .typing import TypedDict


# https://github.com/astral-sh/ruff/issues/10079
list()

from builtins import list as l
l()

import builtins
builtins.list

class C:
@property
def foo(self): ...
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
}
}
}
if checker.enabled(Rule::BannedApi) {
flake8_tidy_imports::rules::banned_name(checker, expr);
}
}
ExprContext::Store => {
if checker.enabled(Rule::NonLowercaseVariableInFunction) {
Expand Down
27 changes: 27 additions & 0 deletions crates/ruff_linter/src/rules/flake8_tidy_imports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ mod tests {

use anyhow::Result;
use rustc_hash::FxHashMap;
use test_case::test_case;

use crate::assert_messages;
use crate::registry::Rule;
use crate::rules::flake8_tidy_imports;
use crate::rules::flake8_tidy_imports::settings::{ApiBan, Strictness};
use crate::settings::types::PreviewMode;
use crate::settings::LinterSettings;
use crate::test::test_path;

Expand Down Expand Up @@ -46,6 +48,31 @@ mod tests {
Ok(())
}

#[test_case("list")]
#[test_case("builtins.list")]
#[test_case("property")]
fn banned_api_preview(path: &str) -> Result<()> {
let snapshot = format!("preview__TID251_{}", path.replace('.', "-"));
let diagnostics = test_path(
Path::new("flake8_tidy_imports/TID251.py"),
&LinterSettings {
preview: PreviewMode::Enabled,
flake8_tidy_imports: flake8_tidy_imports::settings::Settings {
banned_api: FxHashMap::from_iter([(
path.to_string(),
ApiBan {
msg: format!("`{path}` considered harmful."),
},
)]),
..Default::default()
},
..LinterSettings::for_rules(vec![Rule::BannedApi])
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test]
fn banned_api_package() -> Result<()> {
let diagnostics = test_path(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ pub(crate) fn banned_api<T: Ranged>(checker: &mut Checker, policy: &NameMatchPol

/// TID251
pub(crate) fn banned_attribute_access(checker: &mut Checker, expr: &Expr) {
banned_reference(checker, expr);
}

pub(crate) fn banned_name(checker: &mut Checker, expr: &Expr) {
if checker.settings.preview.is_disabled() {
return;
}

if matches!(
checker.semantic().current_expression_parent(),
Some(Expr::Attribute(_))
) {
return;
}

banned_reference(checker, expr);
}

fn banned_reference(checker: &mut Checker, expr: &Expr) {
let banned_api = &checker.settings.flake8_tidy_imports.banned_api;
if banned_api.is_empty() {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: crates/ruff_linter/src/rules/flake8_tidy_imports/mod.rs
---
TID251.py:42:22: TID251 `builtins.list` is banned: `builtins.list` considered harmful.
|
40 | list()
41 |
42 | from builtins import list as l
| ^^^^^^^^^ TID251
43 | l()
|

TID251.py:43:1: TID251 `builtins.list` is banned: `builtins.list` considered harmful.
|
42 | from builtins import list as l
43 | l()
| ^ TID251
44 |
45 | import builtins
|

TID251.py:46:1: TID251 `builtins.list` is banned: `builtins.list` considered harmful.
|
45 | import builtins
46 | builtins.list
| ^^^^^^^^^^^^^ TID251
47 |
48 | class C:
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/ruff_linter/src/rules/flake8_tidy_imports/mod.rs
---
TID251.py:40:1: TID251 `list` is banned: `list` considered harmful.
|
39 | # https://github.com/astral-sh/ruff/issues/10079
40 | list()
| ^^^^ TID251
41 |
42 | from builtins import list as l
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/ruff_linter/src/rules/flake8_tidy_imports/mod.rs
---
TID251.py:49:6: TID251 `property` is banned: `property` considered harmful.
|
48 | class C:
49 | @property
| ^^^^^^^^ TID251
50 | def foo(self): ...
|
Loading