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

fix: #366: Fix Union[str, List[str]] #381

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ repos:
hooks:
- id: pdm-lock-check
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.286"
rev: "v0.0.290"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to split these changes into a new PR.

hooks:
- id: ruff
args: ["--fix"]
- repo: https://github.com/psf/black
rev: 23.7.0
rev: 23.9.1
hooks:
- id: black
args: [--config=./pyproject.toml]
Expand Down Expand Up @@ -59,7 +59,7 @@ repos:
sqlalchemy>=2,
]
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.324
rev: v1.1.327
hooks:
- id: pyright
exclude: "tests"
Expand Down
6 changes: 3 additions & 3 deletions polyfactory/factories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,12 @@ def get_field_value( # noqa: C901, PLR0911, PLR0912
batch_size = cls.__random__.randint(cls.__min_collection_length__, cls.__max_collection_length__)
return factory.batch(size=batch_size)

if (origin := get_type_origin(unwrapped_annotation)) and issubclass(origin, Collection):
return handle_collection_type(field_meta, origin, cls)

if is_union(field_meta.annotation) and field_meta.children:
return cls.get_field_value(cls.__random__.choice(field_meta.children))

if (origin := get_type_origin(unwrapped_annotation)) and issubclass(origin, Collection):
return handle_collection_type(field_meta, origin, cls)

if is_any(unwrapped_annotation) or isinstance(unwrapped_annotation, TypeVar):
return create_random_string(cls.__random__, min_length=1, max_length=10)

Expand Down
15 changes: 14 additions & 1 deletion tests/test_pydantic_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Optional
from typing import List, Optional, Union

import pytest
from pydantic import VERSION, BaseModel, Field, Json
Expand Down Expand Up @@ -78,3 +78,16 @@ class BFactory(ModelFactory[B]):
__model__ = B

assert isinstance(BFactory.build(), B)


def test_union_str_and_list() -> None:
class A(BaseModel):
a: Union[str, List[str]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add a test case for str | list[str] with __future__ import.


class AFactory(ModelFactory[A]):
__model__ = A

for _ in range(1000):
a = AFactory.process_kwargs()
if isinstance(a["a"], list):
assert len(a) == 1