Skip to content

Custom formatters not working with modifiers (Undocumented breaking change in 0.24) #2017

@ManuelHu

Description

@ManuelHu

Combining a custom format registered with @pint.register_unit_format and a modifier does not work on 0.24 any more. Apparently the formatter is silently not even called.

import pint

ureg = pint.get_application_registry().get()

@pint.register_unit_format("test")
def _test_format(unit, registry, **options):
    print("format called")
    proc = {u.replace("µ", "u"): e for u, e in unit.items()}
    fmt = pint.formatting if hasattr(pint, "formatting") else pint
    return fmt.formatter(
        proc.items(),
        as_ratio=True,
        single_denominator=False,
        product_fmt="*",
        division_fmt="/",
        power_fmt="{}{}",
        parentheses_fmt="({})",
        **options,
    )

base_unit = ureg.microsecond
print(f"with modifier - {base_unit:~test}")
print(f"without modifier - {base_unit:test}")

Output on pint 0.23

format called
with modifier - us
format called
without modifier - microsecond

Output on pint 0.24

with modifier - µs
format called
without modifier - microsecond

unfortunately we currently have to still support python 3.9, so only supporting pint 0.24+ is not an option for us, our code has to work on both versions... :-(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions