Skip to content

Return an Integer when rounding a Float without digits - #695

Open
Sskift wants to merge 1 commit into
python-poetry:masterfrom
Sskift:fix/float-round-integral-result
Open

Sskift wants to merge 1 commit into
python-poetry:masterfrom
Sskift:fix/float-round-integral-result

Conversation

@Sskift

@Sskift Sskift commented Nov 2, 2026

Copy link
Copy Markdown

Summary

round() without ndigits (or with None) should return an integer for a float, but Float.__round__ defaults to zero digits and returns another Float. A value read from TOML therefore fails where the equivalent rounded native float works:

import tomlkit

count = tomlkit.parse("count = 2.5")["count"]
range(round(count))  # TypeError before this change

Distinguish omitted/None digits from an explicit digit count, wrapping the native integral result as Integer with copied trivia. Explicit digit counts still produce Float, including zero. This also restores the native errors for rounding infinities and NaN without digits.

Validation on Windows / CPython 3.12.10:

  • The new tests produce 16 failures and 4 passing controls on the original source.
  • All 132 item tests and all 1,244 tests, including the pinned TOML conformance corpus, pass with this change.
  • Ruff 0.17.2 lint/format and git diff --check pass.
  • Mypy 1.19.1 reports the same 33 existing errors in eight files on both versions; diagnostic messages are identical after normalizing shifted line numbers.
  • A separate Codex agent reviewed the exact patch and independently reproduced the red/green item tests. Its 2,812-case native-Python comparison found no mismatches after the fix, and strict result-type fixtures passed in Python 3.9 and 3.12 type-checking modes. Runtime testing used Python 3.12.

Tests cover ties-to-even, both signs, omitted/None/direct invocation, explicit digit counts, nonfinite errors, and comment/sign preservation.

Duplicate Check

Agent Drafting Metadata

  • Agent: OpenAI Codex
  • Model: Exact model identifier was not exposed to this worker.
  • Notes: Codex assisted investigation, implementation, tests, and this description. The separate review described above was performed by another AI agent; no human review or maintainer acceptance is claimed.

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

Successfully merging this pull request may close these issues.

1 participant