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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
from __future__ import annotations
import pickle
import pytest
from pint import (
DefinitionSyntaxError,
DimensionalityError,
LogarithmicUnitCalculusError,
OffsetUnitCalculusError,
PintError,
Quantity,
RedefinitionError,
UndefinedUnitError,
UnitRegistry,
)
from pint.errors import LOG_ERROR_DOCS_HTML, OFFSET_ERROR_DOCS_HTML
class TestErrors:
def test_definition_syntax_error(self):
ex = DefinitionSyntaxError("foo")
assert str(ex) == "foo"
def test_redefinition_error(self):
ex = RedefinitionError("foo", "bar")
assert str(ex) == "Cannot redefine 'foo' (bar)"
with pytest.raises(PintError):
raise ex
def test_undefined_unit_error(self):
x = ("meter",)
msg = "'meter' is not defined in the unit registry"
ex = UndefinedUnitError(x)
assert str(ex) == msg
ex = UndefinedUnitError(list(x))
assert str(ex) == msg
ex = UndefinedUnitError(set(x))
assert str(ex) == msg
with pytest.raises(PintError):
raise ex
def test_undefined_unit_error_multi(self):
x = ("meter", "kg")
msg = "('meter', 'kg') are not defined in the unit registry"
ex = UndefinedUnitError(x)
assert str(ex) == msg
ex = UndefinedUnitError(list(x))
assert str(ex) == msg
with pytest.raises(PintError):
raise ex
def test_dimensionality_error(self):
ex = DimensionalityError("a", "b")
assert str(ex) == "Cannot convert from 'a' to 'b'"
ex = DimensionalityError("a", "b", "c")
assert str(ex) == "Cannot convert from 'a' (c) to 'b' ()"
ex = DimensionalityError("a", "b", "c", "d", extra_msg=": msg")
assert str(ex) == "Cannot convert from 'a' (c) to 'b' (d): msg"
with pytest.raises(PintError):
raise ex
def test_offset_unit_calculus_error(self):
ex = OffsetUnitCalculusError(Quantity("1 kg")._units)
assert (
str(ex)
== "Ambiguous operation with offset unit (kilogram). See "
+ OFFSET_ERROR_DOCS_HTML
+ " for guidance."
)
ex = OffsetUnitCalculusError(Quantity("1 kg")._units, Quantity("1 s")._units)
assert (
str(ex)
== "Ambiguous operation with offset unit (kilogram, second). See "
+ OFFSET_ERROR_DOCS_HTML
+ " for guidance."
)
with pytest.raises(PintError):
raise ex
def test_logarithmic_unit_calculus_error(self):
Quantity = UnitRegistry(autoconvert_offset_to_baseunit=True).Quantity
ex = LogarithmicUnitCalculusError(Quantity("1 dB")._units)
assert (
str(ex)
== "Ambiguous operation with logarithmic unit (decibel). See "
+ LOG_ERROR_DOCS_HTML
+ " for guidance."
)
ex = LogarithmicUnitCalculusError(
Quantity("1 dB")._units, Quantity("1 octave")._units
)
assert (
str(ex)
== "Ambiguous operation with logarithmic unit (decibel, octave). See "
+ LOG_ERROR_DOCS_HTML
+ " for guidance."
)
with pytest.raises(PintError):
raise ex
def test_pickle_definition_syntax_error(self, subtests):
# OffsetUnitCalculusError raised from a custom ureg must be pickleable even if
# the ureg is not registered as the application ureg
ureg = UnitRegistry(filename=None)
ureg.define("foo = [bar]")
ureg.define("bar = 2 foo")
q1 = ureg.Quantity("1 foo")
q2 = ureg.Quantity("1 bar")
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
for ex in (
DefinitionSyntaxError("foo"),
RedefinitionError("foo", "bar"),
UndefinedUnitError("meter"),
DimensionalityError("a", "b", "c", "d", extra_msg=": msg"),
OffsetUnitCalculusError(
Quantity("1 kg")._units, Quantity("1 s")._units
),
OffsetUnitCalculusError(q1._units, q2._units),
):
with subtests.test(protocol=protocol, etype=type(ex)):
pik = pickle.dumps(ureg.Quantity("1 foo"), protocol)
with pytest.raises(UndefinedUnitError):
pickle.loads(pik)
# assert False, ex.__reduce__()
ex2 = pickle.loads(pickle.dumps(ex, protocol))
print(ex)
print(ex2)
assert type(ex) is type(ex2)
assert ex == ex
# assert ex.__dict__ == ex2.__dict__
assert str(ex) == str(ex2)
with pytest.raises(PintError):
raise ex
def test_dimensionality_error_message(self):
ureg = UnitRegistry(system="SI")
with pytest.raises(ValueError) as error:
ureg.get_dimensionality("[bilbo]")
assert (
str(error.value)
== "[bilbo] is not defined as dimension in the pint UnitRegistry"
)
|