1616import abc
1717import enum
1818import sys
19- from typing import Callable , Generic , List , Sequence , TypeVar , Union
19+ from typing import Callable , Dict , Generic , List , Sequence , TypeVar , Union
2020
2121import pytest
2222
2626 InvalidArgument ,
2727 ResolutionFailed ,
2828)
29+ from hypothesis .internal .reflection import get_pretty_function_description
2930from hypothesis .strategies ._internal import types
3031from hypothesis .strategies ._internal .core import _from_type
3132from hypothesis .strategies ._internal .types import _global_type_lookup
@@ -232,6 +233,18 @@ def __init__(self, arg: T) -> None:
232233 self .arg = arg
233234
234235
236+ class Lines (Sequence [str ]):
237+ """Represent a sequence of text lines.
238+
239+ It turns out that resolving a class which inherits from a parametrised generic
240+ type is... tricky. See https://github.com/HypothesisWorks/hypothesis/issues/2951
241+ """
242+
243+
244+ class SpecificDict (Dict [int , int ]):
245+ pass
246+
247+
235248def using_generic (instance : MyGeneric [T ]) -> T :
236249 return instance .arg
237250
@@ -248,6 +261,26 @@ def test_generic_origin_empty():
248261_skip_callables_mark = pytest .mark .skipif (sys .version_info [:2 ] < (3 , 7 ), reason = "old" )
249262
250263
264+ @_skip_callables_mark
265+ def test_issue_2951_regression ():
266+ lines_strat = st .builds (Lines , lines = st .lists (st .text ()))
267+ with temp_registered (Lines , lines_strat ):
268+ assert st .from_type (Lines ) == lines_strat
269+ # Now let's test that the strategy for ``Sequence[int]`` did not
270+ # change just because we registered a strategy for ``Lines``:
271+ expected = "one_of(binary(), lists(integers()))"
272+ assert repr (st .from_type (Sequence [int ])) == expected
273+
274+
275+ @_skip_callables_mark
276+ def test_issue_2951_regression_two_params ():
277+ map_strat = st .builds (SpecificDict , st .dictionaries (st .integers (), st .integers ()))
278+ expected = repr (st .from_type (Dict [int , int ]))
279+ with temp_registered (SpecificDict , map_strat ):
280+ assert st .from_type (SpecificDict ) == map_strat
281+ assert expected == repr (st .from_type (Dict [int , int ]))
282+
283+
251284@pytest .mark .parametrize (
252285 "generic" ,
253286 (
@@ -287,13 +320,20 @@ def test_generic_origin_without_type_args(generic):
287320 pass
288321
289322
290- def test_generic_origin_from_type ():
323+ @pytest .mark .parametrize (
324+ "strat, type_" ,
325+ [
326+ (st .from_type , MyGeneric [T ]),
327+ (st .from_type , MyGeneric [int ]),
328+ (st .from_type , MyGeneric ),
329+ (st .builds , using_generic ),
330+ (st .builds , using_concrete_generic ),
331+ ],
332+ ids = get_pretty_function_description ,
333+ )
334+ def test_generic_origin_from_type (strat , type_ ):
291335 with temp_registered (MyGeneric , st .builds (MyGeneric )):
292- find_any (st .from_type (MyGeneric [T ]))
293- find_any (st .from_type (MyGeneric [int ]))
294- find_any (st .from_type (MyGeneric ))
295- find_any (st .builds (using_generic ))
296- find_any (st .builds (using_concrete_generic ))
336+ find_any (strat (type_ ))
297337
298338
299339def test_generic_origin_concrete_builds ():
0 commit comments