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 use built-in collections for field annotations #16

Closed jvstme closed 1 month ago

jvstme commented 1 month ago

To Reproduce

>>> from pydantic_duality import DualBaseModel
>>> class M(DualBaseModel):
...   a: list[str]
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jvstme/git/dstack/dstack/temp/venv/lib64/python3.10/site-packages/pydantic_duality/__init__.py", line 138, in __new__
    new_class._generate_alternative_classes(
  File "/home/jvstme/git/dstack/dstack/temp/venv/lib64/python3.10/site-packages/pydantic_duality/__init__.py", line 171, in _generate_alternative_classes
    _alter_attrs(attrs, name + request_suffix, REQUEST_ATTR),
  File "/home/jvstme/git/dstack/dstack/temp/venv/lib64/python3.10/site-packages/pydantic_duality/__init__.py", line 73, in _alter_attrs
    annotations[key] = _resolve_annotation(val, attr)
  File "/home/jvstme/git/dstack/dstack/temp/venv/lib64/python3.10/site-packages/pydantic_duality/__init__.py", line 51, in _resolve_annotation
    if inspect.isclass(annotation) and issubclass(annotation, BaseModel):
  File "/usr/lib64/python3.10/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

Expected behavior Using built-in collections for annotations is expected to be equivalent to using the (now deprecated) typing.* types.

>>> from pydantic_duality import DualBaseModel
>>> from typing import List
>>> class M(DualBaseModel):
...   a: List[str]
... 
>>>

Additional context Looks like the issue was introduced in pydantic-duality==1.2.0. The same example works fine with pydantic-duality==1.1.1

zmievsa commented 1 month ago

Thanks for reporting! I'll take a look at both of the issues this week.

zmievsa commented 1 month ago

Hi! Fixed both bugs in 1.2.1. Will publish today.