From 3010a317853e23605fc538f9fdbf72bdc7636779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jules=20Ch=C3=A9ron?= Date: Tue, 10 Dec 2024 20:58:58 +0100 Subject: [PATCH] fix: Fix round function returning `float` instead of `int` Fix #2081 --- CHANGES | 1 + pint/facets/plain/quantity.py | 29 +++++++++++++---------------- pint/testsuite/test_quantity.py | 5 +++++ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index 07328fb1c..18cc23e4c 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,7 @@ Pint Changelog ------------------- - Add docs to the functions in ``pint.testing`` (PR #2070) +- Fix round function returning float instead of int (#2081) 0.24.4 (2024-11-07) diff --git a/pint/facets/plain/quantity.py b/pint/facets/plain/quantity.py index a18919273..ccb08fd83 100644 --- a/pint/facets/plain/quantity.py +++ b/pint/facets/plain/quantity.py @@ -1,9 +1,9 @@ """ - pint.facets.plain.quantity - ~~~~~~~~~~~~~~~~~~~~~~~~~ +pint.facets.plain.quantity +~~~~~~~~~~~~~~~~~~~~~~~~~ - :copyright: 2022 by Pint Authors, see AUTHORS for more details. - :license: BSD, see LICENSE for more details. +:copyright: 2022 by Pint Authors, see AUTHORS for more details. +:license: BSD, see LICENSE for more details. """ from __future__ import annotations @@ -166,24 +166,22 @@ def __reduce__(self) -> tuple[type, Magnitude, UnitsContainer]: @overload def __new__( cls, value: MagnitudeT, units: UnitLike | None = None - ) -> PlainQuantity[MagnitudeT]: - ... + ) -> PlainQuantity[MagnitudeT]: ... @overload - def __new__(cls, value: str, units: UnitLike | None = None) -> PlainQuantity[Any]: - ... + def __new__( + cls, value: str, units: UnitLike | None = None + ) -> PlainQuantity[Any]: ... @overload def __new__( # type: ignore[misc] cls, value: Sequence[ScalarT], units: UnitLike | None = None - ) -> PlainQuantity[Any]: - ... + ) -> PlainQuantity[Any]: ... @overload def __new__( cls, value: PlainQuantity[Any], units: UnitLike | None = None - ) -> PlainQuantity[Any]: - ... + ) -> PlainQuantity[Any]: ... def __new__(cls, value, units=None): if is_upcast_type(type(value)): @@ -831,8 +829,7 @@ def __iadd__(self, other: datetime.datetime) -> datetime.timedelta: # type: ign ... @overload - def __iadd__(self, other) -> PlainQuantity[MagnitudeT]: - ... + def __iadd__(self, other) -> PlainQuantity[MagnitudeT]: ... def __iadd__(self, other): if isinstance(other, datetime.datetime): @@ -1288,8 +1285,8 @@ def __rpow__(self, other) -> PlainQuantity[MagnitudeT]: def __abs__(self) -> PlainQuantity[MagnitudeT]: return self.__class__(abs(self._magnitude), self._units) - def __round__(self, ndigits: int | None = 0) -> PlainQuantity[MagnitudeT]: - return self.__class__(round(self._magnitude, ndigits=ndigits), self._units) + def __round__(self, ndigits: int | None = None) -> PlainQuantity[int]: + return self.__class__(round(self._magnitude, ndigits), self._units) def __pos__(self) -> PlainQuantity[MagnitudeT]: return self.__class__(operator.pos(self._magnitude), self._units) diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py index 26a5ee05d..6f173216c 100644 --- a/pint/testsuite/test_quantity.py +++ b/pint/testsuite/test_quantity.py @@ -60,6 +60,11 @@ def test_quantity_creation(self, caplog): assert 4.2 * self.ureg.meter == self.Q_(4.2, 2 * self.ureg.meter) assert len(caplog.records) == 1 + def test_round(self): + x = self.Q_(1.1, "kg") + assert isinstance(round(x).magnitude, int) + assert isinstance(round(x, 0).magnitude, float) + def test_quantity_with_quantity(self): x = self.Q_(4.2, "m") assert self.Q_(x, "m").magnitude == 4.2