gh-153785: Generate AttributeError messages from context#153786
gh-153785: Generate AttributeError messages from context#153786johnslavik wants to merge 46 commits into
AttributeError messages from context#153786Conversation
This comment was marked as resolved.
This comment was marked as resolved.
AttributeError messages from contextAttributeError messages from context
Documentation build overview
39 files changed ·
|
This reverts commit 7378e30.
|
|
||
| When possible, :attr:`name` and :attr:`obj` are set automatically. | ||
|
|
||
| .. versionchanged:: 3.10 |
There was a problem hiding this comment.
We dropped the .. versionchanged:: next note that documented the generated message. This is a user-visible change to str(), so I think we should keep a versionchanged here, no?
There was a problem hiding this comment.
@serhiy-storchaka, do you agree on bringing it back?
There was a problem hiding this comment.
We usually do not add versionchanged for minor changes like changed error message. Otherwise our documentation would be overwhelmed by them.
vstinner
left a comment
There was a problem hiding this comment.
The following code writes 'lazy_import' object has no attribute 'missing1': it mentions lazy_import rather than module 'asyncio'. But I don't think that AttributeError_str() would be the right place to reify the asyncio module!
lazy import asyncio
class RaiseWithName:
def __getattr__(self, name):
raise AttributeError(name)
try:
getattr(globals()['asyncio'], "missing1")
except AttributeError as exc:
# assert exc.obj is ...
assert exc.name == "missing1"
print(str(exc))
"this" and "current one" is unclear here. Please update the PR description to describe the motivation. Also consider linking to the code for the trick in KeyError. |
I used Claude to infer the motivations and update the description. |
This trick has been applied to KeyError and AttributeError; should it be applied to other exceptions? Should it be implemented in such a way that the concern is shared across exceptions (explicitly or implicitly)? |
Short answer, not really. Details(Analysis by Claude, on my behalf.) A worthwhile question. Worth noting first that the two "tricks" only look alike at the surface (both override
Only the second is the interesting reusable pattern ("I have structured context attributes; render them lazily instead of forcing every raise site to build the string"). The natural cohort there is So I'd lean against building a shared framework here:
The one genuinely reusable piece is the guard predicate — "was this constructed with a default/absent message, or one that merely duplicates a structured field, so I'm safe to override?" (here: |
@vstinner, this code does not trigger the code path from this PR ( |
Co-authored-by: Victor Stinner <vstinner@python.org>
|
|
||
| When possible, :attr:`name` and :attr:`obj` are set automatically. | ||
|
|
||
| .. versionchanged:: 3.10 |
There was a problem hiding this comment.
We usually do not add versionchanged for minor changes like changed error message. Otherwise our documentation would be overwhelmed by them.
I think this is the best place to do this. We do the same trick in
KeyError.This also fixed issue #143811 that motivated the current one.
AttributeErrormessages from their context #153785