-
Notifications
You must be signed in to change notification settings - Fork 578
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
from_type fails on nested annotated type with mysterious error #3891
Comments
As an additional data point, the following works as expected: FiniteFloat = Annotated[float, Predicate(math.isfinite)]
@given(thing=st.from_type(FiniteFloat))
def test_annotated_type(thing):
assert thing == thing This is peculiar as |
from typing import Annotated
assert isinstance(float, type)
assert not isinstance(Annotated[float, "some annotation"], type) |
from_type ; perhaps the name is simply inaccurate?
|
In fact, it looks like the problem is that _NumericType = TypeVar('_NumericType', bound=Union[SupportsFloat, SupportsIndex])
IsFinite = Annotated[_NumericType, Predicate(math.isfinite)]
AsciiString = Annotated[str, IsAscii]
@given(thing=st.from_type(AsciiString))
def test_annotated_type(thing):
assert thing == thing |
It looks like the intended usage is as AsciiString = Annotated[str, IsAscii[str]]
@given(thing=st.from_type(AsciiString))
def test_annotated_type(thing):
assert thing == thing
|
Aha! It seems things like So maybe |
I think the core problem here is that the error message is confusing, and we should improve it! e.g. you saw
but "Arguments to the Annotated type cannot be Annotated" only helps if you already know the problem. Some ideas for how to improve this, in both Hypothesis and annotated-types:
|
I have the following code:
The result of running this test is:
Presumably I have annotated this type incorrectly, but I can't tell how. There is no obvious nesting in the way I wrote this. Can
hypothesis
clarify what is actually wrong here? Or is this an issue forannotated-types
(annotated-types/annotated-types#61)?The Python documentation at https://docs.python.org/3/library/typing.html#typing.Annotated appears to say that annotations that are nested should be automatically flattened, but this would be nesting on the first argument; I don't even understand how nesting on the second element arises.
The
hypothesis
test suite does not include annotatedfloat
types or theIsFinite
condition, but the format appears to match what is used for integers here:hypothesis/hypothesis-python/tests/test_annotated_types.py
Line 67 in 21fd917
The text was updated successfully, but these errors were encountered: