File: test_expected_output.py

package info (click to toggle)
python-hypothesis 6.138.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,272 kB
  • sloc: python: 62,853; ruby: 1,107; sh: 253; makefile: 41; javascript: 6
file content (362 lines) | stat: -rw-r--r-- 11,956 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

"""
'Golden master' tests for the ghostwriter.

To update the recorded outputs, run `pytest --hypothesis-update-outputs ...`.
"""

import ast
import base64
import builtins
import collections.abc
import operator
import pathlib
import re
import subprocess
import sys
from collections.abc import Sequence
from typing import Optional, Union

import black
import numpy
import numpy.typing
import pytest
from example_code.future_annotations import (
    add_custom_classes,
    invalid_types,
    merge_dicts,
)

import hypothesis
from hypothesis import settings
from hypothesis.extra import ghostwriter
from hypothesis.utils.conventions import not_set

pytestmark = pytest.mark.skipif(
    settings._current_profile == "threading",
    reason="ghostwriter is not thread safe",
)


@pytest.fixture
def update_recorded_outputs(request):
    return request.config.getoption("--hypothesis-update-outputs")


def get_recorded(name, actual=""):
    file_ = pathlib.Path(__file__).parent / "recorded" / f"{name}.txt"
    if actual:
        file_.write_text(actual, encoding="utf-8")
    return file_.read_text(encoding="utf-8")


def timsort(seq: Sequence[int]) -> Sequence[int]:
    return sorted(seq)


def with_docstring(a, b, c, d=int, e=lambda x: f"xx{x}xx") -> None:
    """Demonstrates parsing params from the docstring

    :param a: sphinx docstring style
    :type a: sequence of integers

    b (list, tuple, or None): Google docstring style

    c : {"foo", "bar", or None}
        Numpy docstring style
    """


class A_Class:
    @classmethod
    def a_classmethod(cls, arg: int):
        pass

    @staticmethod
    def a_staticmethod(arg: int):
        pass


def add(a: float, b: float) -> float:
    return a + b


def divide(a: int, b: int) -> float:
    """This is a RST-style docstring for `divide`.

    :raises ZeroDivisionError: if b == 0
    """
    return a / b


def optional_parameter(a: float, b: Optional[float]) -> float:
    return optional_union_parameter(a, b)


def optional_union_parameter(a: float, b: Optional[Union[float, int]]) -> float:
    return a if b is None else a + b


if sys.version_info[:2] >= (3, 10):

    def union_sequence_parameter(items: Sequence[float | int]) -> float:
        return sum(items)

else:

    def union_sequence_parameter(items: Sequence[Union[float, int]]) -> float:
        return sum(items)


def sequence_from_collections(items: collections.abc.Sequence[int]) -> int:
    return min(items)


if sys.version_info[:2] >= (3, 10):

    def various_numpy_annotations(
        f: numpy.typing.NDArray[numpy.float64],
        fc: numpy.typing.NDArray[numpy.float64 | numpy.complex128],
        union: numpy.typing.NDArray[numpy.float64 | numpy.complex128] | None,
    ):
        pass

else:
    various_numpy_annotations = add


# Note: for some of the `expected` outputs, we replace away some small
#       parts which vary between minor versions of Python.
@pytest.mark.parametrize(
    "data",
    [
        ("fuzz_sorted", lambda: ghostwriter.fuzz(sorted)),
        (
            "fuzz_sorted_with_annotations",
            lambda: ghostwriter.fuzz(sorted, annotate=True),
        ),
        ("fuzz_with_docstring", lambda: ghostwriter.fuzz(with_docstring)),
        ("fuzz_classmethod", lambda: ghostwriter.fuzz(A_Class.a_classmethod)),
        ("fuzz_staticmethod", lambda: ghostwriter.fuzz(A_Class.a_staticmethod)),
        ("fuzz_ufunc", lambda: ghostwriter.fuzz(numpy.add)),
        ("magic_gufunc", lambda: ghostwriter.magic(numpy.matmul)),
        ("optional_parameter", lambda: ghostwriter.magic(optional_parameter)),
        (
            "optional_union_parameter",
            lambda: ghostwriter.magic(optional_union_parameter),
        ),
        (
            "union_sequence_parameter",
            lambda: ghostwriter.magic(union_sequence_parameter),
        ),
        (
            "sequence_from_collections",
            lambda: ghostwriter.magic(sequence_from_collections),
        ),
        pytest.param(
            ("add_custom_classes", lambda: ghostwriter.magic(add_custom_classes)),
            marks=pytest.mark.skipif("sys.version_info[:2] < (3, 10)"),
        ),
        pytest.param(
            ("merge_dicts", lambda: ghostwriter.magic(merge_dicts)),
            marks=pytest.mark.skipif("sys.version_info[:2] < (3, 10)"),
        ),
        pytest.param(
            ("invalid_types", lambda: ghostwriter.magic(invalid_types)),
            marks=pytest.mark.skipif("sys.version_info[:2] < (3, 10)"),
        ),
        ("magic_base64_roundtrip", lambda: ghostwriter.magic(base64.b64encode)),
        (
            "magic_base64_roundtrip_with_annotations",
            lambda: ghostwriter.magic(base64.b64encode, annotate=True),
        ),
        ("re_compile", lambda: ghostwriter.fuzz(re.compile)),
        (
            "re_compile_except",
            lambda: ghostwriter.fuzz(re.compile, except_=re.error).replace(
                "re.PatternError", "re.error"  # changed in Python 3.13
            ),
        ),
        ("re_compile_unittest", lambda: ghostwriter.fuzz(re.compile, style="unittest")),
        pytest.param(
            ("base64_magic", lambda: ghostwriter.magic(base64)),
            marks=pytest.mark.skipif("sys.version_info[:2] >= (3, 10)"),
        ),
        ("sorted_idempotent", lambda: ghostwriter.idempotent(sorted)),
        ("timsort_idempotent", lambda: ghostwriter.idempotent(timsort)),
        (
            "timsort_idempotent_asserts",
            lambda: ghostwriter.idempotent(timsort, except_=AssertionError),
        ),
        pytest.param(
            ("eval_equivalent", lambda: ghostwriter.equivalent(eval, ast.literal_eval)),
            marks=[pytest.mark.skipif(sys.version_info[:2] >= (3, 13), reason="kw")],
        ),
        (
            "sorted_self_equivalent",
            lambda: ghostwriter.equivalent(sorted, sorted, sorted),
        ),
        (
            "sorted_self_equivalent_with_annotations",
            lambda: ghostwriter.equivalent(sorted, sorted, sorted, annotate=True),
        ),
        ("addition_op_magic", lambda: ghostwriter.magic(add)),
        ("multiplication_magic", lambda: ghostwriter.magic(operator.mul)),
        ("matmul_magic", lambda: ghostwriter.magic(operator.matmul)),
        (
            "addition_op_multimagic",
            lambda: ghostwriter.magic(add, operator.add, numpy.add),
        ),
        ("division_fuzz_error_handler", lambda: ghostwriter.fuzz(divide)),
        (
            "division_binop_error_handler",
            lambda: ghostwriter.binary_operation(divide, identity=1),
        ),
        (
            "division_roundtrip_error_handler",
            lambda: ghostwriter.roundtrip(divide, operator.mul),
        ),
        (
            "division_roundtrip_error_handler_without_annotations",
            lambda: ghostwriter.roundtrip(divide, operator.mul, annotate=False),
        ),
        (
            "division_roundtrip_arithmeticerror_handler",
            lambda: ghostwriter.roundtrip(
                divide, operator.mul, except_=ArithmeticError
            ),
        ),
        (
            "division_roundtrip_typeerror_handler",
            lambda: ghostwriter.roundtrip(divide, operator.mul, except_=TypeError),
        ),
        (
            "division_operator",
            lambda: ghostwriter.binary_operation(
                operator.truediv, associative=False, commutative=False
            ),
        ),
        (
            "division_operator_with_annotations",
            lambda: ghostwriter.binary_operation(
                operator.truediv, associative=False, commutative=False, annotate=True
            ),
        ),
        (
            "multiplication_operator",
            lambda: ghostwriter.binary_operation(
                operator.mul, identity=1, distributes_over=operator.add
            ),
        ),
        (
            "multiplication_operator_unittest",
            lambda: ghostwriter.binary_operation(
                operator.mul,
                identity=1,
                distributes_over=operator.add,
                style="unittest",
            ),
        ),
        (
            "sorted_self_error_equivalent_simple",
            lambda: ghostwriter.equivalent(sorted, sorted, allow_same_errors=True),
        ),
        (
            "sorted_self_error_equivalent_threefuncs",
            lambda: ghostwriter.equivalent(
                sorted, sorted, sorted, allow_same_errors=True
            ),
        ),
        (
            "sorted_self_error_equivalent_1error",
            lambda: ghostwriter.equivalent(
                sorted,
                sorted,
                allow_same_errors=True,
                except_=ValueError,
            ),
        ),
        (
            "sorted_self_error_equivalent_2error_unittest",
            lambda: ghostwriter.equivalent(
                sorted,
                sorted,
                allow_same_errors=True,
                except_=(TypeError, ValueError),
                style="unittest",
            ),
        ),
        ("magic_class", lambda: ghostwriter.magic(A_Class)),
        pytest.param(
            ("magic_builtins", lambda: ghostwriter.magic(builtins)),
            marks=[
                pytest.mark.skipif(
                    sys.version_info[:2] != (3, 10),
                    reason="often small changes",
                )
            ],
        ),
        pytest.param(
            (
                "magic_numpy",
                lambda: ghostwriter.magic(various_numpy_annotations, annotate=False),
            ),
            marks=pytest.mark.skipif(various_numpy_annotations is add, reason="<=3.9"),
        ),
    ],
    ids=lambda x: x[0],
)
def test_ghostwriter_example_outputs(update_recorded_outputs, data):
    name, get_actual = data
    # ghostwriter computations can be expensive, so defer collection-time
    # computations until test-time
    actual = get_actual()
    expected = get_recorded(name, actual * update_recorded_outputs)
    assert actual == expected  # We got the expected source code
    exec(expected, {})  # and there are no SyntaxError or NameErrors


def test_ghostwriter_on_hypothesis(update_recorded_outputs):
    actual = (
        ghostwriter.magic(hypothesis)
        .replace("Strategy[+Ex]", "Strategy")
        .replace("hypothesis._settings.settings", "hypothesis.settings")
    )
    # hypothesis._settings.settings wraps the line before replacement, and doesn't
    # after replacement
    actual = black.format_str(actual, mode=black.FileMode())
    expected = get_recorded("hypothesis_module_magic", actual * update_recorded_outputs)
    if sys.version_info[:2] == (3, 10):
        assert actual == expected
    exec(expected, {"not_set": not_set})


def test_ghostwriter_suggests_submodules_for_empty_toplevel(
    tmp_path, update_recorded_outputs
):
    foo = tmp_path / "foo"
    foo.mkdir()
    (foo / "__init__.py").write_text("from . import bar\n", encoding="utf-8")
    (foo / "bar.py").write_text("def baz(x: int): ...\n", encoding="utf-8")

    proc = subprocess.run(
        ["hypothesis", "write", "foo"],
        check=True,
        capture_output=True,
        encoding="utf-8",
        cwd=tmp_path,
    )
    actual = proc.stdout.replace(re.search(r"from '(.+)foo/", proc.stdout).group(1), "")

    expected = get_recorded("nothing_found", actual * update_recorded_outputs)
    assert actual == expected  # We got the expected source code
    exec(expected, {})  # and there are no SyntaxError or NameErrors