New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V2: hypothesis plugin rewrite #4682
Comments
I can definitely offer high-intensity code review and design advice, but unfortunately I'm already well over capacity for OSS projects at the moment1. I'd expect this to fall into three main pieces:
Happy to have a call sometime if that's useful, probably once (1) above is well under way. Footnotes
|
Hello, I was wonder how difficult this issue would be to complete? I and my partner @marcoo47 are interested in this and would be our first contribution. |
I think it's going to be a fairly large and intricate challenge, and recommend getting solid experience contributing to Hypothesis and Pydantic before taking it on 🙂 |
Any news on this? 😢 |
I started looking into this a while ago by first fixing HypothesisWorks/hypothesis#3356. However, the implementation is not perfect and some annotated types are not yet supported. If I find some time, I can take a look at this although I expect challenges to be faced during the implementation |
Hypothesis also handles collection-length bounds and some regex filters: HypothesisWorks/hypothesis#3795 This issue might be a nice project for people to work on at the PyCon sprints in a few weeks 🙂 |
Are there any recommended workarounds while this issue is still open, aside from reverting to Pydantic V1? Perhaps limiting which Pydantic V2 features are used or how they're used in a project, in combination with hypothesis-jsonschema? |
@butterlyn I generated the model jsonschema and used hypothesis-jsonschema to generate data. Very straightforward in most cases, but a few tricks:
|
Thanks @iurisilvio, that makes a lot of sense. Could you please elaborate on this trick you mention about custom formatter rules? Is this a Pydantic or a Hypothesis feature? |
The hypothesis-jsonschema Your It is useful for strings with patterns and limited sizes, because hypothesis first generate a string matching the pattern, then check the size constraints. It fails most of the time for high min lengths for example, making the data generation very slow. If you publish your jsonschema, maybe you don't want to add the extra format, you can patch the jsonschema manually, but it is difficult to make it as a generic solution. |
A working example: from typing import Annotated
from hypothesis import given, strategies as st
from hypothesis_jsonschema import from_schema
from pydantic import BaseModel, Field, StringConstraints
class MyModel(BaseModel):
foo: Annotated[
str, StringConstraints(min_length=36, pattern=r"^[abc]+$")
] = Field(json_schema_extra={"format": "foo"})
schema = MyModel.model_json_schema()
# or hack without `json_schema_extra` defined
# schema["properties"]["foo"]["format"] = "foo"
@given(from_schema(schema))
def test_foo_fail(data):
pass
@given(
from_schema(
schema,
custom_formats={
"foo": st.text(
alphabet=st.sampled_from(["a", "b", "c"]),
min_size=36,
)}
)
)
def test_foo_pass(data):
pass |
@Zac-HD (sorry for the ping), when we implemented While Pydantic uses mostly the Would there be a way to add a register mechanism for annotated constraints? If so, should it be in the same form as it is currently? (i.e. my custom base metadata subclass -> filter condition). Thanks in advance Edit: Pydantic also uses arbitrary classes meant to be used in
|
To provide filters, it'd be best for Hypothesis if you could make your annotations into either subclasses of For arbitrary other annotations, I think there are basically two options Hypothesis could support.
|
(plan still WIP). Hypothesis gained the ability to yield strategies from the Meaning it is possible to do: from typing import Annotated
from uuid import UUID
from annotated_types import GroupedMetadata
from hypothesis.strategies import from_type, uuids
class MyMetadata(GroupedMetadata):
def __iter__(self):
yield uuids(version=1)
st = from_type(Annotated[UUID, MyMetadata()])
#> uuids(version=1) As Pydantic implements constraints in different ways, I'll list the available types and how they are going to be supported in hypothesis. Types implemented using
|
|
As per #4516,
pydantic/_hypothesis.py
has been ravaged, I'm sorry about that, it seemed just about worthwhile keeping it in place while preventing mypy/flake8 errors, but I couldn't stretch to keeping it working with the new code in that PR.Once #4516 is merged, we therefore need to rewrite it and move it into
_internal
to respect the new use ofAnnotated
for most constraints. I don't know how helpful the new #4680 conversion table might be too?@Zac-HD as the expert on hypothesis and originally developer of the module, it would of course be amazing if you could work on this. But I understand if that's not possible, please let me know either way here. Also any extra insights or suggestions you have about how to go about this would be much appreciated.
The text was updated successfully, but these errors were encountered: