Skip to content

pint.Context does not inherit ureg non_int_type and causes exceptions when ureg has non_int_type=decimal.Decimal #2301

@nneskildsf

Description

@nneskildsf

Minimal example:

from pint import UnitRegistry, Quantity, Context
import decimal

ureg = UnitRegistry(non_int_type=decimal.Decimal)
ureg.define("CV = 2.6 L")
print(ureg.Quantity(decimal.Decimal("1.0"), "CV").to("L")) # 2.60 L as expected
# Redefine using context
c = Context('columnvolume')
c.redefine("CV = 3.14 L")
ureg.add_context(c)
ureg.enable_contexts('columnvolume')
print(ureg.Quantity(decimal.Decimal("1.0"), "CV").to("L")) # Raises exception

Analysis:
I believe that this issue is caused by pint.facets.context.objects.Context.redefine which doesn't apply the non_int_type used in the ureg. Changing the line cfg = ParserConfig(float) to cfg = ParserConfig(decimal.Decimal) fixes the exception. Perhaps pint.facets.context.objects.Context.redefine should have a non_int_type parameter?

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