Skip to content

Commit

Permalink
ByteString is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jun 18, 2023
1 parent 0ffc180 commit 37f115f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions hypothesis-python/tests/cover/test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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),
)


Expand Down
6 changes: 5 additions & 1 deletion hypothesis-python/tests/cover/test_type_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 8 additions & 1 deletion hypothesis-python/tests/nocover/test_from_type_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 37f115f

Please sign in to comment.