Skip to content

naturalsize(-1) renders "-1 Byte" in non-GNU modes #538

Not planned

Description

@SonicHedghog

What did you do?

import humanize

print(humanize.naturalsize(-1))
print(humanize.naturalsize(-1, binary=True))
print(humanize.naturalsize(1))

Output with humanize 4.16.0:

-1 Byte
-1 Byte
1 Byte

For comparison, Jinja2 3.1.6:

from jinja2.filters import do_filesizeformat

print(do_filesizeformat(-1))  # -1 Bytes
print(do_filesizeformat(1))   # 1 Byte

What did you expect to happen?

The naturalsize() docstring says: "Non-GNU modes are compatible with jinja2's filesizeformat filter." On that basis, I expected -1 Bytes for naturalsize(-1) in the default and binary modes.

What actually happened?

Both non-GNU modes render -1 Byte. The implementation uses abs(bytes_) == 1 for the singular branch, so negative one is singularized even though Jinja2 treats only positive one as singular.

This is small, but negative inputs are not entirely outside the documented domain: the docstring already includes naturalsize(-4096, True) as an example.

The behavior is still present on current main (4e508a33e86455ab37f5f1cdbbaf63b0fbc8a745), where src/humanize/filesize.py checks abs_bytes == 1.

Counter-argument

Negative byte counts are unusual, and "compatible with Jinja2" might mean the general format rather than exact edge-case pluralization. If that is the intent, a documentation clarification may be enough. But if compatibility is meant literally, this is a narrow mismatch.

Possible resolutions

  1. Use positive one for the singular branch, e.g. bytes_ == 1 rather than abs(bytes_) == 1, with regression tests for -1, -1.0, "-1", 1, and both non-GNU modes.
  2. Alternatively, document that negative inputs intentionally differ from Jinja2's pluralization.

I searched open and closed issues and PRs for naturalsize negative, "-1 Byte", "-1 Bytes", filesizeformat negative, and Jinja2 filesizeformat. I found adjacent below-base fractional-byte work in #459 / #462, but not this negative-one pluralization mismatch; #462 leaves the singular branch unchanged. If compatibility is desirable, I would be happy to implement the small fix with tests.

What versions are you using?

  • OS: Linux
  • Python: 3.10.12
  • Humanize: 4.16.0
  • Jinja2: 3.1.6

Activity

  1. SonicHedghog commented on Dec 2, 2026

    @SonicHedghog
    Author

    Withdrawing this — on reflection it's too marginal to be worth maintainer time (negative byte counts are nonsense inputs in practice), and I don't want to add noise. Apologies for the extra traffic.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions