litestar-org / polyfactory

Simple and powerful factories for mock data generation
https://polyfactory.litestar.dev/
MIT License
1.06k stars 83 forks source link

Bug: List[Literal[..]] throws TypeError #239

Closed Simske closed 1 year ago

Simske commented 1 year ago

Description

factory.build() fails for models with a field declared as List[Literal[...]] because issubclass() doesn't support checking Literal types.

A PR with a proposed fix will be opened shortly.

URL to code causing the issue

No response

MCVE

from dataclasses import dataclass
from typing import Literal, List
from polyfactory.factories import DataclassFactory

@dataclass
class Foo:
    bar: List[Literal["Hallo", "Hello", "Hola"]]

class FooFactory(DataclassFactory[Foo]):
    __model__ = Foo

print(FooFactory.build())

Steps to reproduce

1. Create model or dataclass with a field with type `List[Literal[literal_values]]`
2. Create factory for model
3. Use factory to generate object from model

Logs

Traceback (most recent call last):
  File "/tmp/polyfactory_reproduce.py", line 12, in <module>
    print(FooFactory.build())
          ^^^^^^^^^^^^^^^^^^
  File "/tmp/polyfactory/lib64/python3.11/site-packages/polyfactory/factories/base.py", line 765, in build
    return cast("T", cls.__model__(**cls.process_kwargs(**kwargs)))
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/polyfactory/lib64/python3.11/site-packages/polyfactory/factories/base.py", line 748, in process_kwargs
    result[field_meta.name] = cls.get_field_value(field_meta, field_build_parameters=field_build_parameters)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/polyfactory/lib64/python3.11/site-packages/polyfactory/factories/base.py", line 667, in get_field_value
    return handle_complex_type(field_meta=field_meta, factory=cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/polyfactory/lib64/python3.11/site-packages/polyfactory/value_generators/complex_types.py", line 63, in handle_complex_type
    return handle_collection_type(field_meta, origin, factory)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/polyfactory/lib64/python3.11/site-packages/polyfactory/value_generators/complex_types.py", line 35, in handle_collection_type
    container.append(handle_complex_type(field_meta.children[0], factory))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/polyfactory/lib64/python3.11/site-packages/polyfactory/value_generators/complex_types.py", line 62, in handle_complex_type
    if issubclass(origin, Collection):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 1570, in __subclasscheck__
    return issubclass(cls, self.__origin__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen abc>", line 123, in __subclasscheck__
TypeError: issubclass() arg 1 must be a class

Litestar Version

polyfactory==2.3.3 Faker==18.11.1

Platform

Fund with Polar

Goldziher commented 1 year ago

thanks for reporting, ill check it out.