polyfactory icon indicating copy to clipboard operation
polyfactory copied to clipboard

stricturl is not supported?

Open al-stefanitsky-mozdor opened this issue 3 years ago • 4 comments

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

al-stefanitsky-mozdor avatar Apr 01 '22 09:04 al-stefanitsky-mozdor

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 avatar Apr 01 '22 09:04 al-stefanitsky-mozdor

@al-stefanitsky-mozdor -- sure, a PR is welcome

Goldziher avatar Apr 02 '22 09:04 Goldziher

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?

DaanRademaker avatar Apr 11 '22 11:04 DaanRademaker

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.

Goldziher avatar Apr 12 '22 04:04 Goldziher

not gonna happen

Goldziher avatar Apr 03 '23 15:04 Goldziher