summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pint/formatting.py2
-rw-r--r--pint/testsuite/test_formatting.py15
2 files changed, 16 insertions, 1 deletions
diff --git a/pint/formatting.py b/pint/formatting.py
index 8d59756..554b381 100644
--- a/pint/formatting.py
+++ b/pint/formatting.py
@@ -157,7 +157,7 @@ def register_unit_format(name):
def wrapper(func):
if name in _FORMATTERS:
- raise ValueError(f"format {name:!r} already exists") # or warn instead
+ raise ValueError(f"format {name!r} already exists") # or warn instead
_FORMATTERS[name] = func
return wrapper
diff --git a/pint/testsuite/test_formatting.py b/pint/testsuite/test_formatting.py
index d287948..48e770b 100644
--- a/pint/testsuite/test_formatting.py
+++ b/pint/testsuite/test_formatting.py
@@ -52,3 +52,18 @@ def test_split_format(format, default, flag, expected):
result = fmt.split_format(format, default, flag)
assert result == expected
+
+
+def test_register_unit_format(func_registry):
+ @fmt.register_unit_format("custom")
+ def format_custom(unit, registry, **options):
+ return "<formatted unit>"
+
+ quantity = 1.0 * func_registry.meter
+ assert f"{quantity:custom}" == "1.0 <formatted unit>"
+
+ with pytest.raises(ValueError, match="format 'custom' already exists"):
+
+ @fmt.register_unit_format("custom")
+ def format_custom_redefined(unit, registry, **options):
+ return "<overwritten>"