Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Nov 28, 2023

I'm not sure if we should do this, but I was curious what it would look like to implement.

Closes #8874

@zanieb zanieb force-pushed the zb/fmt-skip-reason branch from cfbb17c to 6c8017e Compare November 28, 2023 19:32
Copy link
Contributor

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@zanieb zanieb changed the title Allow additional text to folllow formatter pragma comments Allow additional text to follow formatter pragma comments Nov 28, 2023
@zanieb zanieb changed the title Allow additional text to follow formatter pragma comments Allow additional text following formatter pragma comments Nov 28, 2023
@zanieb zanieb requested a review from MichaReiser November 28, 2023 20:14
Copy link
Member

@MichaReiser MichaReiser left a 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() {
Copy link
Member

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?

Copy link
Member Author

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.

@zanieb
Copy link
Member Author

zanieb commented Nov 29, 2023

Can you double check if we the new implementation is flexible enough to cover Black's preview style #8892

While I agree we should allow pragma comments to follow other comments e.g. # foo # fmt: skip or #foo; fmt: skip, I don't think this implementation will cover those cases and we'll need to do something a little more involved. Additionally, I don't think the semicolon separated format is supported by our linter pragmas e.g. # foo; noqa and we should probably implement support for that in both at once?

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 ; or # :

# 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 ; separator, I guess. Perhaps I should close this and we can focus on the Black syntax instead?

I don't see why we should gate this behind a preview flag

Agreed, unless we are unsure it is the right syntax to support.

@zanieb
Copy link
Member Author

zanieb commented Nov 29, 2023

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?

@BurntSushi
Copy link
Member

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?

I think this question can be considered separately from multiple pragma directives e.g.

My understanding here is that if this PR is merged as-is, then a comment like # noqa - some reason; fmt: skip - other reason would today be allowed and be seen as just a noqa directive, right? But if you wanted to support that second fmt: skip directive, it would potentially change the meaning of what was once just free-form text to a pragma directive. I think a simple restriction on what's allowed in this PR would mitigate that concern? (If it's worth mitigating at all. It could be a non-issue.)

@charliermarsh
Copy link
Member

I think it's correct to allow arbitrary contents after the # fmt: on etc. It's also consistent with how we treat # noqa in the linter.

@zanieb
Copy link
Member Author

zanieb commented Dec 8, 2023

I think we can close this pull request entirely, as the implementation of #8892 will make whatever happens here irrelevant.

It sounds like there's rough consensus to allow trailing text. We can either do that when implementing #8892 or after, depending on if it's more or less work :)

@zanieb zanieb closed this Dec 14, 2023
charliermarsh added a commit that referenced this pull request Jan 5, 2024
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

# fmt: skip should have comments after the skip keyword
4 participants