From 5cf654c6f0ed9ace825545e531b855ac8c29c6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jules=20Ch=C3=A9ron?= Date: Sun, 18 Sep 2022 12:36:40 +0200 Subject: [PATCH 1/3] Update quantity repr - Update CHANGES - Add test for repr - Update repr function to include magnitude repr & units --- CHANGES | 2 ++ pint/facets/plain/quantity.py | 5 +---- pint/testsuite/test_quantity.py | 13 ++++++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 177c198f6..7b6ddc971 100644 --- a/CHANGES +++ b/CHANGES @@ -11,6 +11,8 @@ Pint Changelog (Issue #1030, #574) - Added angular frequency documentation page. - Move ASV benchmarks to dedicated folder. (Issue #1542) +- Change Quantity repr to include magnitude repr & units. + 0.19.2 (2022-04-23) ------------------- diff --git a/pint/facets/plain/quantity.py b/pint/facets/plain/quantity.py index 5e4f33ddb..34351767d 100644 --- a/pint/facets/plain/quantity.py +++ b/pint/facets/plain/quantity.py @@ -267,10 +267,7 @@ def __bytes__(self) -> bytes: return str(self).encode(locale.getpreferredencoding()) def __repr__(self) -> str: - if isinstance(self._magnitude, float): - return f"" - else: - return f"" + return f"" def __hash__(self) -> int: self_base = self.to_base_units() diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py index c9b3fe9ee..1cf7fab77 100644 --- a/pint/testsuite/test_quantity.py +++ b/pint/testsuite/test_quantity.py @@ -121,7 +121,18 @@ def test_quantity_comparison_convert(self): def test_quantity_repr(self): x = self.Q_(4.2, UnitsContainer(meter=1)) assert str(x) == "4.2 meter" - assert repr(x) == "" + assert repr(x) == "" + x = self.Q_(2.9325156461464651456456545649) + assert repr(x) == "" + + @helpers.requires_numpy + def test_quantity_repr_numpy(self): + x = self.Q_(4.2) + assert repr(x) == "" + x = self.Q_(np.array(2.0)) + assert repr(x) == "" + x = self.Q_(np.array(2.0, dtype=np.float32)) + assert repr(x) == "" def test_quantity_hash(self): x = self.Q_(4.2, "meter") From 0e96b8e675a55664a6de6f97acfd4af2e06a22c6 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sat, 22 Jun 2024 14:04:50 +0100 Subject: [PATCH 2/3] Update quantity.py --- pint/facets/plain/quantity.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pint/facets/plain/quantity.py b/pint/facets/plain/quantity.py index 31fc063e3..7ae36d151 100644 --- a/pint/facets/plain/quantity.py +++ b/pint/facets/plain/quantity.py @@ -275,13 +275,13 @@ def __bytes__(self) -> bytes: def __repr__(self) -> str: if HAS_UNCERTAINTIES: if isinstance(self._magnitude, UFloat): - return f"" + return f"" else: - return f"" + return f"" elif isinstance(self._magnitude, float): - return f"" + return f"" - return f"" + return f"" def __hash__(self) -> int: self_base = self.to_base_units() From b8e44aa755fc3a60773c155868513b3179fe35eb Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sat, 22 Jun 2024 14:09:04 +0100 Subject: [PATCH 3/3] Update test_quantity.py --- pint/testsuite/test_quantity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py index 35f88ff61..969d61a19 100644 --- a/pint/testsuite/test_quantity.py +++ b/pint/testsuite/test_quantity.py @@ -128,7 +128,7 @@ def test_quantity_repr(self): assert str(x) == "4.2 meter" assert repr(x) == "" x = self.Q_(2.9325156461464651456456545649) - assert repr(x) == "" + assert repr(x) == "" @helpers.requires_numpy def test_quantity_repr_numpy(self):