Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour of Quantity string representation methods with default_format #1299

Closed
mgab opened this issue May 9, 2021 · 1 comment

Comments

@mgab
Copy link
Contributor

mgab commented May 9, 2021

(Related to #1300, which refers to Measurement instances. However, as the problem is not exactly the same I thought it would better to report separated issues)

Context

The attribute default_format of UnitRegistry and Quantity instances controls the default string representation.
When creating a quantity instance from the unit registry, the value of the default_format attribute is correctly propagated.

Expected behaviour

  1. The behaviour of the different representation methods of Quantity instances is coherent and adheres to the value of the default_format attribute of the instance.
  2. If the value of the default_format attribute is updated, the behaviour of the string representation methods of that instance changes accordingly.
  3. The behaviour of the different representation methods is equivalent to that of related classes such as Measurement.

Issue

However, the behaviour of the different string representation methods of the quantity instance is inconsistent:

  • When updating the value of quantity.default_value with an empty string, _repr_html_ and _repr_latex_ adapt its behaviour accordingly, but __str__ keeps following what ureg.default_format specifies. If quantity.default_value is updated with a non-empty string all three methods update their behaviour
  • __repr__ method ignores both quantity.default_format and ureg.default_format. For comparison, the equivalent method from Measurement instances adhere to what it is specified in ureg.default_format.

Minimal working example

import pint
ureg = pint.UnitRegistry()

def test_reprs(obj):
    reprs = ['__str__', '__repr__', '_repr_html_', '_repr_latex_']
    for repr_ in reprs:
        print(f"{repr_}: '{getattr(obj, repr_)()}'")

ureg.default_format = ''
q = ureg.Quantity(1, 'meter')
test_reprs(q)
# __str__: '1 meter'
# __repr__: '<Quantity(1, 'meter')>'
# _repr_html_: '1 meter'
# _repr_latex_: '$1\ \mathrm{meter}$'

ureg.default_format = '~P'
q = ureg.Quantity(1, 'meter')
test_reprs(q)
# __str__: '1 m'
# __repr__: '<Quantity(1, 'meter')>'
# _repr_html_: '1 m'
# _repr_latex_: '$1\ \mathrm{m}$'

ureg.default_format = ''
q = ureg.Quantity(1, 'meter')
q.default_format = '~P'
test_reprs(q)
# __str__: '1 m'
# __repr__: '<Quantity(1, 'meter')>'
# _repr_html_: '1 m'
# _repr_latex_: '$1\ \mathrm{m}$'

ureg.default_format = '~P'
q = ureg.Quantity(1, 'meter')
q.default_format = 'P'
test_reprs(q)
# __str__: '1 meter'
# __repr__: '<Quantity(1, 'meter')>'
# _repr_html_: '1 meter'
# _repr_latex_: '$1\ \mathrm{meter}$'

ureg.default_format = '~P'
q = ureg.Quantity(1, 'meter')
q.default_format = ''
test_reprs(q)
# __str__: '1 m'
# __repr__: '<Quantity(1, 'meter')>'
# _repr_html_: '1 meter'
# _repr_latex_: '$1\ \mathrm{meter}$'
@mgab
Copy link
Contributor Author

mgab commented Oct 4, 2024

Closed as it is no longer relevant.

Current version 0.24.3 Quantities do not care about Quantity.default_format directly on the instance, nor the documentation mentions it, so the inconsistency is gone.

Thanks for the great library!

@mgab mgab closed this as completed Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant