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

@ -95,6 +95,7 @@ mod tests {
use crate::line_width::LineLength;
use crate::registry::Rule;
use crate::rules::pycodestyle;
use crate::settings::LinterSettings;
use super::check_physical_lines;
@ -114,7 +115,10 @@ mod tests {
&indexer,
&[],
&LinterSettings {
line_length,
pycodestyle: pycodestyle::settings::Settings {
max_line_length: line_length,
..pycodestyle::settings::Settings::default()
},
..LinterSettings::for_rule(Rule::LineTooLong)
},
)

View file

@ -139,7 +139,7 @@ pub(crate) fn multiple_with_statements(
content,
with_stmt.into(),
checker.locator(),
checker.settings.line_length,
checker.settings.pycodestyle.max_line_length,
checker.settings.tab_size,
)
}) {

View file

@ -128,7 +128,7 @@ pub(crate) fn nested_if_statements(
content,
(&nested_if).into(),
checker.locator(),
checker.settings.line_length,
checker.settings.pycodestyle.max_line_length,
checker.settings.tab_size,
)
}) {

View file

@ -195,7 +195,7 @@ pub(crate) fn if_else_block_instead_of_dict_get(checker: &mut Checker, stmt_if:
&contents,
stmt_if.into(),
checker.locator(),
checker.settings.line_length,
checker.settings.pycodestyle.max_line_length,
checker.settings.tab_size,
) {
return;

View file

@ -130,7 +130,7 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &mut Checker, stmt_if: &a
&contents,
stmt_if.into(),
checker.locator(),
checker.settings.line_length,
checker.settings.pycodestyle.max_line_length,
checker.settings.tab_size,
) {
return;

View file

@ -101,7 +101,7 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
&contents,
stmt.into(),
checker.locator(),
checker.settings.line_length,
checker.settings.pycodestyle.max_line_length,
checker.settings.tab_size,
) {
return;
@ -188,7 +188,7 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
.slice(TextRange::new(line_start, stmt.start())),
)
.add_str(&contents)
> checker.settings.line_length
> checker.settings.pycodestyle.max_line_length
{
return;
}

View file

@ -120,7 +120,7 @@ pub(crate) fn organize_imports(
block,
comments,
locator,
settings.line_length,
settings.pycodestyle.max_line_length,
LineWidthBuilder::new(settings.tab_size).add_str(indentation),
stylist,
&settings.src,

View file

@ -16,6 +16,7 @@ mod tests {
use crate::line_width::LineLength;
use crate::registry::Rule;
use crate::rules::pycodestyle;
use crate::settings::types::PreviewMode;
use crate::test::test_path;
use crate::{assert_messages, settings};
@ -229,7 +230,10 @@ mod tests {
Path::new("pycodestyle/E501_2.py"),
&settings::LinterSettings {
tab_size: NonZeroU8::new(tab_size).unwrap().into(),
line_length: LineLength::try_from(6).unwrap(),
pycodestyle: pycodestyle::settings::Settings {
max_line_length: LineLength::try_from(6).unwrap(),
..pycodestyle::settings::Settings::default()
},
..settings::LinterSettings::for_rule(Rule::LineTooLong)
},
)?;

View file

@ -47,6 +47,7 @@ use crate::settings::LinterSettings;
///
/// ## Options
/// - `line-length`
/// - `pycodestyle.max-line-length`
/// - `task-tags`
/// - `pycodestyle.ignore-overlong-task-comments`
///
@ -68,7 +69,7 @@ pub(crate) fn line_too_long(
indexer: &Indexer,
settings: &LinterSettings,
) -> Option<Diagnostic> {
let limit = settings.line_length;
let limit = settings.pycodestyle.max_line_length;
Overlong::try_from_line(
line,

View file

@ -6,6 +6,7 @@ use crate::line_width::LineLength;
#[derive(Debug, Default, CacheKey)]
pub struct Settings {
pub max_line_length: LineLength,
pub max_doc_length: Option<LineLength>,
pub ignore_overlong_task_comments: bool,
}

View file

@ -399,7 +399,7 @@ pub(crate) fn f_strings(
&contents,
template.into(),
checker.locator(),
checker.settings.line_length,
checker.settings.pycodestyle.max_line_length,
checker.settings.tab_size,
) {
return;

View file

@ -26,7 +26,7 @@ use crate::rules::{
use crate::settings::types::{FilePatternSet, PerFileIgnore, PythonVersion};
use crate::{codes, RuleSelector};
use super::line_width::{LineLength, TabSize};
use super::line_width::TabSize;
use self::rule_table::RuleTable;
use self::types::PreviewMode;
@ -56,7 +56,6 @@ pub struct LinterSettings {
pub dummy_variable_rgx: Regex,
pub external: FxHashSet<String>,
pub ignore_init_module_imports: bool,
pub line_length: LineLength,
pub logger_objects: Vec<String>,
pub namespace_packages: Vec<PathBuf>,
pub src: Vec<PathBuf>,
@ -147,7 +146,6 @@ impl LinterSettings {
external: HashSet::default(),
ignore_init_module_imports: false,
line_length: LineLength::default(),
logger_objects: vec![],
namespace_packages: vec![],