Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure Measurement handles operations with Quantity #2155

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions pint/facets/plain/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def wrapped(self, *args, **kwargs):
other = args[0]
if is_upcast_type(type(other)):
return NotImplemented
# ensure Measurement handles operations with Quantity
if (
HAS_UNCERTAINTIES
and not type(self).__name__ == "Measurement"
and type(other).__name__ == "Measurement"
):
return NotImplemented
# pandas often gets to arrays of quantities [ Q_(1,"m"), Q_(2,"m")]
# and expects PlainQuantity * array[PlainQuantity] should return NotImplemented
elif isinstance(other, list) and other and isinstance(other[0], type(self)):
Expand Down Expand Up @@ -166,24 +173,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 +836,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
7 changes: 7 additions & 0 deletions pint/testsuite/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,10 @@ def test_tokenization(self):
for p in pint_eval.tokenizer("8 + / - 4"):
str(p)
assert True

def test_measurement_result(self):
q = self.Q_(4.2, "meter")
m = self.Q_(5.0, "meter").plus_minus(0.1)
# quantity + measurement = measurement
result = q + m
assert isinstance(result, self.ureg.Measurement)
Loading