#154610·Python

Format attribute name with repr() when formatting AttributeError error message

Author: vstinnerCreated Jul 24, 2026Updated Sep 17, 2026
Labelstype-featureinterpreter-corestdlib

Feature or enhancement

Some code paths format the attribute name with str() to format the AttributeError error message. Example:

>>> getattr(object(), "a'b")
AttributeError: 'object' object has no attribute 'a'b'

The quote character ' should be escaped in the error message ('a\'b'), or the attribute name should be quoted with " character ("a'b").

The expected error is:

AttributeError: 'object' object has no attribute "a'b"

I propose replacing str() with repr() to format the attribute name.

This issue is a follow-up of https://github.com/python/cpython/pull/153786#discussion_r3639448620 discussion (another AttributeError change).

Linked PRs

  • gh-154614
  • gh-157685