Skip to content

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

Description

@vstinner

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 #153786 (comment) discussion (another AttributeError change).

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions