polyfactory
polyfactory copied to clipboard
stricturl is not supported?
Hi, is it true? I got an error:
E pydantic_factories.exceptions.ParameterError: Unsupported type: <class 'xxx.xxx.models.UrlValue'>
E
E Either extend the providers map or add a factory function for this model field
Well, fixed with this:
from typing import Any
from pydantic import AnyUrl
class RequestFactory(ModelFactory):
__model__ = Request
@classmethod
def get_mock_value(cls, field_type: Any) -> Any:
if issubclass(field_type, AnyUrl):
return cls._get_faker().url()
return super().get_mock_value(field_type)
But that behaviour is not correct, can we implement this case?
@al-stefanitsky-mozdor -- sure, a PR is welcome
Do you have a reproducible example (For example the Request model that is used in your factory)? So stricturl should actually produce a constrained value right? Simply adding the url to provider_map (and returning AnyUrl) will not be sufficient if you set certain stricturl paramaters. For example if I would make a pydantic model:
from pydantic import BaseModel, stricturl
class TestClass(BaseModel):
url: stricturl(allowed_schemes=["tcp"])
Just returning a faker.url would not coincide with the proper stricturl value. I think supporting stricturl would mean we should support it similarly as handling a other constrained fields (such as ConstrainedFloat, ConstrainedInt etc.) What do you think?
Do you have a reproducible example (For example the Request model that is used in your factory)? So stricturl should actually produce a constrained value right? Simply adding the url to provider_map (and returning AnyUrl) will not be sufficient if you set certain stricturl paramaters. For example if I would make a pydantic model:
from pydantic import BaseModel, stricturl class TestClass(BaseModel): url: stricturl(allowed_schemes=["tcp"])Just returning a faker.url would not coincide with the proper stricturl value. I think supporting stricturl would mean we should support it similarly as handling a other constrained fields (such as ConstrainedFloat, ConstrainedInt etc.) What do you think?
yup, you are correct. The complexity is that we need to fake all the conditions handled by pydantic, which requires a rather complex url generator.
not gonna happen