Skip to content

typing.Annotated fails for functions returning UUIDs #115165

Description

@dave-shawley

Bug report

Bug description:

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.

cpython/Lib/typing.py

Lines 1128 to 1131 in 17689e3

try:
result.__orig_class__ = self
except AttributeError:
pass

Example

import typing
import uuid

class MyAnnotation:
    def __init__(self, **properties) -> None:
        self.properties = properties

def uuid_from_str(s: str) -> uuid.UUID:
    return uuid.UUID(f'urn:uuid:{s}')

coercion = typing.Annotated[uuid_from_str, MyAnnotation(type='str', format='uuid')]
coercion('00000000-0000-0000-0000-000000000000')

Result

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

CPython versions tested on:

3.9, 3.10, 3.11, 3.12

Operating systems tested on:

macOS

Linked PRs

Activity

  1. AlexWaygood commented on Feb 8, 2024

    @AlexWaygood
    Member

    Thanks for the report!

    Arguably there are two bugs here:

    1. 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
    2. 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
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 8, 2024
  3. added 4 commits that reference this issue on Feb 9, 2024
  4. added a commit that references this issue on Feb 9, 2024
  5. added 2 commits that reference this issue on Feb 9, 2024
  6. added 2 commits that reference this issue on Feb 9, 2024
  7. AlexWaygood commented on Feb 9, 2024

    @AlexWaygood
    Member

    Thanks for the report, and the fix!

  8. added a commit that references this issue on Feb 14, 2024
  9. added a commit that references this issue on Feb 22, 2024
  10. added a commit that references this issue on Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions