Skip to content

Check for invalid format specifiers #644

@asmeurer

Description

@asmeurer

pyflakes does some format specifier checks (#443), but it isn't able to detect invalid format specifiers. For example

f"{x:f.1}"
"{x:f.1}".format(x=1)

are both wrong (the f is supposed to go after the .1). Also the f-string one is a ValueError and is only checked at runtime.

The format specification mini-language is pretty complicated, so this would be useful, but also maybe not easy to implement. For .format strings you can use https://docs.python.org/3/library/string.html#string.Formatter.parse, but for f-strings, there's nothing in the standard library that can tokenize the f-string components (it would be useful if there were). Libraries like pygments and parso can tokenize them, or maybe the tokenizer added in #443 can be extended to handle them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions