Skip to content

Quantity.__format__ with unicode or console does not typeset the unit correctly #14398

Closed

Description

@mhvk

Description

As noted by @olebole in #14393, if one formats a Quantity with unicode or console format, the result is bad: the denominator gets put under the value. I think this was an oversight when @eerovaher introduced allowing __format__ to take format as an argument in #13050 -- although the underlying problem is likely that the formatters use line breaks which do not keep pieces together (see below).

Expected behavior

The unit should stay together.

How to Reproduce

import astropy.units as u
q = 10*u.m/u.s
print(f"{q:unicode}")
10.0  m
 ─
 s

print(f"{q:console}")
10.0  m
 -
 s

The problem really seems to be due to the use of line breaks in the unit; one would have to collect this and typeset it differently.

q.unit.to_string('console')
' m\n -\n s'

Versions

Linux-5.10.0-20-amd64-x86_64-with-glibc2.31
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110]
astropy 5.3.dev513+gab61aa73a

Activity

  1. mhvk commented on Feb 17, 2023

    @mhvk
    ContributorAuthor

    Fixed by #14393, since now the default style is inline=True, so that no fraction is used any more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions