Repository: litestar-org/polyfactory
Pull request #574 (2024-08-04 17:37:55)

fix: use provider map for any in coverage
<!--
By submitting this pull request, you agree to:
- follow [Litestar's Code of Conduct](https://github.com/litestar-org/.github/blob/main/CODE_OF_CONDUCT.md)
- follow [Litestar's contribution guidelines](https://github.com/litestar-org/.github/blob/main/CONTRIBUTING.md)
- follow the [PSFs's Code of Conduct](https://www.python.org/psf/conduct/)
-->
## Description

Update coverage to match https://github.com/litestar-org/polyfactory/pull/522

<!--
Please add in issue numbers this pull request will close, if applicable
Examples: Fixes #4321 or Closes #1234

Ensure you are using a supported keyword to properly link an issue:
https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->
## Closes


sonarcloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=starlite-api_polyfactory&pullRequest=574) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=starlite-api_polyfactory&pullRequest=574&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=starlite-api_polyfactory&pullRequest=574&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=starlite-api_polyfactory&pullRequest=574&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [66.7% Coverage on New Code](https://sonarcloud.io/component_measures?id=starlite-api_polyfactory&pullRequest=574&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=starlite-api_polyfactory&pullRequest=574&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=starlite-api_polyfactory&pullRequest=574)


github-actions[bot]: Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/574

Patch:
diff --git a/polyfactory/factories/base.py b/polyfactory/factories/base.py
index 2df44fa..778b496 100644
--- a/polyfactory/factories/base.py
+++ b/polyfactory/factories/base.py
@@ -61,7 +61,7 @@ from polyfactory.utils.helpers import (
     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 (
@@ -847,12 +847,12 @@ class BaseFactory(ABC, Generic[T]):
 
                 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


Test patch:
diff --git a/tests/test_provider_map.py b/tests/test_provider_map.py
index d9ae405..75c54eb 100644
--- a/tests/test_provider_map.py
+++ b/tests/test_provider_map.py
@@ -31,6 +31,9 @@ def test_provider_map_with_any() -> None:
 
     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")
@@ -48,5 +51,7 @@ def test_provider_map_with_typevar() -> None:
             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)