Skip to content

gh-154610: Use repr() to format the attribute name in AttributeError - #154614

Open
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:attr_error_repr
Open

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:attr_error_repr

Conversation

@vstinner

@vstinner vstinner commented Jul 24, 2026 •

Copy link
Copy Markdown
Member
  • In Python code, replace %s with f-string {name!r}.
  • In C code, replace '%U' with %R.
  • In C code, no longer truncate long type names: replace %.50s or %.100s with %s (PEP 737).

@vstinner

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka @johnslavik

@johnslavik
johnslavik self-requested a review July 24, 2026 13:59

@johnslavik johnslavik left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I added suggestions that remove escaping. I think new strings are easier to read, but feel free to ignore them.

Comment thread Lib/test/test_module/__init__.py Resolved
Comment thread Lib/test/test_unittest/test_case.py Resolved
Comment thread Lib/test/test_builtin.py Resolved
Comment thread Lib/test/test_decimal.py Resolved
Comment thread Lib/test/test_builtin.py Resolved
@vstinner

Copy link
Copy Markdown
Member Author

Nit: I added suggestions that remove escaping. I think new strings are easier to read, but feel free to ignore them.

I wasn't sure about triple quote. Anyway, I just applied your suggestions.

@johnslavik johnslavik left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that was tricky :-) It was a syntax error. Single-quote triple will work. I'll fix it so you don't have to.

Comment thread Lib/test/test_module/__init__.py Resolved
Comment thread Lib/test/test_unittest/test_case.py Resolved
Comment thread Lib/test/test_builtin.py Resolved
Comment thread Lib/test/test_decimal.py Resolved
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 29, 2026
…Error

* In Python code, replace `%s` with f-string `{name!r}`.
* In C code, replace `'%U'` with `%R`.
* In C code, no longer truncate long type names:
  replace `%.50s` or `%.100s` with `%s` (PEP 737).
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 17, 2026
@vstinner vstinner removed the stale Stale PR or inactive for long period of time. label Nov 17, 2026
@vstinner

Copy link
Copy Markdown
Member Author

I rebased the PR on the main branch.

In September, I proposed adding new conversion formats (#157685), but they didn't convince anymore: see https://discuss.python.org/t/add-r-conversion-to-f-string-and-str-format-to-format-error-messages/109098/12. So I prefer to abandon my idea and move back to my initial plan to replacing str() with repr() in AttributeError error message: this PR.

@johnslavik: Sorry, I reverted changes to use triple-quote. I came back to my initial PR coding style.

@vstinner

Copy link
Copy Markdown
Member Author

@encukou @serhiy-storchaka: So are you ok with my plan to simply replace str() with repr() when formatting AttributeError error message? It should format properly attribute names which contain quotes (well, see the issue for the rationale).

@encukou

encukou commented Nov 18, 2026

Copy link
Copy Markdown
Member

I'm -0 on this. I don't think it helps human readers too much.
It helps when parsing information out of the message, but that's not something we should encourage -- error message formats change without warning, so parsing them is unreliable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants