File: test_compat.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 (157 lines) | stat: -rw-r--r-- 3,687 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
# 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/.

import math
from collections import defaultdict, namedtuple
from dataclasses import dataclass
from functools import partial
from inspect import Parameter, Signature, signature
from typing import ForwardRef, Optional, Union

import pytest

from hypothesis.internal.compat import (
    add_note,
    ceil,
    dataclass_asdict,
    extract_bits,
    floor,
    get_type_hints,
)

floor_ceil_values = [
    -10.7,
    -10.3,
    -0.5,
    -0.0,
    0,
    0.5,
    10.3,
    10.7,
]


@pytest.mark.parametrize("value", floor_ceil_values)
def test_our_floor_agrees_with_math_floor(value):
    assert floor(value) == math.floor(value)


@pytest.mark.parametrize("value", floor_ceil_values)
def test_our_ceil_agrees_with_math_ceil(value):
    assert ceil(value) == math.ceil(value)


class WeirdSig:
    __signature__ = Signature(
        parameters=[Parameter(name="args", kind=Parameter.VAR_POSITIONAL)]
    )


def test_no_type_hints():
    assert get_type_hints(WeirdSig) == {}


@dataclass
class Foo:
    x: "Foo" = None  # type: ignore


Foo.__signature__ = signature(Foo).replace(  # type: ignore
    parameters=[
        Parameter(
            "x",
            Parameter.POSITIONAL_OR_KEYWORD,
            annotation=ForwardRef("Foo"),
            default=None,
        )
    ]
)


@dataclass
class Bar:
    x: Optional[Union[int, "Bar"]]


Bar.__signature__ = signature(Bar).replace(  # type: ignore
    parameters=[
        Parameter(
            "x",
            Parameter.POSITIONAL_OR_KEYWORD,
            annotation=Optional[Union[int, ForwardRef("Bar")]],  # type: ignore
        )
    ]
)


@pytest.mark.parametrize(
    "obj,expected", [(Foo, Optional[Foo]), (Bar, Optional[Union[int, Bar]])]
)
def test_resolve_fwd_refs(obj, expected):
    # See: https://github.com/HypothesisWorks/hypothesis/issues/3519
    assert get_type_hints(obj)["x"] == expected


def func(a, b: int, *c: str, d: Optional[int] = None):
    pass


@pytest.mark.parametrize(
    "pf, names",
    [
        (partial(func, 1), "b c d"),
        (partial(func, 1, 2), "c d"),
        (partial(func, 1, 2, 3, 4, 5), "c d"),  # varargs don't fill
        (partial(func, 1, 2, 3, d=4), "c d"),  # kwonly args just get new defaults
    ],
)
def test_get_hints_through_partial(pf, names):
    assert set(get_type_hints(pf)) == set(names.split())


@dataclass
class FilledWithStuff:
    a: list
    b: tuple
    c: namedtuple
    d: dict
    e: defaultdict


def test_dataclass_asdict():
    ANamedTuple = namedtuple("ANamedTuple", ("with_some_field"))
    e = defaultdict(list)
    e["a"].append(1)
    obj = FilledWithStuff(a=[1], b=(2), c=ANamedTuple(3), d={4: 5}, e=e)
    assert dataclass_asdict(obj) == {
        "a": [1],
        "b": (2),
        "c": ANamedTuple(3),
        "d": {4: 5},
        "e": {"a": [1]},
    }


@pytest.mark.parametrize("width", [None, 8])
@pytest.mark.parametrize("x", [0, 2, 123])
def test_extract_bits_roundtrip(width, x):
    bits = extract_bits(x, width=width)
    if width is not None:
        assert len(bits) == width
    assert x == sum(v << p for p, v in enumerate(reversed(bits)))


@dataclass(frozen=True)
class ImmutableError:
    msg: str


def test_add_note_fails_gracefully_on_frozen_instance():
    add_note(ImmutableError("msg"), "some note")