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

p2p-ld / numpydantic / 24373313656

14 Apr 2026 12:00AM UTC coverage: 97.821% (-0.5%) from 98.345%
24373313656

push

github

web-flow
Merge pull request #67 from p2p-ld/ndarray-schema-proxies

allow proxies to be used with ndarray schema as input

15 of 17 new or added lines in 4 files covered. (88.24%)

23 existing lines in 11 files now uncovered.

1571 of 1606 relevant lines covered (97.82%)

9.78 hits per line

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

95.45
/src/numpydantic/validation/dtype.py
1
"""
2
Helper functions for validation of dtype.
3

4
For literal dtypes intended for use by end-users, see :mod:`numpydantic.dtype`
5
"""
6

7
from types import UnionType
10✔
8
from typing import Any, Union, get_args, get_origin
10✔
9

10
import numpy as np
10✔
11

12
from numpydantic.types import DtypeType
10✔
13

14

15
def validate_dtype(dtype: Any, target: DtypeType) -> bool:
10✔
16
    """
17
    Validate a dtype against the target dtype.
18

19
    If `dtype` or `target` are `Any`, validation passes trivially.
20
    The `dtype` may be `Any` when the dtype can't be determined,
21
    but failure to determine dtype shouldn't be fatal
22
    (e.g. BaseModel dtypes for empty arrays).
23

24
    Args:
25
        dtype: The dtype to validate
26
        target (:class:`.DtypeType`): The target dtype
27

28
    Returns:
29
        bool: ``True`` if valid, ``False`` otherwise
30
    """
31
    if target is Any or dtype is Any:
10✔
32
        return True
10✔
33

34
    if isinstance(target, tuple):
10✔
35
        valid = any(validate_dtype(dtype, target_dt) for target_dt in target)
10✔
36
    elif is_union(target):
10✔
37
        valid = any(
10✔
38
            [validate_dtype(dtype, target_dt) for target_dt in get_args(target)]
39
        )
40
    elif target is np.str_:
10✔
41
        valid = getattr(dtype, "type", None) in (np.str_, str) or dtype in (
10✔
42
            np.str_,
43
            str,
44
        )
45
    else:
46
        # try to match as any subclass, if target is a class
47
        try:
10✔
48
            valid = issubclass(dtype, target)
10✔
49
        except TypeError:
10✔
50
            # error expected if dtype or target is not a class
51
            # main type check - directly test dtype identity
52
            valid = dtype == target or getattr(dtype, "type", None) == target
10✔
53

54
    return valid
10✔
55

56

57
def is_union(dtype: DtypeType) -> bool:
10✔
58
    """
59
    Check if a dtype is a union
60
    """
61
    if UnionType is None:
10✔
UNCOV
62
        return get_origin(dtype) is Union
×
63
    else:
64
        return get_origin(dtype) in (Union, UnionType)
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

© 2026 Coveralls, Inc