diff options
author | Jules Chéron <43635101+jules-ch@users.noreply.github.com> | 2022-11-22 21:19:33 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-22 21:19:33 +0100 |
commit | 1819e7ebd784aad286b1c60a2fb5cf1e0aff08eb (patch) | |
tree | 6c5d4e3a9b0af5356d27d7f058dd80db7416d1f7 | |
parent | 171834978976a5ef0801e7f7a6d649c6521ee834 (diff) | |
parent | ec8e90c5ba2c93ca39d42198c80b9b8b20c7720b (diff) | |
download | pint-1819e7ebd784aad286b1c60a2fb5cf1e0aff08eb.tar.gz |
Merge pull request #1629 from Yoshanuikabundi/patch-1
Fix cryptic error when re-registering a formatter
-rw-r--r-- | pint/formatting.py | 2 | ||||
-rw-r--r-- | pint/testsuite/test_formatting.py | 15 |
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>" |