Skip to content

Commit 91f907c

Browse files
__format__ must fall back to __str__ when spec='' (#64)
2 parents a4e9267 + 6799c7f commit 91f907c

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

test/test_value_array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,8 @@ class _InvalidUnit:
263263
def test_invalid_unit_raises_error() -> None:
264264
with pytest.raises(ValueError):
265265
_ = tu.ValueArray([1, 2], _InvalidUnit())
266+
267+
268+
def test_format() -> None:
269+
u = tu.GHz * np.random.random(10)
270+
assert f'{u}' == str(u)

tunits/core/cython/with_unit.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ cdef class WithUnit:
729729
self.base_units.__setstate__(pickle_info['base_units'])
730730

731731
def __format__(self, spec: str) -> str:
732-
if spec == '' and self.value == 1: return str(self)
732+
if spec == '': return str(self)
733733
return self.value.__format__(spec) + ' ' + str(self.unit)
734734

735735
_try_interpret_as_with_unit = None

0 commit comments

Comments
 (0)