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

Implement F522-F525 #899

Merged
merged 13 commits into from Nov 25, 2022
Merged

Implement F522-F525 #899

merged 13 commits into from Nov 25, 2022

Conversation

olliemath
Copy link
Contributor

@olliemath olliemath commented Nov 25, 2022

Adds checks for F522 through F525. Closes #891

All of the new checks return None if the format string is invalid (i.e. if F521 fails) - essentially I'm treating it like a syntax error and not processing further. I don't know if there's a neater way to do this, which works even if F521 is disabled.

Most of the logic here is in the helper function FormatSummary::try_from, which extracts all the keywords and indexes from the format string. Taking into account nested format strings like "{spam:{eggs}}" is what makes it a bit involved, so I've added some tests.

The use of HashSet in the summary keeps everything O(n) - for most typical python code, I'd speculate that a Vec would actually be quicker, however I can imagine somebody somewhere has a format function with 1000s of arguments for their own personal reasons and I wouldn't want that to come back to bite us. Use of HashSet means that for F524 the missing placeholders may not be returned in the order they appear in the string literal - I don't know if that's enough of a big deal to warrant implementing something more complicated.

src/pyflakes/checks.rs Outdated Show resolved Hide resolved
src/pyflakes/checks.rs Outdated Show resolved Hide resolved
src/pyflakes/checks.rs Outdated Show resolved Hide resolved
src/pyflakes/checks.rs Outdated Show resolved Hide resolved
src/checks.rs Show resolved Hide resolved
@olliemath olliemath changed the title Implement F522-F525 Draft: Implement F522-F525 Nov 25, 2022
@olliemath
Copy link
Contributor Author

olliemath commented Nov 25, 2022

I've pulled out the construction of FormatSummary, so we build it at most once, and minimised the number of clones.

I can't see a way to avoid cloning when building the failure messages, but that should hopefully be rare.

@charliermarsh
Copy link
Member

This looks great! Thank you. (And yeah, we always clone when building checks and failure messages.)

@charliermarsh
Copy link
Member

I assume this is good to go? Anything else from your perspective @olliemath?

@olliemath olliemath changed the title Draft: Implement F522-F525 Implement F522-F525 Nov 25, 2022
@olliemath
Copy link
Contributor Author

I assume this is good to go? Anything else from your perspective @olliemath?

Yup should be good to merge assuming the pipeline passes.

@olliemath
Copy link
Contributor Author

By the way, I've been using interactive rebase to keep my commit history tidy - it looks like your general strategy is to squash+merge? In which case in future contributions I might abandon that approach and be a bit more liberal with my commits

@charliermarsh
Copy link
Member

@olliemath - Yeah, happy to elaborate but I tend towards squashing such that the Git history corresponds to "one commit per PR".

@charliermarsh charliermarsh merged commit 8b14f1b into astral-sh:main Nov 25, 2022
@charliermarsh
Copy link
Member

Awesome, thanks tons for this!

@olliemath
Copy link
Contributor Author

Happy to help - esp as I'm super excited about using ruff myself

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.

Implementing .format checks
2 participants