New pycodestyle.max-line-length option (#8039)

## Summary

This PR introduces a new `pycodestyl.max-line-length` option that allows overriding the global `line-length` option for `E501` only.

This is useful when using the formatter and `E501` together, where the formatter uses a lower limit and `E501` is only used to catch extra-long lines. 

Closes #7644

## Considerations

~~Our fix infrastructure asserts in some places that the fix doesn't exceed the configured `line-width`. With this change, the question is whether it should use the `pycodestyle.max-line-width` or `line-width` option to make that decision.
I opted for the global `line-width` for now, considering that it should be the lower limit. However, this constraint isn't enforced and users not using the formatter may only specify `pycodestyle.max-line-width` because they're unaware of the global option (and it solves their need).~~


~~I'm interested to hear your thoughts on whether we should use `pycodestyle.max-line-width` or `line-width` to decide on whether to emit a fix or not.~~

Edit: The linter users `pycodestyle.max-line-width`. The `line-width` option has been removed from the `LinterSettings`

## Test Plan

Added integration test. Built the documentation and verified that the links are correct.
This commit is contained in:
Micha Reiser 2023-10-24 17:14:05 +09:00 committed by GitHub
parent 2587aef1ea
commit 9feb86caa4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 125 additions and 28 deletions

View file

@ -21,6 +21,7 @@ use ruff_linter::line_width::{LineLength, TabSize};
use ruff_linter::registry::RuleNamespace;
use ruff_linter::registry::{Rule, RuleSet, INCOMPATIBLE_CODES};
use ruff_linter::rule_selector::{PreviewOptions, Specificity};
use ruff_linter::rules::pycodestyle;
use ruff_linter::settings::rule_table::RuleTable;
use ruff_linter::settings::types::{
FilePattern, FilePatternSet, PerFileIgnore, PreviewMode, PythonVersion, SerializationFormat,
@ -183,6 +184,8 @@ impl Configuration {
let lint = self.lint;
let lint_preview = lint.preview.unwrap_or(global_preview);
let line_length = self.line_length.unwrap_or_default();
Ok(Settings {
cache_dir: self
.cache_dir
@ -225,7 +228,6 @@ impl Configuration {
.unwrap_or_else(|| DUMMY_VARIABLE_RGX.clone()),
external: FxHashSet::from_iter(lint.external.unwrap_or_default()),
ignore_init_module_imports: lint.ignore_init_module_imports.unwrap_or_default(),
line_length: self.line_length.unwrap_or_default(),
tab_size: self.tab_size.unwrap_or_default(),
namespace_packages: self.namespace_packages.unwrap_or_default(),
per_file_ignores: resolve_per_file_ignores(
@ -346,10 +348,14 @@ impl Configuration {
.map(Pep8NamingOptions::try_into_settings)
.transpose()?
.unwrap_or_default(),
pycodestyle: lint
.pycodestyle
.map(PycodestyleOptions::into_settings)
.unwrap_or_default(),
pycodestyle: if let Some(pycodestyle) = lint.pycodestyle {
pycodestyle.into_settings(line_length)
} else {
pycodestyle::settings::Settings {
max_line_length: line_length,
..pycodestyle::settings::Settings::default()
}
},
pydocstyle: lint
.pydocstyle
.map(PydocstyleOptions::into_settings)

View file

@ -60,7 +60,7 @@ pub struct Options {
/// this base configuration file, then merge in any properties defined
/// in the current configuration file.
#[option(
default = r#"None"#,
default = r#"null"#,
value_type = "str",
example = r#"
# Extend the `pyproject.toml` file in the parent directory.
@ -132,7 +132,7 @@ pub struct Options {
/// results across many environments, e.g., with a `pyproject.toml`
/// file).
#[option(
default = "None",
default = "null",
value_type = "str",
example = r#"
required-version = "0.0.193"
@ -362,6 +362,8 @@ pub struct Options {
/// Note: While the formatter will attempt to format lines such that they remain
/// within the `line-length`, it isn't a hard upper bound, and formatted lines may
/// exceed the `line-length`.
///
/// See [`pycodestyle.max-line-length`](#pycodestyle-max-line-length) to configure different lengths for `E501` and the formatter.
#[option(
default = "88",
value_type = "int",
@ -1043,7 +1045,7 @@ pub struct Flake8CopyrightOptions {
/// Author to enforce within the copyright notice. If provided, the
/// author must be present immediately following the copyright notice.
#[option(default = "None", value_type = "str", example = r#"author = "Ruff""#)]
#[option(default = "null", value_type = "str", example = r#"author = "Ruff""#)]
pub author: Option<String>,
/// A minimum file size (in bytes) required for a copyright notice to
@ -2247,6 +2249,32 @@ impl Pep8NamingOptions {
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct PycodestyleOptions {
/// The maximum line length to allow for [`line-too-long`](https://docs.astral.sh/ruff/rules/line-too-long/) violations. By default,
/// this is set to the value of the [`line-length`](#line-length) option.
///
/// Use this option when you want to detect extra-long lines that the formatter can't automatically split by setting
/// `pycodestyle.line-length` to a value larger than [`line-length`](#line-length).
///
/// ```toml
/// line-length = 88 # The formatter wraps lines at a length of 88
///
/// [pycodestyle]
/// max-line-length = 100 # E501 reports lines that exceed the length of 100.
/// ```
///
/// The length is determined by the number of characters per line, except for lines containing East Asian characters or emojis.
/// For these lines, the [unicode width](https://unicode.org/reports/tr11/) of each character is added up to determine the length.
///
/// See the [`line-too-long`](https://docs.astral.sh/ruff/rules/line-too-long/) rule for more information.
#[option(
default = "null",
value_type = "int",
example = r#"
max-line-length = 100
"#
)]
pub max_line_length: Option<LineLength>,
/// The maximum line length to allow for [`doc-line-too-long`](https://docs.astral.sh/ruff/rules/doc-line-too-long/) violations within
/// documentation (`W505`), including standalone comments. By default,
/// this is set to null which disables reporting violations.
@ -2256,7 +2284,7 @@ pub struct PycodestyleOptions {
///
/// See the [`doc-line-too-long`](https://docs.astral.sh/ruff/rules/doc-line-too-long/) rule for more information.
#[option(
default = "None",
default = "null",
value_type = "int",
example = r#"
max-doc-length = 88
@ -2278,9 +2306,10 @@ pub struct PycodestyleOptions {
}
impl PycodestyleOptions {
pub fn into_settings(self) -> pycodestyle::settings::Settings {
pub fn into_settings(self, global_line_length: LineLength) -> pycodestyle::settings::Settings {
pycodestyle::settings::Settings {
max_doc_length: self.max_doc_length,
max_line_length: self.max_line_length.unwrap_or(global_line_length),
ignore_overlong_task_comments: self.ignore_overlong_task_comments.unwrap_or_default(),
}
}
@ -2321,7 +2350,7 @@ pub struct PydocstyleOptions {
/// enabling _additional_ rules on top of a convention is currently
/// unsupported.
#[option(
default = r#"None"#,
default = r#"null"#,
value_type = r#""google" | "numpy" | "pep257""#,
example = r#"
# Use Google-style docstrings.