Skip to content

Suggest !r format specifier for strings like f"foo is '{self.foo}'" #319

@Zac-HD

Description

@Zac-HD

I've seen some code lately where a reminder about the !r format specifier would probably have helped - instead of displaying the repr, single-quotes were manually added outside the braces as in f"No value found at '{path}'". This OK, until path contains a single-quote character and then it can get confusing fast.

I'd therefore propose warning about this case, which is conveniently easy to reliably detect using the JoinedStr and FormattedValue nodes - within a JoinedStr, warn if there is a sequence of Constant/FormattedValue/Constant, where the first constant ends and the second starts with a single or double quote character, and the formatter value has no conversion code.

Separately, I used the regex ".*('\{[^}]+\}').*" to confirm that this is common enough to be worth linting for. It's not especially rare, and IMO pretty useful to teach people about format specifiers.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions