Skip to content

Commit

Permalink
fix: Fix round function returning float instead of int
Browse files Browse the repository at this point in the history
Fix #2081
  • Loading branch information
jules-ch committed Dec 10, 2024
1 parent c70c076 commit 3010a31
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 13 additions & 16 deletions pint/facets/plain/quantity.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3010a31

Please sign in to comment.