• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

p2p-ld / numpydantic / 11289913767

11 Oct 2024 09:29AM UTC coverage: 98.351% (-0.4%) from 98.757%
11289913767

Pull #31

github

web-flow
Merge 8f60c60e8 into 69dbe3955
Pull Request #31: [tests] `numpydantic.testing` - exposing helpers for 3rd-party interface development & combinatoric testing

313 of 317 new or added lines in 10 files covered. (98.74%)

9 existing lines in 3 files now uncovered.

1491 of 1516 relevant lines covered (98.35%)

9.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.04
/src/numpydantic/interface/numpy.py
1
"""
2
Interface to numpy arrays
3
"""
4

5
from typing import Any, Literal, Union
10✔
6

7
from pydantic import BaseModel, SerializationInfo
10✔
8

9
from numpydantic.interface.interface import Interface, JsonDict
10✔
10

11
try:
10✔
12
    import numpy as np
10✔
13
    from numpy import ndarray
10✔
14

15
    ENABLED = True
10✔
16

17
except ImportError:  # pragma: no cover
18
    ENABLED = False
19
    ndarray = None
20
    np = None
21

22

23
class NumpyJsonDict(JsonDict):
10✔
24
    """
25
    JSON-able roundtrip representation of numpy array
26
    """
27

28
    type: Literal["numpy"]
10✔
29
    dtype: str
10✔
30
    value: list
10✔
31

32
    def to_array_input(self) -> ndarray:
10✔
33
        """
34
        Construct a numpy array
35
        """
36
        return np.array(self.value, dtype=self.dtype)
10✔
37

38

39
class NumpyInterface(Interface):
10✔
40
    """
41
    Numpy :class:`~numpy.ndarray` s!
42
    """
43

44
    name = "numpy"
10✔
45
    input_types = (ndarray, list)
10✔
46
    return_type = ndarray
10✔
47
    json_model = NumpyJsonDict
10✔
48
    priority = -999
10✔
49
    """
6✔
50
    The numpy interface is usually the interface of last resort.
51
    We want to use any more specific interface that we might have,
52
    because the numpy interface checks for anything that could be coerced
53
    to a numpy array (see :meth:`.NumpyInterface.check` )
54
    """
55

56
    @classmethod
10✔
57
    def check(cls, array: Any) -> bool:
10✔
58
        """
59
        Check that this is in fact a numpy ndarray or something that can be
60
        coerced to one
61
        """
62
        if array is None:
10✔
NEW
63
            return False
×
64

65
        if isinstance(array, ndarray):
10✔
66
            return True
10✔
67
        elif isinstance(array, dict):
10✔
68
            return NumpyJsonDict.is_valid(array)
10✔
69
        else:
70
            try:
10✔
71
                _ = np.array(array)
10✔
72
                return True
10✔
73
            except Exception:
10✔
74
                return False
10✔
75

76
    def before_validation(self, array: Any) -> ndarray:
10✔
77
        """
78
        Coerce to an ndarray. We have already checked if coercion is possible
79
        in :meth:`.check`
80
        """
81
        if not isinstance(array, ndarray):
10✔
82
            array = np.array(array)
10✔
83

84
        try:
10✔
85
            if issubclass(self.dtype, BaseModel) and isinstance(array.flat[0], dict):
10✔
86
                array = np.vectorize(lambda x: self.dtype(**x))(array)
10✔
87
        except TypeError:
10✔
88
            # fine, dtype isn't a type
89
            pass
10✔
90

91
        return array
10✔
92

93
    @classmethod
10✔
94
    def enabled(cls) -> bool:
10✔
95
        """Check that numpy is present in the environment"""
96
        return ENABLED
10✔
97

98
    @classmethod
10✔
99
    def to_json(
10✔
100
        cls, array: ndarray, info: SerializationInfo = None
101
    ) -> Union[list, JsonDict]:
102
        """
103
        Convert an array of :attr:`.return_type` to a JSON-compatible format using
104
        base python types
105
        """
106
        if not isinstance(array, np.ndarray):  # pragma: no cover
107
            array = np.array(array)
108

109
        json_array = array.tolist()
10✔
110

111
        if info.round_trip:
10✔
112
            json_array = NumpyJsonDict(
10✔
113
                type=cls.name, dtype=str(array.dtype), value=json_array
114
            )
115
        return json_array
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc