-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exclude pragma comments from measured line width #6772
Comments
type:
, pyright:
pylint:
, noqa
, but NOT nocoverage
because it is a trailing clause comment that isn't sensitive about its positioning
Curious of what a solution would look like. These are line suffixes, so would we want to just match on the content that'd we want to ignore? Or is there a more robust way to go about this? |
No, matching on the content is what we'll need to do here. Similar to how formatter suppression comments: ruff/crates/ruff_python_formatter/src/comments/mod.rs Lines 173 to 193 in 0cea497
We'll match on the content and set reserved width to 0 if it is a pragma comment. |
IIUC there would be no exceptions based on the values of the pragmas. So something like this would probably work, right? // Don't reserve width for excluded pragma comments.
let reserved_width = if ["noqa", "type:", "pylint", "pyright:"]
.iter()
.any(|prefix| trimmed.starts_with(prefix))
{
0
} else {
normalized_comment.text_len().to_u32() + 2 // Account for two added spaces
}; Might need some redundant trims to get This is where my mind is at with this currently. |
That looks about right. I don't think we need to handle non-breaking spaces. I don't even think most pragma comments remain valid if you add a non breaking space. |
Interesting. I didn't think that a NBSP would invalidate pragmas. I just tested it with "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # noqa
a = [] # type: list[str] Both contain a leading NBSP.
Is the reasoning just because they become invalid pragmas? I'd think the intention is that they're there to be treated as pragmas. I'd think from a consistency point of view it might make sense to treat their formatting the same. I'm imagining a case where a user realizes their pragmas are invalid due to NBPS and corrects them. They might need to address both their pragmas' reinstated functionality and now potential formatting changes. |
What was the result. Did the typechecker pick up the type or not?
I can see how this is annoying. But I'm not too worried because I don't know how you accidentally end up with a NBPS. That's why I think this is rare. |
It did not pick up the type. You called it. # Lead by a NBSP
a = [] # type: list[str]
# Lead by a space
b = [] # type: list[str]
If you throw the NBSP into
Yea this is what I've been trying to understand. In all transparency I wasn't too familiar with NBSP prior to working on this stuff. So I'd have to look into them more to gauge it. |
Me neither. I only used |
So I've looked into this more. Tools like My preliminary thoughts are that I actually prefer A NBSP is meant to attach two elements together always and not break the content. So while this might be rare, it's not unreasonable (IMO). Here is the relevant Discord thread. Here is a playground script I've been messing with to better understand NBSP in the context of I'll defer to your judgement, but if we decide to move forward with invalidating them then maybe we need to consider updating diff --git a/crates/ruff/src/noqa.rs b/crates/ruff/src/noqa.rs
index 36cf5105a..62801801c 100644
--- a/crates/ruff/src/noqa.rs
+++ b/crates/ruff/src/noqa.rs
@@ -11,7 +11,7 @@ use log::warn;
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_diagnostics::Diagnostic;
-use ruff_python_trivia::indentation_at_offset;
+use ruff_python_trivia::{indentation_at_offset, PythonWhitespace};
use ruff_source_file::{LineEnding, Locator};
use crate::codes::NoqaCode;
@@ -53,7 +53,7 @@ impl<'a> Directive<'a> {
let mut comment_start = noqa_literal_start;
// Trim any whitespace between the `#` character and the `noqa` literal.
- comment_start = text[..comment_start].trim_end().len();
+ comment_start = text[..comment_start].trim_whitespace_end().len();
// The next character has to be the `#` character.
if text[..comment_start] |
FWIW I constantly type unintentional NBSP. On UK mac Keyboard, Alt-3 is #, and Alt-Space is NBSP. If I'm typing fast sometimes these can blur together (hitting space before releasing the alt). However, I always catch them now, because I set up my IDE to highlight NBSP. I'm not sure how often other people encounter or notice this problem, though. |
Exclude trailing expression end-of-line comments to be included in the measured line width (
type:
,pyright:
pylint:
,noqa
, but NOTnocoverage
because it is a trailing clause comment that isn't sensitive about its positioningThe text was updated successfully, but these errors were encountered: