Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
bpo-43857: Improve the AttributeError message when deleting a missing attribute#25424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This PR is stale because it has been open for 30 days with no activity. |
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 :) .)
Uh oh!
There was an error while loading.Please reload this page.
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. |
geryogam commentedMay 4, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Not only objects but alsotype objects have inconsistent $python3Python3.9.12 (main,Mar262022,15:52:10) [Clang13.0.0 (clang-1300.0.29.30)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>classA:pass...>>>A.xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:typeobject'A'hasnoattribute'x'>>>delA.xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:x>>>A().xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:'A'objecthasnoattribute'x'>>>delA().xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:x Compare that with PyPy for which the messages are consistent (well almost, $pypy3Python3.7.13 (7e0ae751533460d5f89f3ac48ce366d8642d1db5,Apr262022,09:31:20)[PyPy7.3.9withGCCAppleLLVM13.0.0 (clang-1300.0.29.30)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>>classA:pass>>>>>>>>A.xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:typeobject'A'hasnoattribute'x'>>>>delA.xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:'type'objecthasnoattribute'x'>>>>A().xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:'A'objecthasnoattribute'x'>>>>delA().xTraceback (mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:'A'objecthasnoattribute'x' Following@markshannon’s PR#28802 for Python 3.11, the message fix ofthis line in Objects/object.c provided at the early stage of this PR no longer works for objects but only fortype 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 updatethis 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 ( -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);+ }+ } |
@JelleZijlstra Done, all tests are passing. The PR is ready. |
Uh oh!
There was an error while loading.Please reload this page.
geryogam commentedMay 5, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Thanks again@JelleZijlstra!
No worries. And even if you were stalking my PRs I would not complain, quite the opposite =) |
Uh oh!
There was an error while loading.Please reload this page.
https://bugs.python.org/issue43857
#88023