Skip to content

fix: format quantity amounts as plain decimals - #87

Merged
DaxServer merged 3 commits into
mainfrom
fix/wikibase-quantity-decimal-format
Apr 16, 2026
Merged

DaxServer merged 3 commits into
mainfrom
fix/wikibase-quantity-decimal-format

Conversation

@DaxServer

Copy link
Copy Markdown
Owner

The Wikibase API rejects scientific notation in quantity values (e.g. +7.8125e-05). Small floats like exposure time 1/12800 were being formatted via Python's default f"+{amount}" which produces scientific notation.

Fix uses format(Decimal(repr(amount)), "f") in _quantity_dv to always produce plain decimal strings (e.g. +0.000078125).

…tation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@DaxServer
DaxServer force-pushed the fix/wikibase-quantity-decimal-format branch from 22b5d92 to f00bcea Compare April 16, 2026 17:33

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the _quantity_dv function in src/wikibots/lib/claim.py to use the Decimal class for formatting numeric amounts, ensuring they are represented in plain decimal notation. A review comment suggests using the +f format specifier to correctly handle both positive and negative values, avoiding invalid strings like +- for negative numbers.

Comment thread src/wikibots/lib/claim.py Outdated
Comment on lines +74 to +75
amount_str = format(Decimal(repr(amount)), "f")
return {"type": "quantity", "value": {"amount": f"+{amount_str}", "unit": unit}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation unconditionally prepends a + sign to the formatted string. If amount is negative, amount_str will already contain a minus sign (e.g., "-0.01"), resulting in an invalid Wikibase amount string like "+-0.01". Although the current use cases in this repository appear to involve only positive quantities, this utility function should handle negative values correctly.

Using the "+f" format specifier with Decimal is a more robust approach: it automatically prepends + for non-negative numbers and preserves the - for negative numbers, while ensuring the plain decimal (non-scientific) notation required by the Wikibase API.

Suggested change
amount_str = format(Decimal(repr(amount)), "f")
return {"type": "quantity", "value": {"amount": f"+{amount_str}", "unit": unit}}
amount_str = format(Decimal(repr(amount)), "+f")
return {"type": "quantity", "value": {"amount": amount_str, "unit": unit}}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Used +f format specifier which handles the sign natively, removing the manual f"+{amount_str}" concatenation too.

— Claude Sonnet 4.6

DaxServer and others added 2 commits April 16, 2026 19:38
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@DaxServer
DaxServer merged commit f7a41e4 into main Apr 16, 2026
5 checks passed
@DaxServer
DaxServer deleted the fix/wikibase-quantity-decimal-format branch April 16, 2026 17:49
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