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

p2p-ld / numpydantic / 10382572278

14 Aug 2024 06:29AM CUT coverage: 98.177% (+0.004%) from 98.173%
10382572278

push

github

web-flow
Merge pull request #8 from p2p-ld/constructable

Make NDArray callable as a functional validator

3 of 3 new or added lines in 1 file covered. (100.0%)

808 of 823 relevant lines covered (98.18%)

9.75 hits per line

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

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

5
from typing import Any
10✔
6

7
from numpydantic.interface.interface import Interface
10✔
8

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

13
    ENABLED = True
10✔
14

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

20

21
class NumpyInterface(Interface):
10✔
22
    """
23
    Numpy :class:`~numpy.ndarray` s!
24
    """
25

26
    input_types = (ndarray, list)
10✔
27
    return_type = ndarray
10✔
28
    priority = -999
10✔
29
    """
6✔
30
    The numpy interface is usually the interface of last resort.
31
    We want to use any more specific interface that we might have,
32
    because the numpy interface checks for anything that could be coerced
33
    to a numpy array (see :meth:`.NumpyInterface.check` )
34
    """
35

36
    @classmethod
10✔
37
    def check(cls, array: Any) -> bool:
10✔
38
        """
39
        Check that this is in fact a numpy ndarray or something that can be
40
        coerced to one
41
        """
42
        if isinstance(array, ndarray):
10✔
43
            return True
10✔
44
        else:
45
            try:
10✔
46
                _ = np.array(array)
10✔
47
                return True
10✔
48
            except Exception:
10✔
49
                return False
10✔
50

51
    def before_validation(self, array: Any) -> ndarray:
10✔
52
        """
53
        Coerce to an ndarray. We have already checked if coercion is possible
54
        in :meth:`.check`
55
        """
56
        if not isinstance(array, ndarray):
10✔
57
            array = np.array(array)
10✔
58
        return array
10✔
59

60
    @classmethod
10✔
61
    def enabled(cls) -> bool:
10✔
62
        """Check that numpy is present in the environment"""
63
        return ENABLED
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