Skip to content

Commit

Permalink
Fix siunitx format of integer powers with non_int_type=decimal.Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
tovrstra committed May 6, 2024
1 parent f2e4081 commit e01337b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ MANIFEST
.mypy_cache
pip-wheel-metadata
pint/testsuite/dask-worker-space
venv
.envrc

# WebDAV file system cache files
.DAV/
Expand Down
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Pint Changelog
- Add `dim_sort` function to _formatter_helpers.
- Add `dim_order` and `default_sort_func` properties to FullFormatter.
(PR #1926, fixes Issue #1841)
- Fix LaTeX siuntix formatting when using non_int_type=decimal.Decimal.


0.23 (2023-12-08)
Expand Down
4 changes: 2 additions & 2 deletions pint/delegates/formatter/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def siunitx_format_unit(
) -> str:
"""Returns LaTeX code for the unit that can be put into an siunitx command."""

def _tothe(power: int | float) -> str:
if isinstance(power, int) or (isinstance(power, float) and power.is_integer()):
def _tothe(power) -> str:
if power == int(power):
if power == 1:
return ""
elif power == 2:
Expand Down
17 changes: 17 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,3 +1201,20 @@ def test_issues_1841_xfail():

# this prints "2*pi hour * radian", not "2*pi radian * hour" unless sort_dims is True
# print(q)


@pytest.mark.parametrize(
"given,expected",
[
(
"8.989e9 newton * meter^2 / coulomb^2",
r"\SI[]{8.989E+9}{\meter\squared\newton\per\coulomb\squared}",
),
("5 * meter / second", r"\SI[]{5}{\meter\per\second}"),
("2.2 * meter^4", r"\SI[]{2.2}{\meter\tothe{4}}"),
("2.2 * meter^-4", r"\SI[]{2.2}{\per\meter\tothe{4}}"),
],
)
def test_issue1772(given, expected):
ureg = UnitRegistry(non_int_type=decimal.Decimal)
assert f"{ureg(given):Lx}" == expected

0 comments on commit e01337b

Please sign in to comment.