Skip to content

Use f-string type conversion !r in error messages to avoid single quotes #3983

@seisman

Description

@seisman

Currently, the error messages are in the following style:

>>> arg = "invalid"
>>> msg = f"Invalid value: '{arg}'."
>>> print(msg)
Invalid value: 'invalid'.

>>> arg = 10
>>> msg = f"Invalid value: '{arg}'."
>>> print(msg)
Invalid value: '10'.

>>> arg = None
>>> msg = f"Invalid value: '{arg}'."
>>> print(msg)
Invalid value: 'None'.

The cons are:

  • Extra single quotes in f-strings
  • Values are always enclosed in single quotes, regardless of the types

I propose to use type conversion !r in the new style:

>>> arg = "invalid"
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: 'invalid'.

>>> arg = 10
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: 10.

>>> arg = None
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: None.

>>> arg = [10, 20, 30]
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: [10, 20, 30].

👍 if you like new style; 👎 if you like the old style; or leave your comments.

Metadata

Metadata

Labels

maintenanceBoring but important stuff for the core devs

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions