diff --git a/hypothesis-python/src/hypothesis/strategies/_internal/types.py b/hypothesis-python/src/hypothesis/strategies/_internal/types.py index ba134ffa58..c2a8637f4a 100644 --- a/hypothesis-python/src/hypothesis/strategies/_internal/types.py +++ b/hypothesis-python/src/hypothesis/strategies/_internal/types.py @@ -429,7 +429,7 @@ def from_typing_type(thing): # Except for sequences of integers, or unions which include integer! # See https://github.com/HypothesisWorks/hypothesis/issues/2257 # - # This block drops ByteString from the types that can be generated + # This block drops bytes from the types that can be generated # if there is more than one allowed type, and the element type is # not either `int` or a Union with `int` as one of its elements. elem_type = (getattr(thing, "__args__", None) or ["not int"])[0] @@ -450,7 +450,7 @@ def from_typing_type(thing): and thing.__forward_arg__ in vars(builtins) ): return st.from_type(getattr(builtins, thing.__forward_arg__)) - # Before Python 3.9, we sometimes have e.g. ByteString from both the typing + # Before Python 3.9, we sometimes have e.g. Sequence from both the typing # module, and collections.abc module. Discard any type which is not it's own # origin, where the origin is also in the mapping. for t in sorted(mapping, key=type_sorting_key): diff --git a/hypothesis-python/tests/cover/test_lookup.py b/hypothesis-python/tests/cover/test_lookup.py index 409c96baaf..c11d1b20fc 100644 --- a/hypothesis-python/tests/cover/test_lookup.py +++ b/hypothesis-python/tests/cover/test_lookup.py @@ -110,7 +110,11 @@ def test_typing_Type_Union(ex): @given(data=st.data()) def test_rare_types(data, typ): ex = data.draw(from_type(typ)) - assert isinstance(ex, typ) + with warnings.catch_warnings(): + if sys.version_info[:2] >= (3, 12): + warnings.simplefilter("ignore", DeprecationWarning) + # ByteString is deprecated in Python 3.12 + assert isinstance(ex, typ) class Elem: @@ -752,7 +756,7 @@ def test_inference_on_generic_collections_abc_aliases(typ, data): def test_bytestring_not_treated_as_generic_sequence(val): # Check that we don't fall into the specific problem from # https://github.com/HypothesisWorks/hypothesis/issues/2257 - assert not isinstance(val, typing.ByteString) + assert not isinstance(val, bytes) # Check it hasn't happened again from some other non-generic sequence type. for x in val: assert isinstance(x, set) @@ -764,7 +768,7 @@ def test_bytestring_not_treated_as_generic_sequence(val): def test_bytestring_is_valid_sequence_of_int_and_parent_classes(type_): find_any( st.from_type(typing.Sequence[type_]), - lambda val: isinstance(val, typing.ByteString), + lambda val: isinstance(val, bytes), ) diff --git a/hypothesis-python/tests/cover/test_type_lookup.py b/hypothesis-python/tests/cover/test_type_lookup.py index e5fb1c594e..5babf51103 100644 --- a/hypothesis-python/tests/cover/test_type_lookup.py +++ b/hypothesis-python/tests/cover/test_type_lookup.py @@ -211,7 +211,11 @@ def test_uninspectable_from_type(): def _check_instances(t): # See https://github.com/samuelcolvin/pydantic/discussions/2508 - return t.__module__ != "typing" and not t.__module__.startswith("pydantic") + return ( + t.__module__ != "typing" + and t.__name__ != "ByteString" + and not t.__module__.startswith("pydantic") + ) @pytest.mark.parametrize( diff --git a/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py b/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py index 488023b6c7..7e4ccfcd36 100644 --- a/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py +++ b/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py @@ -134,7 +134,7 @@ def test_bound_type_cheking_only_forward_ref(): st.builds(typechecking_only_fun).example() -def test_bound_type_cheking_only_forward_ref_wrong_type(): +def test_bound_type_checking_only_forward_ref_wrong_type(): """We should check ``ForwardRef`` parameter name correctly.""" with utils.temp_registered(ForwardRef("WrongType"), st.just(1)): with pytest.raises(ResolutionFailed): diff --git a/hypothesis-python/tests/nocover/test_from_type_recipe.py b/hypothesis-python/tests/nocover/test_from_type_recipe.py index 4d4464f8a6..d640ee6816 100644 --- a/hypothesis-python/tests/nocover/test_from_type_recipe.py +++ b/hypothesis-python/tests/nocover/test_from_type_recipe.py @@ -11,7 +11,14 @@ from hypothesis import given, strategies as st from hypothesis.strategies._internal.types import _global_type_lookup -TYPES = sorted((x for x in _global_type_lookup if x.__module__ != "typing"), key=str) +TYPES = sorted( + ( + x + for x in _global_type_lookup + if x.__module__ != "typing" and x.__name__ != "ByteString" + ), + key=str, +) def everything_except(excluded_types):