python / python/cpython

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

Open
#154,610 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the getattr(object(), "a'b") example and inspect the AttributeError formatting paths discussed in the linked PRs. Confirm that the completed behavior escapes or changes quoting for the attribute name, matching the expected error message shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.