fix(csvstat): --decimal-format strips significant zeroes, and comma locales get a dangling separator - #1353
Open
VXNCXNX wants to merge 1 commit into
Open
Conversation
…l format Use locale-specific decimal point when stripping trailing zeros, and only strip trailing zeros when the decimal point is actually in the formatted value. This preserves significant trailing zeros in formats like %.0f and prevents leaving a trailing decimal separator in non-US locales.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's broken
format_decimalcalls.rstrip('0').rstrip('.')unconditionally, which is only safe when the formatted string actually contains a decimal point.A zero-decimal format loses significant digits.
There is no decimal point in
120, so therstrip('0')eats a real digit.%dand%.0eare affected the same way.Any comma-decimal locale gets a dangling separator on every whole number.
1.000,000becomes1.000,. Therstrip('0')removes the fractional zeros, thenrstrip('.')cannot clean up the trailing separator because it only knows about..The fix
Strip only when the locale's decimal separator is actually present, and strip that separator rather than a hardcoded
.:After:
This is already visible in your own test suite
Under a comma-decimal locale,
tests/test_utilities/test_csvstat.pyfails 3 of 23 on master, for exactly this reason. With the fix it is 23 of 23. The full suite goes from 350 passed and 4 failed to 350 passed and 1 failed underde_DE.UTF-8, and the one that remains istest_csvlook.py::test_no_number_ellipsis, which is a different utility and fails identically on an untouched tree.Under
en_US.UTF-8the suite is 351 passed before and after, so nothing regressed for the common case.A note on the tests
csvstat.pycallslocale.setlocale(locale.LC_ALL, '')at import, so the existingtest_decimal_formatassertions were environment-dependent while spelling their separators literally as,and.. They now derive both fromlocale.localeconv(), which is what made them pass underen_USand fail underde_DEwhile testing the same thing.I did not touch that import-time
setlocale, since it is a separate concern.Verification
A
--decimal-format '%.0f'case added totest_decimal_format, which had three assertions but none with a zero-decimal format.With the fix reverted and the tests kept: 1 failure under
en_US, 3 underde_DE.Changelog entry added under Unreleased.