A from_type(Sequence[XXX]) strategy is able to generate instances of classes unrelated to XXX that are Sequence subtypes registered with register_type_strategy().
Probably best explained with an example. Both of these tests fail:
from collections import namedtuple
from collections.abc import Sequence
from hypothesis import given
from hypothesis import strategies as st
class Thing(namedtuple("Thing", [])):
pass
class CustomSequence(Sequence):
def __getitem__(self, i) -> Never:
raise IndexError()
def __len__(self) -> int:
return 0
class Other:
pass
st.register_type_strategy(Thing, st.builds(Thing))
st.register_type_strategy(CustomSequence, st.builds(CustomSequence))
@given(st.from_type(Sequence[Other]))
def test_thing_is_not_seq_of_other(others: Sequence[Other]) -> None:
assert not isinstance(others, Thing)
@given(st.from_type(Sequence[Other]))
def test_customseq_is_not_seq_of_other(others: Sequence[Other]) -> None:
assert not isinstance(others, CustomSequence)
I ran into this after upgrading from hypothesis 6.68.2 to 6.87.3. In my case I had some strategies registered with namedtuple types which started getting generated for unrelated strategies. It also happens for Sequence subclasses, but only if they are not specifying a generic type.
It seems to be related to #2951.
|
def from_typing_type(thing): |
try_issubclass(k, thing) thinks the Thing namedtuple is a subclass of Sequence[Other].
I can see this is a rather hairy problem looking at the code!
A
from_type(Sequence[XXX])strategy is able to generate instances of classes unrelated toXXXthat are Sequence subtypes registered withregister_type_strategy().Probably best explained with an example. Both of these tests fail:
I ran into this after upgrading from hypothesis
6.68.2to6.87.3. In my case I had some strategies registered with namedtuple types which started getting generated for unrelated strategies. It also happens forSequencesubclasses, but only if they are not specifying a generic type.It seems to be related to #2951.
hypothesis/hypothesis-python/src/hypothesis/strategies/_internal/types.py
Line 326 in 8a379f3
try_issubclass(k, thing)thinks theThingnamedtuple is a subclass ofSequence[Other].I can see this is a rather hairy problem looking at the code!