Skip to content

Allow printing of a length-0 coordinate (#6531)#7122

Open
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/print-empty-time-coord
Open

Allow printing of a length-0 coordinate (#6531)#7122
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/print-empty-time-coord

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Pull Request

Description

Closes #6531.

Printing a length-0 coordinate raised a ValueError instead of producing a summary:

>>> from iris.coords import DimCoord
>>> t_coord = DimCoord([], standard_name="time", units="days since 1999-01-01")
>>> print(t_coord)
ValueError: max() iterable argument is empty

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:

length = max(len(str(x)) for x in data.flatten())

For an empty array this generator is empty, so max() raises. (Date-like coordinates reach this branch because num2date converts the points to strings first; the original report's vectorize/otypes error no longer occurs with current cf-units, leaving this max() call as the remaining failure.)

Fix

Pass default=0 to max(), so an empty array yields a zero width and formats as []. Non-empty coordinates are unaffected.

>>> print(t_coord)
DimCoord :  time / (days since 1999-01-01, standard calendar)
    points: []
    shape: (0,)
    dtype: float64
    standard_name: 'time'

Verification

  • New tests Test___str__::test_empty_time_coord and ::test_empty_string_coord (both date-like and plain-string empty paths). They fail on main and pass here.
  • Full tests/unit/coords/ and tests/unit/representation/ suites pass (470 tests).
  • ruff check / ruff format clean.

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.
@gaoflow gaoflow force-pushed the fix/print-empty-time-coord branch from 7784187 to 3f3d312 Compare June 18, 2026 21:39
@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.15%. Comparing base (3f0dd8d) to head (3f3d312).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7122   +/-   ##
=======================================
  Coverage   90.15%   90.15%           
=======================================
  Files          91       91           
  Lines       24983    24983           
  Branches     4685     4685           
=======================================
  Hits        22524    22524           
  Misses       1682     1682           
  Partials      777      777           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Time coordinate with length 0 cannot be printed

1 participant