zmievsa / pydantic-duality

Automatically and lazily generate three versions of your pydantic models: one with Extra.forbid, one with Extra.ignore, and one with all fields optional
https://ovsyanka83.github.io/pydantic-duality/
MIT License
32 stars 1 forks source link

Cannot define models with `Annotated` under Python 3.13 #15

Closed jvstme closed 1 month ago

jvstme commented 1 month ago

To Reproduce

  1. Create a script and define a model using Annotated.
    
    from pydantic_duality import DualBaseModel, Field
    from typing import Annotated

class M(DualBaseModel): a: Annotated[str, Field(description="a")]

print(M)

2. Run the script under Python 3.13
```shell
$ python3.13 main.py
Traceback (most recent call last):
  File "/tmp/main.py", line 4, in <module>
    class M(DualBaseModel):
        a: Annotated[str, Field(description="a")]
  File "/usr/local/lib/python3.13/site-packages/pydantic_duality/__init__.py", line 138, in __new__
    new_class._generate_alternative_classes(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        name, bases, attrs, request_suffix, response_suffix, patch_request_suffix, kwargs
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/pydantic_duality/__init__.py", line 171, in _generate_alternative_classes
    _alter_attrs(attrs, name + request_suffix, REQUEST_ATTR),
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/pydantic_duality/__init__.py", line 73, in _alter_attrs
    annotations[key] = _resolve_annotation(val, attr)
                       ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/pydantic_duality/__init__.py", line 41, in _resolve_annotation
    return Annotated.__class_getitem__(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/typing.py", line 548, in __getattr__
    raise AttributeError(item)
AttributeError: __class_getitem__

Expected behavior Defining models with Annotated works normally as it did under Python 3.12.

$ python3.12 main.py 
<class '__main__.M'>
zmievsa commented 1 month ago

Works in 1.2.2. Please, check. Thanks for your issue!