Problem in a nutshell
I am having issues with the hypothesis build
strategy and custom pydantic data types (no values are returned when invoking the build strategy on my custom data type.
Problem in more detail
Given the following pydantic custom type, which just validates if a value is a timezone.
import pytz
from pydantic import StrictStr
TIMEZONES = pytz.common_timezones_set
class CountryTimeZone(StrictStr):
"""Validate a country timezone."""
@classmethod
def __get_validators__(cls):
yield from super().__get_validators__()
yield cls.validate_timezone
@classmethod
def validate_timezone(cls, v):
breakpoint()
if v not in TIMEZONES:
raise ValueError(f"{v} is not a valid country timezone")
return v
@classmethod
def __modify_schema__(cls, field_schema):
field_schema.update(examples=TIMEZONES)
When I attempt to use this in some schema...
from pydantic import BaseModel
class Foo(BaseModel):
bar: CountryTimeZone
and subsequently try to build an example in a test, using the pydantic hypothesis plugin like.
from hypothesis import given
from hypothesis import strategies as st
@given(st.builds(Foo))
def test_something_interesting(schema) -> None:
# Some assertions
...
schema.bar
is always ""
.
Questions
- Is there something missing from this implementation, meaning that values like
"Asia/Krasnoyarsk"
aren't being generated? From the documentation, examples likePaymentCardNumber
andEmailStr
build as expected. - Even when using
StrictStr
by itself, the resulting value is also an empty string. I tried to inherit fromstr
but still no luck.