-
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
Spurious PLE1300 error with 0.0.283 #6442
Comments
This is probably related to #6171 |
I believe this occurred with the 0.0.282 -> 0.0.283 update, at least that's what caused it for me, not 0.0.281 → 0.0.282. |
Perhaps I don't understand how pre-commit checks work. Our - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.282
hooks:
- id: ruff |
But yes, you're right about it, it's specific to 0.0.283:
I'm not a fan or pre-commit, it keeps annoying me, whether in CI jobs or on my own computer. |
ruff v0.0.283 is getting picked up by an unpinned ruff dev dependency running in a separate CI job. pre-commit is running v0.0.282 as specified. |
Thanks! I'll take a look at the format string parser. I wonder if we were silently parsing this incorrectly the whole time, and the rule just brought it to light, or if the changes to the parser from #6171 were erroneous. |
I think the parser doesn't quite support this, it'll need to be improved. (The rule was new in v0.0.283.) |
Closes #6442 Python string formatting like `"hello {place}".format(place="world")` supports format specifications for replaced content such as `"hello {place:>10}".format(place="world")` which will align the text to the right in a container filled up to ten characters. Ruff parses formatted strings into `FormatPart`s each of which is either a `Field` (content in `{...}`) or a `Literal` (the normal content). Fields are parsed into name and format specifier sections (we'll ignore conversion specifiers for now). There are a myriad of specifiers that can be used in a `FormatSpec`. Unfortunately for linters, the specifier values can be dynamically set. For example, `"hello {place:{align}{width}}".format(place="world", align=">", width=10)` and `"hello {place:{fmt}}".format(place="world", fmt=">10")` will yield the same string as before but variables can be used to determine the formatting. In this case, when parsing the format specifier we can't know what _kind_ of specifier is being used as their meaning is determined by both position and value. Ruff does not support nested replacements and our current data model does not support the concept. Here the data model is updated to support this concept, although linting of specifications with replacements will be inherently limited. We could split format specifications into two types, one without any replacements that we can perform lints with and one with replacements that we cannot inspect. However, it seems excessive to drop all parsing of format specifiers due to the presence of a replacement. Instead, I've opted to parse replacements eagerly and ignore their possible effect on other format specifiers. This will allow us to retain a simple interface for `FormatSpec` and most syntax checks. We may need to add some handling to relax errors if a replacement was seen previously. It's worth noting that the nested replacement _can_ also include a format specification although it may fail at runtime if you produce an invalid outer format specification. For example, `"hello {place:{fmt:<2}}".format(place="world", fmt=">10")` is valid so we need to represent each nested replacement as a full `FormatPart`. ## Test plan Adding unit tests for `FormatSpec` parsing and snapshots for PLE1300
This is still broken unfortunately. import sys
number = 1234.56
print("{0:.{prec}g}".format(number, prec=sys.float_info.dig)) Ruff output:
Ruff version:
There is a full example here that triggers two erroneous errors. |
@tdulcet Perhaps you should open a new issue, since this one has been automatically closed. |
I have opened #6767 as a follow-up to this ticket. |
After upgrading v0.0.281 → v0.0.282 (codespell-project/codespell#3011)we see what we think is a spurious PLE1300 error:pyproject.toml
fileThe Format String Syntax documentation states:
The text was updated successfully, but these errors were encountered: