You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
typing.Annotated fails for functions returning UUIDs #115165
typing.Annotated tries to set an attribute on the return value of calling an annotated callable. This fails when the returned object is immutable. I believe that adding TypeError to the except clause in typing. _BaseGenericAlias.__call__ will fix the problem. I'm not sure if there are more cases of the same pattern or not.
Traceback (most recent call last):
File "/Users/.../foo.py", line 12, in <module>
coercion('00000000-0000-0000-0000-000000000000')
File "/Users/.../lib/python3.12/typing.py", line 1142, in __call__
result.__orig_class__ = self
^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../lib/python3.12/uuid.py", line 278, in __setattr__
raise TypeError('UUID objects are immutable')
TypeError: UUID objects are immutable
Why is the uuid module raising TypeError from a __setattr__ method? That seems like an antipattern; it should probably be raising AttributeError. We probably can't change that now, though, for backwards-compatibility reasons
Yes, the typing module should indeed be more careful here. Feel free to send a PR — I think except Exception might actually be better here than just adding TypeError to the except clause. Otherwise we'll just get this bug reported again when somebody tries to use Annotated with a class that raises e.g. RuntimeError (or whatever) from its __setattr__ method
Activity
AlexWaygood commentedon Feb 8, 2024
Thanks for the report!
Arguably there are two bugs here:
uuidmodule raisingTypeErrorfrom a__setattr__method? That seems like an antipattern; it should probably be raisingAttributeError. We probably can't change that now, though, for backwards-compatibility reasonstypingmodule should indeed be more careful here. Feel free to send a PR — I thinkexcept Exceptionmight actually be better here than just addingTypeErrorto theexceptclause. Otherwise we'll just get this bug reported again when somebody tries to useAnnotatedwith a class that raises e.g.RuntimeError(or whatever) from its__setattr__methodfixup! pythongh-115165: Fix Annotated for immutable types
fixup! pythongh-115165: Fix Annotated for immutable types
fixup! pythongh-115165: Fix Annotated for immutable types
fixup! pythongh-115165: Fix Annotated for immutable types
gh-115165: Fix `typing.Annotated` for immutable types (#115213)
pythongh-115165: Fix `typing.Annotated` for immutable types (pythonGH…
pythongh-115165: Fix `typing.Annotated` for immutable types (pythonGH…
[3.12] gh-115165: Fix `typing.Annotated` for immutable types (GH-115213…
[3.11] gh-115165: Fix `typing.Annotated` for immutable types (GH-115213…
AlexWaygood commentedon Feb 9, 2024
Thanks for the report, and the fix!
pythongh-115165: Fix `typing.Annotated` for immutable types (python#1…
pythongh-115165: Fix Annotated for immutable types
Add UUID support