inline-snapshot-numpy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- inline_snapshot_numpy/__init__.py +105 -0
- inline_snapshot_numpy-0.1.0.dist-info/METADATA +86 -0
- inline_snapshot_numpy-0.1.0.dist-info/RECORD +6 -0
- inline_snapshot_numpy-0.1.0.dist-info/WHEEL +4 -0
- inline_snapshot_numpy-0.1.0.dist-info/entry_points.txt +2 -0
- inline_snapshot_numpy-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import numpy
|
|
5
|
+
import numpy.testing
|
|
6
|
+
from inline_snapshot import snapshot, snapshot_arg
|
|
7
|
+
from inline_snapshot._snapshot.generic_value import GenericValue
|
|
8
|
+
from inline_snapshot.plugin import ImportFrom, customize
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
__all__ = (
|
|
13
|
+
"assert_allclose",
|
|
14
|
+
"assert_array_almost_equal",
|
|
15
|
+
"assert_array_almost_equal_nulp",
|
|
16
|
+
"assert_array_equal",
|
|
17
|
+
"assert_array_max_ulp",
|
|
18
|
+
"setup",
|
|
19
|
+
"snapshot",
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Array:
|
|
24
|
+
# Make NumPy defer ndarray comparisons to this wrapper.
|
|
25
|
+
__array_priority__ = float("inf")
|
|
26
|
+
|
|
27
|
+
def __init__(self, value, compare):
|
|
28
|
+
self.value = value
|
|
29
|
+
self.compare = compare
|
|
30
|
+
|
|
31
|
+
def __eq__(self, other):
|
|
32
|
+
if isinstance(other, GenericValue):
|
|
33
|
+
return NotImplemented
|
|
34
|
+
if isinstance(other, Array):
|
|
35
|
+
other = other.value
|
|
36
|
+
return self.compare(self.value, other)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@customize
|
|
40
|
+
def customize_array(value, builder):
|
|
41
|
+
if not isinstance(value, Array):
|
|
42
|
+
return None
|
|
43
|
+
|
|
44
|
+
array = numpy.asarray(value.value)
|
|
45
|
+
values = array.tolist()
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
inferred_dtype = numpy.asarray(values).dtype
|
|
49
|
+
except (TypeError, ValueError): # pragma: no cover - defensive for object arrays
|
|
50
|
+
inferred_dtype = None
|
|
51
|
+
|
|
52
|
+
kwargs = {}
|
|
53
|
+
if inferred_dtype != array.dtype:
|
|
54
|
+
kwargs["dtype"] = (
|
|
55
|
+
array.dtype.descr if array.dtype.names is not None else array.dtype.str
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
array_function = builder.create_code(
|
|
59
|
+
"array", imports=[ImportFrom("numpy", "array")]
|
|
60
|
+
)
|
|
61
|
+
return builder.create_call(array_function, [values], kwargs)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _make_assert_equal(assert_equal):
|
|
65
|
+
@wraps(assert_equal)
|
|
66
|
+
def result(actual, desired=..., *args, **kwargs):
|
|
67
|
+
error: Optional[Exception] = None
|
|
68
|
+
|
|
69
|
+
def compare(left, right):
|
|
70
|
+
nonlocal error
|
|
71
|
+
try:
|
|
72
|
+
assert_equal(left, right, *args, **kwargs)
|
|
73
|
+
except Exception as exception:
|
|
74
|
+
error = exception
|
|
75
|
+
return False
|
|
76
|
+
return True
|
|
77
|
+
|
|
78
|
+
if not isinstance(desired, GenericValue):
|
|
79
|
+
desired = Array(desired, compare)
|
|
80
|
+
desired = snapshot_arg(desired)
|
|
81
|
+
|
|
82
|
+
if not (Array(actual, compare) == desired):
|
|
83
|
+
assert error is not None
|
|
84
|
+
raise error
|
|
85
|
+
|
|
86
|
+
return result
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
assert_allclose = _make_assert_equal(numpy.testing.assert_allclose)
|
|
90
|
+
assert_array_almost_equal = _make_assert_equal(numpy.testing.assert_array_almost_equal)
|
|
91
|
+
assert_array_almost_equal_nulp = _make_assert_equal(
|
|
92
|
+
numpy.testing.assert_array_almost_equal_nulp
|
|
93
|
+
)
|
|
94
|
+
assert_array_equal = _make_assert_equal(numpy.testing.assert_array_equal)
|
|
95
|
+
assert_array_max_ulp = _make_assert_equal(numpy.testing.assert_array_max_ulp)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def setup():
|
|
99
|
+
"""Make NumPy's testing helpers accept inline snapshots."""
|
|
100
|
+
|
|
101
|
+
numpy.testing.assert_allclose = assert_allclose
|
|
102
|
+
numpy.testing.assert_array_almost_equal = assert_array_almost_equal
|
|
103
|
+
numpy.testing.assert_array_almost_equal_nulp = assert_array_almost_equal_nulp
|
|
104
|
+
numpy.testing.assert_array_equal = assert_array_equal
|
|
105
|
+
numpy.testing.assert_array_max_ulp = assert_array_max_ulp
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: inline-snapshot-numpy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: NumPy integration for inline-snapshot
|
|
5
|
+
Project-URL: Documentation, https://github.com/15r10nk/inline-snapshot-numpy#readme
|
|
6
|
+
Project-URL: Issues, https://github.com/15r10nk/inline-snapshot-numpy/issues
|
|
7
|
+
Project-URL: Source, https://github.com/15r10nk/inline-snapshot-numpy
|
|
8
|
+
Author-email: Frank Hoffmann <15r10nk@polarbit.de>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
20
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Requires-Dist: inline-snapshot>=0.36.1
|
|
24
|
+
Requires-Dist: numpy>=1.20; python_version < '3.15'
|
|
25
|
+
Requires-Dist: numpy>=2.5.4; python_version >= '3.15'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# inline-snapshot-numpy
|
|
29
|
+
|
|
30
|
+
[NumPy](https://numpy.org/) integration for
|
|
31
|
+
[inline-snapshot](https://github.com/15r10nk/inline-snapshot).
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install inline-snapshot-numpy
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
The package provides snapshot-aware versions of NumPy's array assertions:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from inline_snapshot_numpy import assert_array_equal
|
|
45
|
+
from numpy import array
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_array():
|
|
49
|
+
actual = array([[1, 2], [3, 4]])
|
|
50
|
+
assert_array_equal(actual)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Create the missing snapshot with:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pytest --inline-snapshot=create
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The test will become:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
def test_array():
|
|
63
|
+
actual = array([[1, 2], [3, 4]])
|
|
64
|
+
assert_array_equal(actual, desired=array([[1, 2], [3, 4]]))
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The following helpers are available: `assert_array_equal`, `assert_allclose`,
|
|
68
|
+
`assert_array_almost_equal`, `assert_array_almost_equal_nulp`,
|
|
69
|
+
and `assert_array_max_ulp`.
|
|
70
|
+
|
|
71
|
+
The expected array is managed through `snapshot_arg()`, so no explicit
|
|
72
|
+
`snapshot()` call or import is needed. You can still pass an explicit
|
|
73
|
+
`snapshot(...)` for compatibility.
|
|
74
|
+
|
|
75
|
+
Alternatively, call `setup()` from `conftest.py` to patch the corresponding
|
|
76
|
+
functions in `numpy.testing`:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from inline_snapshot_numpy import setup
|
|
80
|
+
|
|
81
|
+
setup()
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
Distributed under the terms of the MIT license.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
inline_snapshot_numpy/__init__.py,sha256=VKdxiDRuJLdvMW9ZWXGTP4RWQCMzmc3_u0zNQCIn384,3135
|
|
2
|
+
inline_snapshot_numpy-0.1.0.dist-info/METADATA,sha256=WxJJmEyScHi3tJltDmvR_TTrQgESRPYi51UbG1OCOt0,2509
|
|
3
|
+
inline_snapshot_numpy-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
4
|
+
inline_snapshot_numpy-0.1.0.dist-info/entry_points.txt,sha256=RWkqXB3hqUqPE5k8-DGmJgi1YALEDy-kGqgOnkBMkEQ,48
|
|
5
|
+
inline_snapshot_numpy-0.1.0.dist-info/licenses/LICENSE,sha256=zWVpwX42Okr-pdIa0l5cGUv5kQx1tpfTvaF06kZfVgY,1081
|
|
6
|
+
inline_snapshot_numpy-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Frank Hoffmann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|