| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import math |
| 12 |
from collections import defaultdict, namedtuple |
| 13 |
from dataclasses import dataclass |
| 14 |
from functools import partial |
| 15 |
from inspect import Parameter, Signature, signature |
| 16 |
from typing import ForwardRef, Union |
| 17 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
from hypothesis.internal.compat import ( |
| 21 |
add_note, |
| 22 |
ceil, |
| 23 |
dataclass_asdict, |
| 24 |
extract_bits, |
| 25 |
floor, |
| 26 |
get_type_hints, |
| 27 |
) |
| 28 |
|
| 29 |
floor_ceil_values = [ |
| 30 |
-10.7, |
| 31 |
-10.3, |
| 32 |
-0.5, |
| 33 |
-0.0, |
| 34 |
0, |
| 35 |
0.5, |
| 36 |
10.3, |
| 37 |
10.7, |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize("value", floor_ceil_values) |
| 42 |
def test_our_floor_agrees_with_math_floor(value): |
| 43 |
assert floor(value) == math.floor(value) |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.parametrize("value", floor_ceil_values) |
| 47 |
def test_our_ceil_agrees_with_math_ceil(value): |
| 48 |
assert ceil(value) == math.ceil(value) |
| 49 |
|
| 50 |
|
| 51 |
class WeirdSig: |
| 52 |
__signature__ = Signature( |
| 53 |
parameters=[Parameter(name="args", kind=Parameter.VAR_POSITIONAL)] |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def test_no_type_hints(): |
| 58 |
assert get_type_hints(WeirdSig) == {} |
| 59 |
|
| 60 |
|
| 61 |
@dataclass |
| 62 |
class Foo: |
| 63 |
x: "Foo" = None # type: ignore |
| 64 |
|
| 65 |
|
| 66 |
Foo.__signature__ = signature(Foo).replace( # type: ignore |
| 67 |
parameters=[ |
| 68 |
Parameter( |
| 69 |
"x", |
| 70 |
Parameter.POSITIONAL_OR_KEYWORD, |
| 71 |
annotation=ForwardRef("Foo"), |
| 72 |
default=None, |
| 73 |
) |
| 74 |
] |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
@dataclass |
| 79 |
class Bar: |
| 80 |
x: Union[int, "Bar"] | None |
| 81 |
|
| 82 |
|
| 83 |
Bar.__signature__ = signature(Bar).replace( # type: ignore |
| 84 |
parameters=[ |
| 85 |
Parameter( |
| 86 |
"x", |
| 87 |
Parameter.POSITIONAL_OR_KEYWORD, |
| 88 |
# ruff reports a false-positive UP007 here, since int | ForwardRef("Bar") |
| 89 |
# errors on 3.10. We can change this once we drop 3.10. |
| 90 |
# |
| 91 |
# see also https://github.com/astral-sh/ruff/issues/20883 |
| 92 |
annotation=Union[int, ForwardRef("Bar"), None], # type: ignore # noqa: UP007 |
| 93 |
) |
| 94 |
] |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.parametrize("obj,expected", [(Foo, Foo | None), (Bar, int | Bar | None)]) |
| 99 |
def test_resolve_fwd_refs(obj, expected): |
| 100 |
# See: https://github.com/HypothesisWorks/hypothesis/issues/3519 |
| 101 |
assert get_type_hints(obj)["x"] == expected |
| 102 |
|
| 103 |
|
| 104 |
def func(a, b: int, *c: str, d: int | None = None): |
| 105 |
pass |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize( |
| 109 |
"pf, names", |
| 110 |
[ |
| 111 |
(partial(func, 1), "b c d"), |
| 112 |
(partial(func, 1, 2), "c d"), |
| 113 |
(partial(func, 1, 2, 3, 4, 5), "c d"), # varargs don't fill |
| 114 |
(partial(func, 1, 2, 3, d=4), "c d"), # kwonly args just get new defaults |
| 115 |
], |
| 116 |
) |
| 117 |
def test_get_hints_through_partial(pf, names): |
| 118 |
assert set(get_type_hints(pf)) == set(names.split()) |
| 119 |
|
| 120 |
|
| 121 |
@dataclass |
| 122 |
class FilledWithStuff: |
| 123 |
a: list |
| 124 |
b: tuple |
| 125 |
c: namedtuple |
| 126 |
d: dict |
| 127 |
e: defaultdict |
| 128 |
|
| 129 |
|
| 130 |
def test_dataclass_asdict(): |
| 131 |
ANamedTuple = namedtuple("ANamedTuple", ("with_some_field")) |
| 132 |
e = defaultdict(list) |
| 133 |
e["a"].append(1) |
| 134 |
obj = FilledWithStuff(a=[1], b=(2), c=ANamedTuple(3), d={4: 5}, e=e) |
| 135 |
assert dataclass_asdict(obj) == { |
| 136 |
"a": [1], |
| 137 |
"b": (2), |
| 138 |
"c": ANamedTuple(3), |
| 139 |
"d": {4: 5}, |
| 140 |
"e": {"a": [1]}, |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.parametrize("width", [None, 8]) |
| 145 |
@pytest.mark.parametrize("x", [0, 2, 123]) |
| 146 |
def test_extract_bits_roundtrip(width, x): |
| 147 |
bits = extract_bits(x, width=width) |
| 148 |
if width is not None: |
| 149 |
assert len(bits) == width |
| 150 |
assert x == sum(v << p for p, v in enumerate(reversed(bits))) |
| 151 |
|
| 152 |
|
| 153 |
@dataclass(frozen=True, slots=False) |
| 154 |
class ImmutableError: |
| 155 |
msg: str |
| 156 |
|
| 157 |
|
| 158 |
def test_add_note_fails_gracefully_on_frozen_instance(): |
| 159 |
add_note(ImmutableError("msg"), "some note") |