Repository: litestar-org/polyfactory (python)

Issue:
Any not supported in provider_map
### Description

Trying to use polyfactory with a model with a `typing.Any` field, I cannot seem to cover it in the `get_provider_map`. Using `object` instead of `Any` works as a workaround, but does not feel right. 

Related to https://github.com/litestar-org/polyfactory/discussions/515

### URL to code causing the issue

_No response_

### MCVE

```python
from typing import Any

from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel


class WeirdModel(BaseModel):
    any: Any
    obj: object


class Factorio(ModelFactory[WeirdModel]):
    @classmethod
    def get_provider_map(cls) -> dict[type, Any]:
        providers_map = super().get_provider_map()

        return {
            **providers_map,
            Any: lambda: "this does not work",
            object: lambda: "this works as workaround",
        }


print(Factorio().build())
# prints: any='<a random string>' obj='this works as workaround'
# I would expect "this does not work" for any
```


### Steps to reproduce

```bash
Just run the above script (tested with python 3.12) for instance on the command line.
```


### Screenshots

"In the format of: `![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)`"


### Logs

_No response_

### Release Version

polyfactory==2.15.0
pydantic==2.6.4
pydantic_core==2.16.3

### Platform

- [X] Linux
- [ ] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)

<!-- POLAR PLEDGE BADGE START -->
---
> [!NOTE]  
> While we are open for sponsoring on [GitHub Sponsors](https://github.com/sponsors/litestar-org/) and 
> [OpenCollective](https://opencollective.com/litestar), we also utilize [Polar.sh](https://polar.sh/) to engage in pledge-based sponsorship.
>
> Check out all issues funded or available for funding [on our Polar.sh dashboard](https://polar.sh/litestar-org)
> * If you would like to see an issue prioritized, make a pledge towards it!
> * We receive the pledge once the issue is completed & verified
> * This, along with engagement in the community, helps us know which features are a priority to our users.

<a href="https://polar.sh/litestar-org/polyfactory/issues/521">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/litestar-org/polyfactory/issues/521/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/litestar-org/polyfactory/issues/521/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->


Pull request description:
fix: use provider map for generating value for 'Any'
<!--
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/)
-->

### Pull Request Checklist

- [ ] New code has 100% test coverage
- [ ] (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
- [ ] (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR
- [ ] Pre-Commit Checks were ran and passed
- [ ] Tests were ran and passed

### Description
<!--
Please describe your pull request for new release changelog purposes
-->

- We were incorrectly not considering the value provided in the provider map for any field typed as `Any.` This ensures that we consider the value given in the provider map allowing users to override the generated value.
- This also fixes the same issue that may arise if the user wishes to override the value generated for `TypeVar`.


### Close Issue(s)
<!--
Please add in issue numbers this pull request will close, if applicable
Examples: Fixes #4321 or Closes #1234
-->

- Fixes #521.


Patch:
diff --git a/polyfactory/factories/base.py b/polyfactory/factories/base.py
index e694b05..95a8f2f 100644
--- a/polyfactory/factories/base.py
+++ b/polyfactory/factories/base.py
@@ -491,7 +491,7 @@ class BaseFactory(ABC, Generic[T]):
             return lambda *args: None
 
         return {
-            Any: lambda: None,
+            Any: lambda: create_random_string(cls.__random__, min_length=1, max_length=10),
             # primitives
             object: object,
             float: cls.__faker__.pyfloat,
@@ -747,12 +747,12 @@ class BaseFactory(ABC, Generic[T]):
 
             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)
-
         if provider := cls.get_provider_map().get(unwrapped_annotation):
             return provider()
 
+        if isinstance(unwrapped_annotation, TypeVar):
+            return create_random_string(cls.__random__, min_length=1, max_length=10)
+
         if 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 07fd538..d9ae405 100644
--- a/tests/test_provider_map.py
+++ b/tests/test_provider_map.py
@@ -1,6 +1,8 @@
-from typing import Any
+from dataclasses import dataclass
+from typing import Any, Callable, Dict, Generic, TypeVar
 
 from polyfactory.factories.base import BaseFactory
+from polyfactory.factories.dataclass_factory import DataclassFactory
 
 
 def test_provider_map() -> None:
@@ -10,3 +12,41 @@ def test_provider_map() -> None:
     for type_, handler in provider_map.items():
         value = handler()
         assert isinstance(value, type_)
+
+
+def test_provider_map_with_any() -> None:
+    @dataclass
+    class Foo:
+        foo: Any
+
+    class FooFactory(DataclassFactory[Foo]):
+        @classmethod
+        def get_provider_map(cls) -> Dict[Any, Callable[[], Any]]:
+            provider_map = super().get_provider_map()
+            provider_map[Any] = lambda: "any"
+
+            return provider_map
+
+    foo = FooFactory.build()
+
+    assert foo.foo == "any"
+
+
+def test_provider_map_with_typevar() -> None:
+    T = TypeVar("T")
+
+    @dataclass
+    class Foo(Generic[T]):
+        foo: T
+
+    class FooFactory(DataclassFactory[Foo]):
+        @classmethod
+        def get_provider_map(cls) -> Dict[Any, Callable[[], Any]]:
+            provider_map = super().get_provider_map()
+            provider_map[T] = lambda: "any"
+
+            return provider_map
+
+    foo = FooFactory.build()
+
+    assert foo.foo == "any"