GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-43857: Improve the AttributeError message when deleting a missing attribute by geryogam · Pull Request #25424 · python/cpython

Conversation

@geryogam

@geryogam geryogam commented Apr 15, 2021 •
edited by JelleZijlstra
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

https://bugs.python.org/issue43857

#88023

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@geryogam
Fix the AttributeError message for deletion of a missing attribute
014e47d
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Apr 15, 2021
@bedevere-bot bedevere-bot added the awaiting review label Apr 15, 2021
@github-actions

github-actions Bot commented Jun 3, 2021

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Jun 3, 2021
@JelleZijlstra
Merge branch 'main' into patch-24
375bf08
JelleZijlstra
JelleZijlstra reviewed May 3, 2022
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I agree with this idea. Could you add a NEWS entry and a unit test that exercises this message?

(I promise I'm not stalking you, I just opened page 24 of the open PRs and found this one :) .)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
geryogam added 3 commits May 4, 2022 11:11
@geryogam
Remove duplicate test (see python#25863)
42da011
@geryogam
Add tests of error messages for attribute access
e8a7b27
@geryogam
Create 2022-05-04-11-37-20.bpo-43857.WuX8p3.rst
4b63ada
@geryogam geryogam changed the title bpo-43857: Fix the AttributeError message for deletion of a missing attribute bpo-43857: Improve the AttributeError message when deleting a missing attribute May 4, 2022
JelleZijlstra
JelleZijlstra reviewed May 4, 2022
View reviewed changes
Comment thread Lib/test/test_class.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra JelleZijlstra self-assigned this May 4, 2022
geryogam added 2 commits May 4, 2022 17:57
@geryogam
Restore a test incorrectly removed
ad05ba3
@geryogam
Update test_class.py
935eea5
JelleZijlstra
JelleZijlstra approved these changes May 4, 2022
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels May 4, 2022
@geryogam

geryogam commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @JelleZijlstra, but please don’t merge right now as some tests are failing. I am going to work on it in the next few hours. I’ll let you know when it works.

JelleZijlstra reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@geryogam
Update dictobject.c
1cb97fc
@geryogam
geryogam requested review from markshannon and methane as code owners May 4, 2022 17:23
geryogam added 4 commits May 4, 2022 21:04
@geryogam
Update object.c
8dd6bb0
@geryogam
Update object.c
128d6e5
@geryogam
Update dictobject.c
e8f2cc7
@geryogam
Update object.c
4391f88
@geryogam

geryogam commented May 4, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Not only objects but also type objects have inconsistent AttributeError messages between missing attribute retrieval and deletion:

$ python3
Python 3.9.12 (main, Mar 26 2022, 15:52:10) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A: pass
... 
>>> A.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'A' has no attribute 'x'
>>> del A.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: x
>>> A().x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'x'
>>> del A().x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: x

Compare that with PyPy for which the messages are consistent (well almost, 'type' object has no attribute 'x' should be type object 'A' has no attribute 'x' for del A.x):

$ pypy3
Python 3.7.13 (7e0ae751533460d5f89f3ac48ce366d8642d1db5, Apr 26 2022, 09:31:20)
[PyPy 7.3.9 with GCC Apple LLVM 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> class A: pass
>>>> 
>>>> A.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'A' has no attribute 'x'
>>>> del A.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'type' object has no attribute 'x'
>>>> A().x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'x'
>>>> del A().x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'x'

Following @markshannon’s PR #28802 for Python 3.11, the message fix of this line in Objects/object.c provided at the early stage of this PR no longer works for objects but only for type objects:

      if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
-         PyErr_SetObject(PyExc_AttributeError, name);
+         PyErr_Format(PyExc_AttributeError,
+                      "'%.100s' object has no attribute '%U'",
+                      tp->tp_name, name);

To fix the message for objects, we should also update this line in Objects/dictobject.c:

-             PyErr_SetObject(PyExc_AttributeError, name);
+             PyErr_Format(PyExc_AttributeError,
+                          "'%.100s' object has no attribute '%U'",
+                          Py_TYPE(obj)->tp_name, name);

That way we get the same messages as in PyPy.

Finally to solve the last message inconsistency ('type' object has no attribute 'x' should be type object 'A' has no attribute 'x' for del A.x), I replaced the last fix in Objects/object.c with this fix:

-     if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
-         PyErr_SetObject(PyExc_AttributeError, name);
+     if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
+         if (PyType_IsSubtype(tp, &PyType_Type)) {
+             PyErr_Format(PyExc_AttributeError,
+                          "type object '%.50s' has no attribute '%U'",
+                          ((PyTypeObject*)obj)->tp_name, name);
+         }
+         else {
+             PyErr_Format(PyExc_AttributeError,
+                          "'%.100s' object has no attribute '%U'",
+                          tp->tp_name, name);
+         }
+     }
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@geryogam

geryogam commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

@JelleZijlstra Done, all tests are passing. The PR is ready.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra
JelleZijlstra reviewed May 5, 2022
View reviewed changes
Comment thread Objects/object.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@geryogam
Update object.c
6f3ca32
JelleZijlstra
JelleZijlstra approved these changes May 5, 2022
View reviewed changes
@JelleZijlstra
JelleZijlstra merged commit a95138b into python:main May 5, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label May 5, 2022
@geryogam
geryogam deleted the patch-24 branch May 5, 2022 14:04
@geryogam

geryogam commented May 5, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Thanks again @JelleZijlstra!

(I promise I'm not stalking you, I just opened page 24 of the open PRs and found this one :) .)

No worries. And even if you were stalking my PRs I would not complain, quite the opposite =)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@maggyero maggyero mannequin mentioned this pull request May 8, 2023
Fix the AttributeError message for deletion of a missing attribute #88023
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@methane methane Awaiting requested review from methane methane is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@JelleZijlstra JelleZijlstra

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@geryogam @JelleZijlstra @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.