It seems like defining a type in get_provider_map(cls)
wouldn't work if that type is already handled by an existing base_factory (such as dataclass, Pydantic model, etc).
It's possible this is intended, as the Handling Custom Types docs only specifies a plain-old Python class CustomSecret
.
Please see the MCVE below for what my desired behavior would be. Is this currently possible? I've tried playing around with __base_factory_overrides__
but didn't have any luck.
No response
from pydantic import BaseModel, ConfigDict
from polyfactory.factories.pydantic_factory import ModelFactory
class CustomSecretPydantic(BaseModel):
value: str
class CustomSecretPlain:
def __init__(self, value: str) -> None:
self.value = value
def __repr__(self) -> str:
return self.value
def __str__(self) -> str:
return self.value
class MyModel(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
id: int
secret: CustomSecretPydantic
class MyFactory(ModelFactory[MyModel]):
@classmethod
def get_provider_map(cls) -> dict[type, Any]:
providers_map = super().get_provider_map()
return {
**providers_map,
CustomSecretPydantic: lambda: CustomSecretPydantic.model_validate({"value": "asdf"}),
CustomSecretPlain: lambda: CustomSecretPlain(value="asdf"),
}
if __name__ == "__main__":
print(MyFactory.build())
1. Run code
2. Observe that "asdf" is NOT generated
I'd expect that it would be.
"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)
"
No response
2.17.0
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too