gh-115165: Fix Annotated for immutable types#115213
Conversation
The return value from an annotated callable can raise any exception from __setattr__ for the `__orig_class__` property.
JelleZijlstra
left a comment
There was a problem hiding this comment.
Thanks!
I think this is safe to backport to the bugfix versions (3.11 and 3.12).
Misc/NEWS.d/next/Library/2024-02-09-07-20-16.gh-issue-115165.yfJLXA.rst
Outdated
Show resolved
Hide resolved
AlexWaygood
left a comment
There was a problem hiding this comment.
Thanks! This is really great for a first contribution :)
A few small points below:
Misc/NEWS.d/next/Library/2024-02-09-07-20-16.gh-issue-115165.yfJLXA.rst
Outdated
Show resolved
Hide resolved
Update news blurb for pythongh-115165
Add comment justifying `except Exception`
Renamed test_annotated_callable_returning_immutable to test_instantiate_immutable and moved it near the other instantiation tests.
Add a test for generic instantiation that trips the same __setattr__ exception in Annotation.
|
@AlexWaygood and @JelleZijlstra -- thanks for the suggestions. I've addressed them locally. Do you prefer "fixup! ..." commits for auto-squashing or independent commits that will be preserved? |
|
It doesn't really matter, we squash the commits on merge. The only thing we generally ask for is to avoid force-pushing to your branch; that makes review harder. |
All commits will be squash-merged into a single commit by GitHub when we merge your PR into the |
|
Thanks @dave-shawley for the PR, and @AlexWaygood for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12. |
|
GH-115227 is a backport of this pull request to the 3.12 branch. |
|
GH-115228 is a backport of this pull request to the 3.11 branch. |
…15213) The return value from an annotated callable can raise any exception from __setattr__ for the `__orig_class__` property.
|
@JelleZijlstra @AlexWaygood Can this be backported to older versions too? |
|
Looks like it was backported to 3.11 and 3.12. Older versions only receive security fixes now, which this isn't. |
This PR widens the list of ignored exceptions when setting the
__orig_class__attribute on the return value from an annotated callable. OnlyAttributeErrorwas caught yet instances can raise anyExceptionfrom__setattr__. After this PR, it will catch anyExceptioninstead. This change was motivated by theuuid.UUID.__setattr__implementation raisesTypeError.