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: use provider map for any in coverage #574

Merged
merged 1 commit into from
Aug 17, 2024
Merged
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
8 changes: 4 additions & 4 deletions polyfactory/factories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
unwrap_optional,
)
from polyfactory.utils.model_coverage import CoverageContainer, CoverageContainerCallable, resolve_kwargs_coverage
from polyfactory.utils.predicates import get_type_origin, is_any, is_literal, is_optional, is_safe_subclass, is_union
from polyfactory.utils.predicates import get_type_origin, is_literal, is_optional, is_safe_subclass, is_union
from polyfactory.utils.types import NoneType
from polyfactory.value_generators.complex_types import handle_collection_type, handle_collection_type_coverage
from polyfactory.value_generators.constrained_collections import (
Expand Down Expand Up @@ -847,12 +847,12 @@ def get_field_value_coverage( # noqa: C901,PLR0912

yield handle_collection_type_coverage(child_meta, origin, cls, build_context=build_context)

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

elif provider := cls.get_provider_map().get(unwrapped_annotation):
yield CoverageContainerCallable(provider)

elif isinstance(unwrapped_annotation, TypeVar):
yield create_random_string(cls.__random__, min_length=1, max_length=10)

elif callable(unwrapped_annotation):
# if value is a callable we can try to naively call it.
# this will work for callables that do not require any parameters passed
Expand Down
7 changes: 6 additions & 1 deletion tests/test_provider_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def get_provider_map(cls) -> Dict[Any, Callable[[], Any]]:

assert foo.foo == "any"

coverage_result = list(FooFactory.coverage())
assert all(result.foo == "any" for result in coverage_result)


def test_provider_map_with_typevar() -> None:
T = TypeVar("T")
Expand All @@ -48,5 +51,7 @@ def get_provider_map(cls) -> Dict[Any, Callable[[], Any]]:
return provider_map

foo = FooFactory.build()

assert foo.foo == "any"

coverage_result = list(FooFactory.coverage())
assert all(result.foo == "any" for result in coverage_result)
Loading