Allow printing of a length-0 coordinate (#6531)#7122
Open
gaoflow wants to merge 1 commit into
Open
Conversation
Coord.summary() formats string and date-like values by first finding the longest string with max(...), which raised ValueError on an empty array. A length-0 coordinate is a valid object (e.g. a time coordinate for an unfilled unlimited dimension), so its summary should not crash. Pass default=0 to max() so an empty array formats as '[]'. Fixes SciTools#6531.
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.
🚀 Pull Request
Description
Closes #6531.
Printing a length-0 coordinate raised a
ValueErrorinstead of producing a summary:As confirmed by @SciTools/peloton on the issue, a length-0 time coordinate is a valid object — it represents an unlimited dimension that does not yet have any points — so printing it should not crash.
Cause
Coord.summary()formats string- and date-like values by first finding the longest element so it can apply a common width:For an empty array this generator is empty, so
max()raises. (Date-like coordinates reach this branch becausenum2dateconverts the points to strings first; the original report'svectorize/otypeserror no longer occurs with currentcf-units, leaving thismax()call as the remaining failure.)Fix
Pass
default=0tomax(), so an empty array yields a zero width and formats as[]. Non-empty coordinates are unaffected.Verification
Test___str__::test_empty_time_coordand::test_empty_string_coord(both date-like and plain-string empty paths). They fail onmainand pass here.tests/unit/coords/andtests/unit/representation/suites pass (470 tests).ruff check/ruff formatclean.