litestar-org / polyfactory

Simple and powerful factories for mock data generation
https://polyfactory.litestar.dev/
MIT License
988 stars 78 forks source link

Any not supported in provider_map #521

Closed guillaume-alliander closed 3 months ago

guillaume-alliander commented 4 months ago

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

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

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


[!NOTE]
While we are open for sponsoring on GitHub Sponsors and OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • 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.

Fund with Polar