Skip to content

Commit

Permalink
fix: use provider map for any in coverage (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
adhtruong authored Aug 17, 2024
1 parent d75b1d2 commit 6f5b78c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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)

0 comments on commit 6f5b78c

Please sign in to comment.