Skip to content

Commit

Permalink
Fixes issue #386
Browse files Browse the repository at this point in the history
  • Loading branch information
burnpanck committed Nov 28, 2021
1 parent ea2cac3 commit 0973bc5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
14 changes: 12 additions & 2 deletions pint/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
RedefinitionError,
UndefinedUnitError,
)
from .pint_eval import build_eval_tree
from .pint_eval import _BINARY_OPERATOR_MAP, build_eval_tree
from .systems import Group, System
from .util import (
ParserHelper,
Expand Down Expand Up @@ -1232,6 +1232,11 @@ def _eval_token(self, token, case_sensitive=None, use_decimal=False, **values):
else:
raise Exception("unknown token type")

def _eval_implicit_mul(self, left, right):
if isinstance(left, self.Quantity):
return left * right
return self.Quantity(left, right)

def parse_pattern(
self,
input_string: str,
Expand Down Expand Up @@ -1337,8 +1342,13 @@ def parse_expression(
input_string = string_preprocessor(input_string)
gen = tokenizer(input_string)

# replace implicit multiplication with self._eval_implicit_mul
bin_op = dict(_BINARY_OPERATOR_MAP)
bin_op[""] = self._eval_implicit_mul

return build_eval_tree(gen).evaluate(
lambda x: self._eval_token(x, case_sensitive=case_sensitive, **values)
lambda x: self._eval_token(x, case_sensitive=case_sensitive, **values),
bin_op=bin_op,
)

__call__ = parse_expression
Expand Down
9 changes: 7 additions & 2 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,13 @@ def test_issue354_356_370(self):
assert "{:~}".format(1 * self.ureg("MiB")) == "1 MiB"

def test_issue_386(self):
x = ureg.Quantity(7, "degC")
y = ureg.Quantity("7 degC")
x = ureg.Quantity(42, "degC")
y = ureg.Quantity("42 degC")
assert x == y

# make sure normal combined units still work
x = ureg.Quantity(42, ureg.mm * ureg.s)
y = ureg.Quantity("42 mm s")
assert x == y

def test_issue468(self):
Expand Down
36 changes: 18 additions & 18 deletions pint/testsuite/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_square_cube(self):
self._test("sq bcd", "bcd**2")
self._test("square bcd", "bcd**2")
self._test("cubic bcd", "bcd**3")
self._test("bcd efg", "bcd*efg")
self._test("bcd efg", "bcd efg")

def test_per(self):
self._test("miles per hour", "miles/hour")
Expand All @@ -244,25 +244,25 @@ def test_numbers(self):
self._test("1E24", "1E24")

def test_space_multiplication(self):
self._test("bcd efg", "bcd*efg")
self._test("bcd efg", "bcd*efg")
self._test("1 hour", "1*hour")
self._test("1. hour", "1.*hour")
self._test("1.1 hour", "1.1*hour")
self._test("1E24 hour", "1E24*hour")
self._test("1E-24 hour", "1E-24*hour")
self._test("1E+24 hour", "1E+24*hour")
self._test("1.2E24 hour", "1.2E24*hour")
self._test("1.2E-24 hour", "1.2E-24*hour")
self._test("1.2E+24 hour", "1.2E+24*hour")
self._test("bcd efg", "bcd efg")
self._test("bcd efg", "bcd efg")
self._test("1 hour", "1 hour")
self._test("1. hour", "1. hour")
self._test("1.1 hour", "1.1 hour")
self._test("1E24 hour", "1E24 hour")
self._test("1E-24 hour", "1E-24 hour")
self._test("1E+24 hour", "1E+24 hour")
self._test("1.2E24 hour", "1.2E24 hour")
self._test("1.2E-24 hour", "1.2E-24 hour")
self._test("1.2E+24 hour", "1.2E+24 hour")

def test_joined_multiplication(self):
self._test("1hour", "1*hour")
self._test("1.hour", "1.*hour")
self._test("1.1hour", "1.1*hour")
self._test("1h", "1*h")
self._test("1.h", "1.*h")
self._test("1.1h", "1.1*h")
self._test("1hour", "1 hour")
self._test("1.hour", "1. hour")
self._test("1.1hour", "1.1 hour")
self._test("1h", "1 h")
self._test("1.h", "1. h")
self._test("1.1h", "1.1 h")

def test_names(self):
self._test("g_0", "g_0")
Expand Down
3 changes: 1 addition & 2 deletions pint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,8 @@ def __rtruediv__(self, other):
(r"sq ({})", r"\1**2"),
(
r"\b([0-9]+\.?[0-9]*)(?=[e|E][a-zA-Z]|[a-df-zA-DF-Z])",
r"\1*",
r"\1 ",
), # Handle numberLetter for multiplication
(r"([\w\.\-])\s+(?=\w)", r"\1*"), # Handle space for multiplication
]

#: Compiles the regex and replace {} by a regex that matches an identifier.
Expand Down

0 comments on commit 0973bc5

Please sign in to comment.