-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
150 lines (122 loc) · 4.97 KB
/
conftest.py
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
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from typing import TYPE_CHECKING, Optional, Union
import numpy as np
import pytest
from syrupy.extensions.amber import AmberSnapshotExtension
if TYPE_CHECKING:
from syrupy.types import SerializableData, SerializedData, SnapshotIndex
DEFAULT_RTOL = 1.0e-03
DEFAULT_ATOL = 1.0e-03
class Approx:
"""
Wrapper object for approximately compared numpy arrays.
"""
def __init__(
self,
data: Union[np.ndarray, list],
*,
rtol: Optional[float] = None,
atol: Optional[float] = None,
):
if isinstance(data, list):
self.data = np.array(data)
elif isinstance(data, np.ndarray):
self.data = data
else:
raise TypeError(f"Cannot convert {type(data)} to np.array")
self.rtol = rtol if rtol is not None else DEFAULT_RTOL
self.atol = atol if atol is not None else DEFAULT_ATOL
self.tol_repr = True
def __repr__(self):
data = np.array_repr(self.data)
data = "\n".join(f"\t{line}" for line in data.splitlines())
tol_repr = ""
if self.tol_repr:
tol_repr = f", \n\trtol={self.rtol}, \n\tatol={self.atol}"
return f"Approx(\n{data}{tol_repr}\n)"
class _ApproxNumpyFormatter:
def __init__(self, data):
self.data = data
def __repr__(self):
return Approx(
self.data.expected,
rtol=self.data.rel,
atol=self.data.abs,
).__repr__()
def _try_parse_approx(data: "SerializableData"):
"""
Parse the string representation of an Approx object.
We can just use eval here, since we know the string is safe.
"""
if not isinstance(data, str):
return None
data = data.strip()
if not data.startswith("Approx("):
return None
approx = eval(
data.replace("dtype=", "dtype=np."),
{"Approx": Approx, "np": np},
{"array": np.array},
)
if not isinstance(approx, Approx):
return None
return approx
class ApproxExtension(AmberSnapshotExtension):
"""
By default, syrupy uses the __repr__ of the expected (snapshot) and actual values
to serialize them into strings. Then, it compares the strings to see if they match.
However, this behavior is not ideal for comparing floats/ndarrays. For example,
if we have a snapshot with a float value of 0.1, and the actual value is 0.10000000000000001,
then the strings will not match, even though the values are effectively equal.
To work around this, we override the serialize method to seralize the expected value
into a special representation. Then, we override the matches function (which originally does a
simple string comparison) to parse the expected and actual values into numpy arrays.
Finally, we compare the arrays using np.allclose.
"""
def matches(
self,
*,
serialized_data: "SerializableData",
snapshot_data: "SerializableData",
) -> bool:
# if both serialized_data and snapshot_data are serialized Approx objects,
# then we can load them as numpy arrays and compare them using np.allclose
serialized_approx = _try_parse_approx(serialized_data)
snapshot_approx = _try_parse_approx(snapshot_data)
if serialized_approx is not None and snapshot_approx is not None:
return np.allclose(
snapshot_approx.data,
serialized_approx.data,
rtol=serialized_approx.rtol,
atol=serialized_approx.atol,
)
return super().matches(
serialized_data=serialized_data, snapshot_data=snapshot_data
)
def serialize(self, data, **kwargs):
# we override the existing serialization behavior
# of the `pytest.approx()` object to serialize it into a special string.
if isinstance(data, type(pytest.approx(np.array(0.0)))):
return super().serialize(_ApproxNumpyFormatter(data), **kwargs)
elif isinstance(data, type(pytest.approx(0.0))):
raise NotImplementedError("Scalar approx not implemented yet")
return super().serialize(data, **kwargs)
def write_snapshot(
self, *, data: "SerializedData", index: "SnapshotIndex"
) -> None:
# Right before writing to file, we update the serialized snapshot data
# and remove the atol/rtol from the string representation.
# This is an implementation detail, and is not necessary for the extension to work.
# It just makes the snapshot files a bit cleaner.
approx = _try_parse_approx(data)
if approx is not None:
approx.tol_repr = False
data = self.serialize(approx)
return super().write_snapshot(data=data, index=index)
@pytest.fixture
def snapshot(snapshot):
return snapshot.use_extension(ApproxExtension)