Use tool.ruff.lint in more places (#8317)

## Summary

As a follow-up of https://github.com/astral-sh/ruff/pull/7732, use
`tool.ruff.lint` in more places in documentations, tests and internal
usages.
This commit is contained in:
Mathieu Kniewallner 2023-10-29 01:39:38 +02:00 committed by GitHub
parent 2f5734d1ac
commit 317b6e8682
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 45 deletions

View file

@ -1,7 +1,7 @@
[tool.ruff] [tool.ruff]
line-length = 88 line-length = 88
[tool.ruff.isort] [tool.ruff.lint.isort]
lines-after-imports = 3 lines-after-imports = 3
lines-between-types = 2 lines-between-types = 2
known-local-folder = ["ruff"] known-local-folder = ["ruff"]

View file

@ -5,4 +5,6 @@ extend-exclude = [
"migrations", "migrations",
"with_excluded_file/other_excluded_file.py", "with_excluded_file/other_excluded_file.py",
] ]
[tool.ruff.lint]
per-file-ignores = { "__init__.py" = ["F401"] } per-file-ignores = { "__init__.py" = ["F401"] }

View file

@ -1,5 +1,7 @@
[tool.ruff] [tool.ruff]
src = [".", "python_modules/*"] src = [".", "python_modules/*"]
exclude = ["examples/excluded"] exclude = ["examples/excluded"]
[tool.ruff.lint]
extend-select = ["I001"] extend-select = ["I001"]
extend-ignore = ["F841"] extend-ignore = ["F841"]

View file

@ -265,7 +265,7 @@ impl Violation for MissingReturnTypePrivateFunction {
/// or `ruff.toml` file: /// or `ruff.toml` file:
/// ///
/// ```toml /// ```toml
/// [tool.ruff.flake8-annotations] /// [tool.ruff.lint.flake8-annotations]
/// mypy-init-return = true /// mypy-init-return = true
/// ``` /// ```
/// ///

View file

@ -828,7 +828,7 @@ pub struct LintCommonOptions {
value_type = "dict[str, list[RuleSelector]]", value_type = "dict[str, list[RuleSelector]]",
example = r#" example = r#"
# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`. # Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
[tool.ruff.per-file-ignores] [tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402"] "__init__.py" = ["E402"]
"path/to/file.py" = ["E402"] "path/to/file.py" = ["E402"]
"# "#
@ -842,7 +842,7 @@ pub struct LintCommonOptions {
value_type = "dict[str, list[RuleSelector]]", value_type = "dict[str, list[RuleSelector]]",
example = r#" example = r#"
# Also ignore `E402` in all `__init__.py` files. # Also ignore `E402` in all `__init__.py` files.
[tool.ruff.extend-per-file-ignores] [tool.ruff.lint.extend-per-file-ignores]
"__init__.py" = ["E402"] "__init__.py" = ["E402"]
"# "#
)] )]
@ -1198,7 +1198,7 @@ pub struct Flake8ImportConventionsOptions {
default = r#"{"altair": "alt", "matplotlib": "mpl", "matplotlib.pyplot": "plt", "numpy": "np", "pandas": "pd", "seaborn": "sns", "tensorflow": "tf", "tkinter": "tk", "holoviews": "hv", "panel": "pn", "plotly.express": "px", "polars": "pl", "pyarrow": "pa"}"#, default = r#"{"altair": "alt", "matplotlib": "mpl", "matplotlib.pyplot": "plt", "numpy": "np", "pandas": "pd", "seaborn": "sns", "tensorflow": "tf", "tkinter": "tk", "holoviews": "hv", "panel": "pn", "plotly.express": "px", "polars": "pl", "pyarrow": "pa"}"#,
value_type = "dict[str, str]", value_type = "dict[str, str]",
example = r#" example = r#"
[tool.ruff.flake8-import-conventions.aliases] [tool.ruff.lint.flake8-import-conventions.aliases]
# Declare the default aliases. # Declare the default aliases.
altair = "alt" altair = "alt"
"matplotlib.pyplot" = "plt" "matplotlib.pyplot" = "plt"
@ -1216,7 +1216,7 @@ pub struct Flake8ImportConventionsOptions {
default = r#"{}"#, default = r#"{}"#,
value_type = "dict[str, str]", value_type = "dict[str, str]",
example = r#" example = r#"
[tool.ruff.flake8-import-conventions.extend-aliases] [tool.ruff.lint.flake8-import-conventions.extend-aliases]
# Declare a custom alias for the `matplotlib` module. # Declare a custom alias for the `matplotlib` module.
"dask.dataframe" = "dd" "dask.dataframe" = "dd"
"# "#
@ -1228,7 +1228,7 @@ pub struct Flake8ImportConventionsOptions {
default = r#"{}"#, default = r#"{}"#,
value_type = "dict[str, list[str]]", value_type = "dict[str, list[str]]",
example = r#" example = r#"
[tool.ruff.flake8-import-conventions.banned-aliases] [tool.ruff.lint.flake8-import-conventions.banned-aliases]
# Declare the banned aliases. # Declare the banned aliases.
"tensorflow.keras.backend" = ["K"] "tensorflow.keras.backend" = ["K"]
"# "#
@ -1541,7 +1541,7 @@ pub struct Flake8TidyImportsOptions {
default = r#"{}"#, default = r#"{}"#,
value_type = r#"dict[str, { "msg": str }]"#, value_type = r#"dict[str, { "msg": str }]"#,
example = r#" example = r#"
[tool.ruff.flake8-tidy-imports.banned-api] [tool.ruff.lint.flake8-tidy-imports.banned-api]
"cgi".msg = "The cgi module is deprecated, see https://peps.python.org/pep-0594/#cgi." "cgi".msg = "The cgi module is deprecated, see https://peps.python.org/pep-0594/#cgi."
"typing.TypedDict".msg = "Use typing_extensions.TypedDict instead." "typing.TypedDict".msg = "Use typing_extensions.TypedDict instead."
"# "#
@ -1994,7 +1994,7 @@ pub struct IsortOptions {
value_type = "dict[str, list[str]]", value_type = "dict[str, list[str]]",
example = r#" example = r#"
# Group all Django imports into a separate section. # Group all Django imports into a separate section.
[tool.ruff.isort.sections] [tool.ruff.lint.isort.sections]
"django" = ["django"] "django" = ["django"]
"# "#
)] )]
@ -2376,7 +2376,7 @@ pub struct PydocstyleOptions {
/// documentation for every function parameter: /// documentation for every function parameter:
/// ///
/// ```toml /// ```toml
/// [tool.ruff] /// [tool.ruff.lint]
/// # Enable all `pydocstyle` rules, limiting to those that adhere to the /// # Enable all `pydocstyle` rules, limiting to those that adhere to the
/// # Google convention via `convention = "google"`, below. /// # Google convention via `convention = "google"`, below.
/// select = ["D"] /// select = ["D"]
@ -2385,7 +2385,7 @@ pub struct PydocstyleOptions {
/// # documentation for every function parameter. /// # documentation for every function parameter.
/// ignore = ["D417"] /// ignore = ["D417"]
/// ///
/// [tool.ruff.pydocstyle] /// [tool.ruff.lint.pydocstyle]
/// convention = "google" /// convention = "google"
/// ``` /// ```
/// ///

View file

@ -161,7 +161,7 @@ mod tests {
use ruff_linter::line_width::LineLength; use ruff_linter::line_width::LineLength;
use ruff_linter::settings::types::PatternPrefixPair; use ruff_linter::settings::types::PatternPrefixPair;
use crate::options::{LintCommonOptions, Options}; use crate::options::{LintCommonOptions, LintOptions, Options};
use crate::pyproject::{find_settings_toml, parse_pyproject_toml, Pyproject, Tools}; use crate::pyproject::{find_settings_toml, parse_pyproject_toml, Pyproject, Tools};
use crate::tests::test_resource_path; use crate::tests::test_resource_path;
@ -228,7 +228,7 @@ exclude = ["foo.py"]
let pyproject: Pyproject = toml::from_str( let pyproject: Pyproject = toml::from_str(
r#" r#"
[tool.black] [tool.black]
[tool.ruff] [tool.ruff.lint]
select = ["E501"] select = ["E501"]
"#, "#,
)?; )?;
@ -236,10 +236,13 @@ select = ["E501"]
pyproject.tool, pyproject.tool,
Some(Tools { Some(Tools {
ruff: Some(Options { ruff: Some(Options {
lint_top_level: LintCommonOptions { lint: Some(LintOptions {
select: Some(vec![codes::Pycodestyle::E501.into()]), common: LintCommonOptions {
..LintCommonOptions::default() select: Some(vec![codes::Pycodestyle::E501.into()]),
}, ..LintCommonOptions::default()
},
..LintOptions::default()
}),
..Options::default() ..Options::default()
}) })
}) })
@ -248,7 +251,7 @@ select = ["E501"]
let pyproject: Pyproject = toml::from_str( let pyproject: Pyproject = toml::from_str(
r#" r#"
[tool.black] [tool.black]
[tool.ruff] [tool.ruff.lint]
extend-select = ["RUF100"] extend-select = ["RUF100"]
ignore = ["E501"] ignore = ["E501"]
"#, "#,
@ -257,11 +260,14 @@ ignore = ["E501"]
pyproject.tool, pyproject.tool,
Some(Tools { Some(Tools {
ruff: Some(Options { ruff: Some(Options {
lint_top_level: LintCommonOptions { lint: Some(LintOptions {
extend_select: Some(vec![codes::Ruff::_100.into()]), common: LintCommonOptions {
ignore: Some(vec![codes::Pycodestyle::E501.into()]), extend_select: Some(vec![codes::Ruff::_100.into()]),
..LintCommonOptions::default() ignore: Some(vec![codes::Pycodestyle::E501.into()]),
}, ..LintCommonOptions::default()
},
..LintOptions::default()
}),
..Options::default() ..Options::default()
}) })
}) })
@ -279,7 +285,7 @@ line_length = 79
assert!(toml::from_str::<Pyproject>( assert!(toml::from_str::<Pyproject>(
r#" r#"
[tool.black] [tool.black]
[tool.ruff] [tool.ruff.lint]
select = ["E123"] select = ["E123"]
"#, "#,
) )
@ -315,13 +321,16 @@ other-attribute = 1
"with_excluded_file/other_excluded_file.py".to_string(), "with_excluded_file/other_excluded_file.py".to_string(),
]), ]),
lint_top_level: LintCommonOptions { lint: Some(LintOptions {
per_file_ignores: Some(FxHashMap::from_iter([( common: LintCommonOptions {
"__init__.py".to_string(), per_file_ignores: Some(FxHashMap::from_iter([(
vec![codes::Pyflakes::_401.into()] "__init__.py".to_string(),
)])), vec![codes::Pyflakes::_401.into()]
..LintCommonOptions::default() )])),
}, ..LintCommonOptions::default()
},
..LintOptions::default()
}),
..Options::default() ..Options::default()
} }
); );

View file

@ -261,7 +261,7 @@ them. You can find the supported settings in the [API reference](settings.md#iso
For example, you can set `known-first-party` like so: For example, you can set `known-first-party` like so:
```toml ```toml
[tool.ruff] [tool.ruff.lint]
select = [ select = [
# Pyflakes # Pyflakes
"F", "F",
@ -275,7 +275,7 @@ select = [
# Note: Ruff supports a top-level `src` option in lieu of isort's `src_paths` setting. # Note: Ruff supports a top-level `src` option in lieu of isort's `src_paths` setting.
src = ["src", "tests"] src = ["src", "tests"]
[tool.ruff.isort] [tool.ruff.lint.isort]
known-first-party = ["my_module1", "my_module2"] known-first-party = ["my_module1", "my_module2"]
``` ```
@ -375,7 +375,7 @@ Found 3 errors.
Yes! To enforce a docstring convention, add the following to your `pyproject.toml`: Yes! To enforce a docstring convention, add the following to your `pyproject.toml`:
```toml ```toml
[tool.ruff.pydocstyle] [tool.ruff.lint.pydocstyle]
convention = "google" # Accepts: "google", "numpy", or "pep257". convention = "google" # Accepts: "google", "numpy", or "pep257".
``` ```
@ -387,12 +387,12 @@ Alongside `convention`, you'll want to explicitly enable the `D` rule code prefi
rules are not enabled by default: rules are not enabled by default:
```toml ```toml
[tool.ruff] [tool.ruff.lint]
select = [ select = [
"D", "D",
] ]
[tool.ruff.pydocstyle] [tool.ruff.lint.pydocstyle]
convention = "google" convention = "google"
``` ```
@ -423,7 +423,7 @@ For example, given this `pyproject.toml`:
[tool.ruff] [tool.ruff]
line-length = 88 line-length = 88
[tool.ruff.pydocstyle] [tool.ruff.lint.pydocstyle]
convention = "google" convention = "google"
``` ```

View file

@ -32,28 +32,28 @@ hypothetical rule, `HYP001`. If `HYP001` were in preview, it would _not_ be enab
`pyproject.toml`: `pyproject.toml`:
```toml ```toml
[tool.ruff] [tool.ruff.lint]
extend-select = ["HYP001"] extend-select = ["HYP001"]
``` ```
It also would _not_ be enabled by selecting the `HYP` category, like so: It also would _not_ be enabled by selecting the `HYP` category, like so:
```toml ```toml
[tool.ruff] [tool.ruff.lint]
extend-select = ["HYP"] extend-select = ["HYP"]
``` ```
Similarly, it would _not_ be enabled via the `ALL` selector: Similarly, it would _not_ be enabled via the `ALL` selector:
```toml ```toml
[tool.ruff] [tool.ruff.lint]
select = ["ALL"] select = ["ALL"]
``` ```
However, it would be enabled in any of the above cases if you enabled preview in your configuration file: However, it would be enabled in any of the above cases if you enabled preview in your configuration file:
```toml ```toml
[tool.ruff] [tool.ruff.lint]
extend-select = ["HYP"] extend-select = ["HYP"]
preview = true preview = true
``` ```
@ -69,7 +69,7 @@ If you'd prefer to opt-in to each preview rule individually, you can toggle the
setting in your `pyproject.toml`: setting in your `pyproject.toml`:
```toml ```toml
[tool.ruff] [tool.ruff.lint]
preview = true preview = true
explicit-preview-rules = true explicit-preview-rules = true
``` ```

View file

@ -10,6 +10,6 @@ dependencies = ["unidiff==0.7.5"]
[project.scripts] [project.scripts]
ruff-ecosystem = "ruff_ecosystem.cli:entrypoint" ruff-ecosystem = "ruff_ecosystem.cli:entrypoint"
[tool.ruff] [tool.ruff.lint]
extend-select = ["I"] extend-select = ["I"]
preview = true preview = true

2
ruff.schema.json generated
View file

@ -2241,7 +2241,7 @@
"type": "object", "type": "object",
"properties": { "properties": {
"convention": { "convention": {
"description": "Whether to use Google-style or NumPy-style conventions or the [PEP 257](https://peps.python.org/pep-0257/) defaults when analyzing docstring sections.\n\nEnabling a convention will force-disable any rules that are not included in the specified convention. As such, the intended use is to enable a convention and then selectively disable any additional rules on top of it.\n\nFor example, to use Google-style conventions but avoid requiring documentation for every function parameter:\n\n```toml [tool.ruff] # Enable all `pydocstyle` rules, limiting to those that adhere to the # Google convention via `convention = \"google\"`, below. select = [\"D\"]\n\n# On top of the Google convention, disable `D417`, which requires # documentation for every function parameter. ignore = [\"D417\"]\n\n[tool.ruff.pydocstyle] convention = \"google\" ```\n\nAs conventions force-disable all rules not included in the convention, enabling _additional_ rules on top of a convention is currently unsupported.", "description": "Whether to use Google-style or NumPy-style conventions or the [PEP 257](https://peps.python.org/pep-0257/) defaults when analyzing docstring sections.\n\nEnabling a convention will force-disable any rules that are not included in the specified convention. As such, the intended use is to enable a convention and then selectively disable any additional rules on top of it.\n\nFor example, to use Google-style conventions but avoid requiring documentation for every function parameter:\n\n```toml [tool.ruff.lint] # Enable all `pydocstyle` rules, limiting to those that adhere to the # Google convention via `convention = \"google\"`, below. select = [\"D\"]\n\n# On top of the Google convention, disable `D417`, which requires # documentation for every function parameter. ignore = [\"D417\"]\n\n[tool.ruff.lint.pydocstyle] convention = \"google\" ```\n\nAs conventions force-disable all rules not included in the convention, enabling _additional_ rules on top of a convention is currently unsupported.",
"anyOf": [ "anyOf": [
{ {
"$ref": "#/definitions/Convention" "$ref": "#/definitions/Convention"

View file

@ -9,6 +9,8 @@ line-length = 88
[tool.ruff] [tool.ruff]
line-length = 88 line-length = 88
[tool.ruff.lint]
select = ["ALL"] select = ["ALL"]
ignore = [ ignore = [
"C901", # McCabe complexity "C901", # McCabe complexity
@ -22,5 +24,5 @@ ignore = [
"ANN401", "ANN401",
] ]
[tool.ruff.isort] [tool.ruff.lint.isort]
required-imports = ["from __future__ import annotations"] required-imports = ["from __future__ import annotations"]