Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

typing.Annotated fails for functions returning UUIDs #115165

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error
@dave-shawley

Description

@dave-shawley

Bug report

Bug description:

typing.Annotated tries to set an attribute on thereturn value of calling an annotated callable. This fails when the returned object is immutable. I believe that addingTypeError to theexcept clause intyping. _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 in17689e3

try:
result.__orig_class__=self
exceptAttributeError:
pass

Example

importtypingimportuuidclassMyAnnotation:def__init__(self,**properties)->None:self.properties=propertiesdefuuid_from_str(s:str)->uuid.UUID:returnuuid.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

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


      [8]ページ先頭

      ©2009-2025 Movatter.jp