Skip to content

Commit

Permalink
New pycodestyle.max-line-width option
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Oct 24, 2023
1 parent 1c1aed4 commit 1e916cd
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 8 deletions.
9 changes: 7 additions & 2 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ruff_linter::settings::types::{
};
use ruff_linter::{RuleParser, RuleSelector, RuleSelectorParser};
use ruff_workspace::configuration::{Configuration, RuleSelection};
use ruff_workspace::options::PycodestyleOptions;
use ruff_workspace::resolver::ConfigurationTransformer;

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -685,8 +686,12 @@ impl ConfigurationTransformer for CliOverrides {
if let Some(force_exclude) = &self.force_exclude {
config.force_exclude = Some(*force_exclude);
}
if let Some(line_length) = &self.line_length {
config.line_length = Some(*line_length);
if let Some(line_length) = self.line_length {
config.line_length = Some(line_length);
config.lint.pycodestyle = Some(PycodestyleOptions {
max_line_width: Some(line_length),
..config.lint.pycodestyle.unwrap_or_default()
});
}
if let Some(preview) = &self.preview {
config.preview = Some(*preview);
Expand Down
38 changes: 38 additions & 0 deletions crates/ruff_cli/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,41 @@ if __name__ == "__main__":
"###);
Ok(())
}

#[test]
fn line_too_long_width_override() -> Result<()> {
let tempdir = TempDir::new()?;
let ruff_toml = tempdir.path().join("ruff.toml");
fs::write(
&ruff_toml,
r#"
line-width = 80
select = ["E501"]
[pycodestyle]
max-line-width = 100
"#,
)?;

assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.arg("--config")
.arg(&ruff_toml)
.args(["--stdin-filename", "test.py"])
.arg("-")
.pass_stdin(r#"
# wider than 80, but less than 100
_ = "---------------------------------------------------------------------------亜亜亜亜亜亜"
# wider than 100
_ = "---------------------------------------------------------------------------亜亜亜亜亜亜亜亜亜亜亜亜亜亜"
"#), @r###"
success: false
exit_code: 1
----- stdout -----
test.py:5:91: E501 Line too long (109 > 100 width)
Found 1 error.
----- stderr -----
"###);
Ok(())
}
6 changes: 5 additions & 1 deletion crates/ruff_linter/src/checkers/physical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
},
)
Expand Down
6 changes: 5 additions & 1 deletion crates/ruff_linter/src/rules/pycodestyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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_width: LineLength::try_from(6).unwrap(),
..pycodestyle::settings::Settings::default()
},
..settings::LinterSettings::for_rule(Rule::LineTooLong)
},
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub(crate) fn line_too_long(
indexer: &Indexer,
settings: &LinterSettings,
) -> Option<Diagnostic> {
let limit = settings.line_length;
let limit = settings.pycodestyle.max_line_width;

Overlong::try_from_line(
line,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pycodestyle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
6 changes: 4 additions & 2 deletions crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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
Expand Down Expand Up @@ -225,7 +227,7 @@ 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(),
line_length,
tab_size: self.tab_size.unwrap_or_default(),
namespace_packages: self.namespace_packages.unwrap_or_default(),
per_file_ignores: resolve_per_file_ignores(
Expand Down Expand Up @@ -348,7 +350,7 @@ impl Configuration {
.unwrap_or_default(),
pycodestyle: lint
.pycodestyle
.map(PycodestyleOptions::into_settings)
.map(|options| options.into_settings(line_length))
.unwrap_or_default(),
pydocstyle: lint
.pydocstyle
Expand Down
28 changes: 27 additions & 1 deletion crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,31 @@ impl Pep8NamingOptions {
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct PycodestyleOptions {
/// The maximum [width](http://www.unicode.org/reports/tr11/#Overview) to allow for [`line-too-long`] violations. By default,
/// this is set to the value of the global `line-width` option.
///
/// Overriding the global [`line-width`] allows to use a lower limit for Ruff's formatter and warn about
/// extra-long lines that can't be split by the formatter using [`line-too-long`]:
///
/// ```toml
/// line-width = 88 # The formatter breaks line's with a width of 88.
///
/// [pycodestyle]
/// max-line-width = 100 # E501 reports lines that exceed the width of 100.
/// ```
///
/// [`line-too-long`].
///
/// 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-width = 100
"#
)]
pub max_line_width: Option<LineWidth>,

/// 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.
Expand Down Expand Up @@ -2278,9 +2303,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_width: self.max_line_length.unwrap_or(global_line_length),
ignore_overlong_task_comments: self.ignore_overlong_task_comments.unwrap_or_default(),
}
}
Expand Down
11 changes: 11 additions & 0 deletions ruff.schema.json

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

0 comments on commit 1e916cd

Please sign in to comment.