Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
87 changes: 87 additions & 0 deletions crates/ruff/tests/cli/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,93 @@ extend = "ruff3.toml"
Ok(())
}

#[test]
fn extend_banned_api() -> Result<()> {
let fixture = CliTest::new()?;
fixture.write_file(
"ruff.toml",
r#"
[lint]
select = ["TID251"]

[lint.flake8-tidy-imports.extend-banned-api]
"typing.TypedDict".msg = "Use typing_extensions.TypedDict instead."
"cgi".msg = "Use a supported library instead."
"typing.Any".msg = "Use a precise type instead."
"#,
)?;
fixture.write_file(
"test.py",
"import cgi\nfrom typing import TypedDict\nimport typing\nx: typing.Any\n",
)?;

assert_cmd_snapshot!(fixture.check_command());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the snapshots are small enough that they can be inline snapshots (which is what we now mainly use for tests)

Ok(())
}

#[test]
fn extend_banned_api_inherited() -> Result<()> {
let fixture = CliTest::with_settings(|_, mut settings| {
// The default path filter preserves `\n` escapes, including `\nested` on Windows.
settings.add_filter(r"child\\nested", "child/nested");
settings
})?;
fixture.write_file(
"ruff.toml",
r#"
[lint]
select = ["TID251"]

[lint.flake8-tidy-imports.banned-api]
"cgi".msg = "The cgi module is deprecated."
"pipes".msg = "Use shlex instead."

[lint.flake8-tidy-imports.extend-banned-api]
"typing.Any".msg = "Use a precise type instead."
"#,
)?;
fixture.write_file(
"child/ruff.toml",
r#"
extend = "../ruff.toml"

[lint.flake8-tidy-imports.extend-banned-api]
"cgi".msg = "Use a supported library instead."
"typing.TypedDict".msg = "Use typing_extensions.TypedDict instead."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe the indent here is necessary. Isn't CliTest calling dedent?

"#,
)?;
fixture.write_file("child/nested/ruff.toml", "extend = \"../ruff.toml\"\n")?;
fixture.write_file(
"child/nested/test.py",
"import cgi\nimport pipes\nfrom typing import Any, TypedDict\n",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest annoyance with codex right now. Can you ask it to pretty please use rust multiline strings instead of squeezing every file on a single line (I believe write_file even calls dedent for you)

)?;

assert_cmd_snapshot!(fixture.check_command());

// Clear inherited extensions while preserving the inherited base bans.
fixture.write_file(
"child/nested/ruff.toml",
r#"
extend = "../ruff.toml"
[lint.flake8-tidy-imports]
extend-banned-api = {}
"#,
)?;
assert_cmd_snapshot!(fixture.check_command());

// Clear inherited base bans while preserving the inherited extensions.
fixture.write_file(
"child/nested/ruff.toml",
r#"
extend = "../ruff.toml"
[lint.flake8-tidy-imports]
banned-api = {}
"#,
)?;
assert_cmd_snapshot!(fixture.check_command());
Ok(())
}

#[test]
fn circular_extend() -> Result<()> {
let fixture = CliTest::new()?;
Expand Down
19 changes: 19 additions & 0 deletions crates/ruff/tests/cli/snapshots/cli__lint__extend_banned_api.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: crates/ruff/tests/cli/lint.rs
info:
program: ruff
args:
- check
- "--no-cache"
- "--output-format"
- concise
---
success: false
exit_code: 1
----- stdout -----
test.py:1:8: TID251 `cgi` is banned: Use a supported library instead.
test.py:2:20: TID251 `typing.TypedDict` is banned: Use typing_extensions.TypedDict instead.
test.py:4:4: TID251 `typing.Any` is banned: Use a precise type instead.
Found 3 errors.

----- stderr -----
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/ruff/tests/cli/lint.rs
info:
program: ruff
args:
- check
- "--no-cache"
- "--output-format"
- concise
---
success: false
exit_code: 1
----- stdout -----
child/nested/test.py:1:8: TID251 `cgi` is banned: The cgi module is deprecated.
child/nested/test.py:2:8: TID251 `pipes` is banned: Use shlex instead.
Found 2 errors.

----- stderr -----
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/ruff/tests/cli/lint.rs
info:
program: ruff
args:
- check
- "--no-cache"
- "--output-format"
- concise
---
success: false
exit_code: 1
----- stdout -----
child/nested/test.py:1:8: TID251 `cgi` is banned: Use a supported library instead.
child/nested/test.py:3:25: TID251 `typing.TypedDict` is banned: Use typing_extensions.TypedDict instead.
Found 2 errors.

----- stderr -----
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: crates/ruff/tests/cli/lint.rs
info:
program: ruff
args:
- check
- "--no-cache"
- "--output-format"
- concise
---
success: false
exit_code: 1
----- stdout -----
child/nested/test.py:1:8: TID251 `cgi` is banned: Use a supported library instead.
child/nested/test.py:2:8: TID251 `pipes` is banned: Use shlex instead.
child/nested/test.py:3:25: TID251 `typing.TypedDict` is banned: Use typing_extensions.TypedDict instead.
Found 3 errors.

----- stderr -----
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::rules::flake8_tidy_imports::matchers::NameMatchPolicy;
///
/// ## Options
/// - `lint.flake8-tidy-imports.banned-api`
/// - `lint.flake8-tidy-imports.extend-banned-api`
#[derive(ViolationMetadata)]
#[violation_metadata(stable_since = "v0.0.201", category = Category::Restriction)]
pub(crate) struct BannedApi {
Expand Down
24 changes: 23 additions & 1 deletion crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,9 @@ pub struct Flake8TidyImportsOptions {
ban_relative_imports: Option<Strictness>,

/// Specific modules or module members that may not be imported or accessed.
/// These can be extended by the
/// [`extend-banned-api`](#lint_flake8-tidy-imports_extend-banned-api) option.
///
/// Note that this rule is only meant to flag accidental uses,
/// and can be circumvented via `eval` or `importlib`.
#[option(
Expand All @@ -2168,6 +2171,20 @@ pub struct Flake8TidyImportsOptions {
)]
banned_api: Option<FxHashMap<String, ApiBan>>,

/// Additional modules or module members that may not be imported or accessed.
/// These entries will be added to the
/// [`banned-api`](#lint_flake8-tidy-imports_banned-api) mapping and will override
/// any existing entries if the two settings overlap.
#[option(
default = r#"{}"#,
value_type = r#"dict[str, { "msg": str }]"#,
scope = "extend-banned-api",
example = r#"
"typing.TypedDict".msg = "Use typing_extensions.TypedDict instead."
"#
)]
extend_banned_api: Option<FxHashMap<String, ApiBan>>,

/// List of specific modules that may not be imported at module level, and should instead be
/// imported lazily (e.g., within a function definition, or an `if TYPE_CHECKING:`
/// block, or some other nested context). This also affects the rule `import-outside-top-level`
Expand Down Expand Up @@ -2220,6 +2237,11 @@ pub struct Flake8TidyImportsOptions {

impl Flake8TidyImportsOptions {
pub(crate) fn try_into_settings(self) -> Result<flake8_tidy_imports::settings::Settings> {
let mut banned_api = self.banned_api.unwrap_or_default();
if let Some(extend_banned_api) = self.extend_banned_api {
banned_api.extend(extend_banned_api);
}

let require_lazy = self.require_lazy.unwrap_or_default();
let ban_lazy = self.ban_lazy.unwrap_or_default();

Expand All @@ -2231,7 +2253,7 @@ impl Flake8TidyImportsOptions {

Ok(flake8_tidy_imports::settings::Settings {
ban_relative_imports: self.ban_relative_imports.unwrap_or(Strictness::Parents),
banned_api: self.banned_api.unwrap_or_default(),
banned_api,
banned_module_level_imports: self.banned_module_level_imports.unwrap_or_default(),
require_lazy,
ban_lazy,
Expand Down
12 changes: 11 additions & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading