From 81bb7a97f45e44dee51f1264225dd58eee1981d8 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 19 Mar 2024 14:35:53 -0400 Subject: [PATCH] Allow SPDX license headers to exceed the line length --- .../resources/test/fixtures/pycodestyle/E501.py | 5 +++++ crates/ruff_linter/src/rules/pycodestyle/overlong.rs | 11 ++++++++++- .../src/rules/pycodestyle/rules/line_too_long.rs | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E501.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E501.py index 6738ad51d55a2..7c3dc583422f9 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E501.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E501.py @@ -82,3 +82,8 @@ class Bar: """ This is a long sentence that ends with a shortened URL and, therefore, could easily be broken across multiple lines ([source](https://ruff.rs)) """ + + +# OK +# SPDX-FileCopyrightText: Copyright 2012-2015 Charlie Marsh +# SPDX-License-Identifier: a very long license identifier that exceeds the line length limit diff --git a/crates/ruff_linter/src/rules/pycodestyle/overlong.rs b/crates/ruff_linter/src/rules/pycodestyle/overlong.rs index 950bafa0554a5..cb1988746c283 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/overlong.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/overlong.rs @@ -53,7 +53,7 @@ impl Overlong { }; let mut chunks = line.split_whitespace(); - let (Some(_), Some(second_chunk)) = (chunks.next(), chunks.next()) else { + let (Some(first_chunk), Some(second_chunk)) = (chunks.next(), chunks.next()) else { // Single word / no printable chars - no way to make the line shorter. return None; }; @@ -67,6 +67,15 @@ impl Overlong { } } + // Do not enforce the line length limit for SPDX license headers, which are machine-readable + // and explicitly _not_ recommended to wrap over multiple lines. + if matches!( + (first_chunk, second_chunk), + ("#", "SPDX-License-Identifier:" | "SPDX-FileCopyrightText:") + ) { + return None; + } + // Obtain the start offset of the part of the line that exceeds the limit. let mut start_offset = line.start(); let mut start_width = LineWidthBuilder::new(tab_size); diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/line_too_long.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/line_too_long.rs index 1d197516f074a..a722344fa050b 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/line_too_long.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/line_too_long.rs @@ -27,6 +27,9 @@ use crate::settings::LinterSettings; /// line-length threshold. That is, a line will not be flagged as /// overlong if a pragma comment _causes_ it to exceed the line length. /// (This behavior aligns with that of the Ruff formatter.) +/// 4. Ignores SPDX license identifiers and copyright notices +/// (e.g., `# SPDX-License-Identifier: MIT`), which are machine-readable +/// and should _not_ wrap over multiple lines. /// /// If [`lint.pycodestyle.ignore-overlong-task-comments`] is `true`, this rule will /// also ignore comments that start with any of the specified [`lint.task-tags`]