Repository navigation
[flake8-tidy-imports] Add extend-banned-api
#28644
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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()); | ||
| 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." | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe the indent here is necessary. Isn't |
||
| "#, | ||
| )?; | ||
| 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", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| )?; | ||
|
|
||
| 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()?; | ||
|
|
||
| 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 ----- |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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)