Repository navigation
fix: format quantity amounts as plain decimals - #87
Conversation
…tation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
22b5d92 to
f00bcea
Compare
There was a problem hiding this comment.
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.
| amount_str = format(Decimal(repr(amount)), "f") | ||
| return {"type": "quantity", "value": {"amount": f"+{amount_str}", "unit": unit}} |
There was a problem hiding this comment.
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.
| 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}} |
There was a problem hiding this comment.
Fixed. Used +f format specifier which handles the sign natively, removing the manual f"+{amount_str}" concatenation too.
— Claude Sonnet 4.6
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The Wikibase API rejects scientific notation in quantity values (e.g.
+7.8125e-05). Small floats like exposure time1/12800were being formatted via Python's defaultf"+{amount}"which produces scientific notation.Fix uses
format(Decimal(repr(amount)), "f")in_quantity_dvto always produce plain decimal strings (e.g.+0.000078125).