Skip to content

Preserve Float Subclass #361

@deckar01

Description

@deckar01

Numbers are cast to float before format()ing, which prevents custom formatting logic provided by subclasses. tabulate/__init__.py#L1350

Example

from tabulate import tabulate
from prefixed import Float

tabulate([[Float(1)]], floatfmt='h')

ValueError: Unknown format code 'h' for object of type 'float'

Background

Proposals to extend the built-in python formats have been rejected, because subclasses are intended to satisfy this use case.

https://discuss.python.org/t/new-format-specifiers-for-string-formatting-of-floats-with-si-and-iec-prefixes/26914/6

This regression was introduced in version 0.2. 5115cea/tabulate.py#L154

Related to #297.

Proposal

Only apply the float cast in _format() to strings.

Edit: And decimal align any string by . or \d([^d]) in _afterpoint().

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