Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 4 tasks
Simske opened this issue Jun 25, 2023 · 1 comment · Fixed by #240
Closed
1 of 4 tasks

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

Simske opened this issue Jun 25, 2023 · 1 comment · Fixed by #240
Assignees
Labels
bug Something isn't working

Comments

@Simske
Copy link
Contributor

Simske commented Jun 25, 2023

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

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
Fund with Polar
@Simske Simske added the bug Something isn't working label Jun 25, 2023
@Goldziher
Copy link
Contributor

thanks for reporting, ill check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants