Skip to content

Commit

Permalink
ensured that ghostwriter does not use deprecated aliases
Browse files Browse the repository at this point in the history
collections contains deprecated aliases for collections.abc classes.
This commit ensures that those are not used anymore.
  • Loading branch information
ThunderKey committed Jun 15, 2023
1 parent 9484551 commit db3688f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ their individual contributions.
* `Nicholas Chammas <https://www.github.com/nchammas>`_
* `Nick Anyos <https://www.github.com/NickAnyos>`_
* `Nick Muoh <https://github.com/OdinTech3>`_ (nickspirit3@gmail.com)
* `Nicolas Ganz <https://www.github.com/ThunderKey>`_
* `Nicolas Erni <https://www.github.com/ThunderKey>`_
* `Nikita Sobolev <https://github.com/sobolevn>`_ (mail@sobolevn.me)
* `Oleg Höfling <https://github.com/hoefling>`_ (oleg.hoefling@gmail.com)
* `Paul Ganssle <https://ganssle.io>`_ (paul@ganssle.io)
Expand Down
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This release ensures that Ghostwriter does not use the deprecated aliases
for the ``collections.abc`` classes in ``collections``.
5 changes: 5 additions & 0 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@ def _get_module_helper(obj):
# The goal is to show location from which obj should usually be accessed, rather
# than what we assume is an internal submodule which defined it.
module_name = obj.__module__

# if "collections.abc" is used don't use the deprecated aliases in "collections"
if module_name == "collections.abc":
return module_name

dots = [i for i, c in enumerate(module_name) if c == "."] + [None]
for idx in dots:
if getattr(sys.modules.get(module_name[:idx]), obj.__name__, None) is obj:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import collections.abc
import test_expected_output
from hypothesis import given, strategies as st


@given(items=st.one_of(st.binary(), st.lists(st.integers())))
def test_fuzz_sequence_from_collections(items: collections.abc.Sequence[int]) -> None:
test_expected_output.sequence_from_collections(items=items)
16 changes: 16 additions & 0 deletions hypothesis-python/tests/ghostwriter/test_expected_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ast
import base64
import builtins
import collections.abc
import operator
import pathlib
import re
Expand Down Expand Up @@ -106,6 +107,17 @@ def union_sequence_parameter(items: Sequence[Union[float, int]]) -> float:
return sum(items)


if sys.version_info[:2] >= (3, 9):
CollectionsSequence = collections.abc.Sequence
else:
# in older versions collections.abc was not generic
CollectionsSequence = Sequence


def sequence_from_collections(items: CollectionsSequence[int]) -> int:
return min(items)


# Note: for some of the `expected` outputs, we replace away some small
# parts which vary between minor versions of Python.
@pytest.mark.parametrize(
Expand All @@ -128,6 +140,10 @@ def union_sequence_parameter(items: Sequence[Union[float, int]]) -> float:
),
("optional_union_parameter", ghostwriter.magic(optional_union_parameter)),
("union_sequence_parameter", ghostwriter.magic(union_sequence_parameter)),
pytest.param(
("sequence_from_collections", ghostwriter.magic(sequence_from_collections)),
marks=pytest.mark.skipif("sys.version_info[:2] < (3, 9)"),
),
pytest.param(
("add_custom_classes", ghostwriter.magic(add_custom_classes)),
marks=pytest.mark.skipif("sys.version_info[:2] < (3, 10)"),
Expand Down

0 comments on commit db3688f

Please sign in to comment.