GithubHelp home page GithubHelp logo

Comments (8)

jobh avatar jobh commented on August 20, 2024 1

...or even better, put this error message in Pydantic 🙂

from hypothesis.

Zac-HD avatar Zac-HD commented on August 20, 2024

Seems fine to me: None can't be less than 42, so your type annotation is invalid!

Instead, try using Optional[Annotated[int, Le(42)]] - I'd guess this is how Field(default=None, le=42) comes out.

from hypothesis.

jcklie avatar jcklie commented on August 20, 2024

Thank you for the quick reply. Both the pydantic and annotated types fail. I am not sure whether I understood your response. How should I write the pydantic model so that it works with hypothesis?

from hypothesis.

Zac-HD avatar Zac-HD commented on August 20, 2024

How should I write the pydantic model so that it works with hypothesis?

Ah, apparently I was too optimistic about Pydantic semantics making sense matching the rest of Python.

Maybe using my suggested type annotation would do the trick? But I don't actually know, sorry, and pydantic/pydantic#4682 is ongoing but obviously doesn't solve this yet.

from hypothesis.

jobh avatar jobh commented on August 20, 2024

@jcklie The annotated type fails because it's written as Annotated[Optional] instead of Optional[Annotated], and of course the assert must also cover None. @Zac-HD's suggestion seems to work fine. But only without a field validator — if the field validator is added, it is applied to both types i.e. one substrategy is none().filter(functools.partial(ge, 42)), which doesn't make sense.

class Foo(BaseModel):
    x: Optional[Annotated[int, Le(42)]] = Field(default=None)


@given(st.from_type(Foo))
def test_foo_1(foo):
    assert foo.x is None or foo.x <= 42


def test_foo_2():
    Foo(x=None)
    Foo(x=42)
    with pytest.raises(ValidationError):
        Foo(x=43)

from hypothesis.

jcklie avatar jcklie commented on August 20, 2024

All the examples I find on the internet using Annotated and Optional use the syntax of Annotated[Optional[x]], e.g. 1, 2, so I think at least both orders should be valid. The first link is even from cpython. Thus I still think this is a bug and both the pydantic and annotated types examples I gave should not throw. Could you maybe open this issue again at least to track this?

from hypothesis.

jobh avatar jobh commented on August 20, 2024

All the examples I find on the internet using Annotated and Optional use the syntax of Annotated[Optional[x]]

It depends on what should be annotated. I.e.: what should be <=42? It's the int. In example 1, what should be documented? It's the top level type i.e. the Optional[int].

But if pydantic accepts the swapped order, that's unfortunate and IMO too lenient... so yeah, possibly it would be nice to recognize this and either accept or have a customized error message.

from hypothesis.

Zac-HD avatar Zac-HD commented on August 20, 2024

All the examples I find on the internet using Annotated and Optional use the syntax of Annotated[Optional[x]], e.g. 1, 2, so I think at least both orders should be valid.

The CPython example is adding metadata but not a type-specific constraint. The Pydantic example also doesn't have any constraints.

But even if there was precedent in other libraries, I would reject this idea for Hypothesis - special cases are the bane of user understanding.

(I'd be happy to accept a PR with a specific error message, but don't think that's worth a tracking issue)

from hypothesis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.