File: errors.py

package info (click to toggle)
pint-xarray 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,244 kB
  • sloc: python: 5,043; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,533 bytes parent folder | download
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
from collections.abc import Hashable
from typing import Any


class PintExceptionGroup(ExceptionGroup, ValueError):
    """Exception group for errors related to unit operations

    Raised whenever there's the possibility of multiple errors.
    """

    pass


def _add_note(e: Exception, note: str) -> Exception:
    e.add_note(note)

    return e


def create_exception_group(mapping: dict[Hashable, Any], op: str) -> ExceptionGroup:
    match op:
        case "attach":
            message = "Cannot attach units"
            errors = [
                _add_note(e, f"cannot attach units to variable {key!r}: {unit}")
                for key, (unit, e) in mapping.items()
            ]
        case "parse":
            message = "Cannot parse units"
            errors = [
                _add_note(e, f"invalid units for variable {key!r}: {unit} ({type})")
                for key, (unit, type, e) in mapping.items()
            ]
        case "convert":
            message = "Cannot convert variables"
            errors = [
                _add_note(e, f"incompatible units for variable {key!r}")
                for key, e in mapping.items()
            ]
        case "convert_indexers":
            message = "Cannot convert indexers"
            errors = [
                _add_note(e, f"incompatible units for indexer for {key!r}")
                for key, e in mapping.items()
            ]
        case _:  # pragma: no cover
            raise ValueError("invalid op")

    return PintExceptionGroup(message, errors)