diff --git a/CHANGES b/CHANGES index 8a2ba2d60..0395b7fc0 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,13 @@ Pint Changelog 0.10 (unreleased) ----------------- +- Improvements to wraps and check: + - fail upon decoration (not execution) by checking wrapped function signature against + wraps/check arguments. + (might BREAK test code) + - wraps only accepts strings and Units (not quantities) to avoid confusion with magnitude. + (might BREAK code not conforming to documentation) + - when strict=True, strings that can be parsed to quantities are accepted as arguments. - Add revolutions per second (rps) - Improved compatbility for upcast types like xarray's DataArray or Dataset, to which Pint Quantities now fully defer for arithmetic and NumPy operations. A collection of diff --git a/pint/testsuite/test_unit.py b/pint/testsuite/test_unit.py index 7f64473cc..0b6c086a8 100644 --- a/pint/testsuite/test_unit.py +++ b/pint/testsuite/test_unit.py @@ -433,6 +433,9 @@ def func(x): ureg = self.ureg + self.assertRaises(TypeError, ureg.wraps, (3 * ureg.meter, [None])) + self.assertRaises(TypeError, ureg.wraps, (None, [3 * ureg.meter])) + f0 = ureg.wraps(None, [None])(func) self.assertEqual(f0(3.0), 3.0) @@ -451,6 +454,16 @@ def func(x): self.assertEqual(f1b(3.0 * ureg.meter), 3.0) self.assertRaises(DimensionalityError, f1b, 3 * ureg.second) + f1c = ureg.wraps("meter", [ureg.meter])(func) + self.assertEqual(f1c(3.0 * ureg.centimeter), 0.03 * ureg.meter) + self.assertEqual(f1c(3.0 * ureg.meter), 3.0 * ureg.meter) + self.assertRaises(DimensionalityError, f1c, 3 * ureg.second) + + f1d = ureg.wraps(ureg.meter, [ureg.meter])(func) + self.assertEqual(f1d(3.0 * ureg.centimeter), 0.03 * ureg.meter) + self.assertEqual(f1d(3.0 * ureg.meter), 3.0 * ureg.meter) + self.assertRaises(DimensionalityError, f1d, 3 * ureg.second) + f1 = ureg.wraps(None, "meter")(func) self.assertRaises(ValueError, f1, 3.0) self.assertEqual(f1(3.0 * ureg.centimeter), 0.03) @@ -565,17 +578,8 @@ def gfunc(x, y): 1 * ureg.meter / ureg.second ** 2, ) - g2 = ureg.check("[speed]")(gfunc) - self.assertRaises(DimensionalityError, g2, 3.0, 1) - self.assertRaises(TypeError, g2, 2 * ureg.parsec) - self.assertRaises(DimensionalityError, g2, 2 * ureg.parsec, 1.0) - self.assertEqual(g2(2.0 * ureg.km / ureg.hour, 2), 1 * ureg.km / ureg.hour) - - g3 = ureg.check("[speed]", "[time]", "[mass]")(gfunc) - self.assertRaises(TypeError, g3, 1 * ureg.parsec, 1 * ureg.angstrom) - self.assertRaises( - TypeError, g3, 1 * ureg.parsec, 1 * ureg.angstrom, 1 * ureg.kilogram - ) + self.assertRaises(TypeError, ureg.check("[speed]"), gfunc) + self.assertRaises(TypeError, ureg.check("[speed]", "[time]", "[mass]"), gfunc) def test_to_ref_vs_to(self): self.ureg.autoconvert_offset_to_baseunit = True