Fix TypeError for datetime dict keys with key-cleaning flags (fixes #550)#599
Open
gaoflow wants to merge 1 commit into
Open
Fix TypeError for datetime dict keys with key-cleaning flags (fixes #550)#599gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
… flags
number_to_string guards on `numbers`, which includes the datetime types
(they are orderable). Datetimes therefore passed the guard and reached
`round(number, ...)`, which they do not support. Using a datetime/date as a
dict key together with a flag that cleans keys (ignore_numeric_type_changes,
ignore_string_case, ignore_string_type_changes) raised:
TypeError: type datetime.datetime doesn't define __round__ method
Guard on `only_numbers` instead so non-round()-able values (datetimes,
dates, ...) are returned unchanged, like every other non-numeric value.
Adds a regression test. Fixes qlustered#550.
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.
Summary
Using a
datetime/dateas a dict key together with one of thekey-cleaning flags raised a
TypeError:The same happens with
ignore_string_caseandignore_string_type_changes(any flag that routes dict keys through
_get_clean_to_keys_mapping), and withdatetime.datekeys.Root cause
_get_clean_to_keys_mappingcleans numeric keys vianumber_to_string, whosetype guard is:
numbersis defined asonly_numbers + datetimes(datetimes are includedbecause they are orderable). So a datetime key passes the guard and falls
through to
round(number, ndigits=significant_digits), which datetimes do notimplement →
TypeError.Fix
Guard on
only_numbersinstead. Datetimes/dates (and anything else that is nota real, round()-able number) are returned unchanged, exactly like every other
non-numeric value — which is what the early-return branch is for. Numeric
behaviour (int/float/Decimal/Fraction/complex/numpy, significant-digit
rounding) is unchanged.
Verification
Added
test_datetime_dict_key_with_ignore_flagstotests/test_diff_datetime.py. It fails ondev(
TypeError ... __round__) and passes with this change. The existingtests/test_diff_datetime.pyandtests/test_helper.pysuites (168 tests)still pass.
Fixes #550.