-
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
Allow additional text following formatter pragma comments #8876
Conversation
cfbb17c
to
6c8017e
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable. Can you double check if we the new implementation is flexible enough to cover Black's preview style #8892 (I don't see why we should gate this behind a preview flag)
_ => None, | ||
|
||
// Allow additional context separated by whitespace | ||
command => match command.split_whitespace().next() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just replace match command.trim_whitespace_start()
with match command.trim_whitespace_start(). split_whitespace().next()
? That is, could we just always split by whitespace and take the first token, rather than nesting the match expressions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's how I implemented it at first but then I was wondering if we should only allow comments on fmt: off
and fmt: skip
but not fmt: on
which led me to this structure. I decided it'd be weird not to support fmt: on - reason
though and I can see if being useful. I left it this way because I figured there may be a performance benefit and it's still easy to read. I'm happy to go back though.
While I agree we should allow pragma comments to follow other comments e.g. Additionally, the syntax proposed here is actually different than Black's preview style. Black does not allow a reason unless it is separated by a # valid
foo = 1 # fmt: skip; reason
foo = 1 # noqa; fmt: skip; reason
foo = 1 # fmt: skip; noqa
foo = 1 # fmt: skip # reason
foo = 1 # fmt: skip # reason # noqa
foo = 1 # reason # fmt: skip
# invalid
foo = 1 # fmt: skip - reason; noqa
foo = 1 # fmt: skip - reason
foo = 1 # fmt: skip reason I'd be okay with requiring a
Agreed, unless we are unsure it is the right syntax to support. |
In light of #8892 I think the goal here should be to determine if we want to allow trailing text after pragma directives. I think this question can be considered separately from multiple pragma directives e.g. foo = 1 # noqa - some reason; fmt: skip - other reason This seems like a valid use-case? |
I think I like this overall. It might be nice to start by sticking to Black's syntax here. It does look more conservative right? In that we could expand it something more liberal like you have here later?
My understanding here is that if this PR is merged as-is, then a comment like |
I think it's correct to allow arbitrary contents after the |
## Summary This is similar to #8876, but more limited in scope: 1. It only applies to `# fmt: skip` (like Black). Like `# isort: on`, `# fmt: on` needs to be on its own line (still). 2. It only delimits on `#`, so you can do `# fmt: skip # noqa`, but not `# fmt: skip - some other content` or `# fmt: skip; noqa`. If we want to support the `;`-delimited version, we should revisit later, since we don't support that in the linter (so `# fmt: skip; noqa` wouldn't register a `noqa`). Closes #8892.
I'm not sure if we should do this, but I was curious what it would look like to implement.
Closes #8874