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.

@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 (746f0a6) to head (b568535).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7122   +/-   ##
=======================================
  Coverage   90.15%   90.15%           
=======================================
  Files          91       91           
  Lines       24985    24985           
  Branches     4685     4685           
=======================================
  Hits        22526    22526           
  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.

@ukmo-ccbunney ukmo-ccbunney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to the code look good - thank you @gaoflow. 👍🏼

There are some updates required regarding the What's New, but I see that you have already been applying those to your other PRs - top points for that! 💯

Are you happy to make the same updates here?

If not, please do say and I'll raise a PR against your branch.

@ukmo-ccbunney ukmo-ccbunney self-assigned this Jun 25, 2026
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 3f3d312 to b568535 Compare June 26, 2026 11:39
@gaoflow

gaoflow commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Updated this PR for the new towncrier changelog layout and rebased onto current main.

Changes in b568535e:

  • removed the hand-written docs/src/whatsnew/latest.rst entry from this PR
  • added changelog/7122.bugfix.rst
  • kept the code/test change otherwise unchanged

Verification after the rebase:

ruff check lib/iris/coords.py lib/iris/tests/unit/coords/test_Coord.py
ruff format --check lib/iris/coords.py lib/iris/tests/unit/coords/test_Coord.py
uv run --with pytest --with pytest-mock --with requests --with filelock --with-editable . python -m pytest lib/iris/tests/unit/coords/test_Coord.py::Test___str__::test_empty_time_coord lib/iris/tests/unit/coords/test_Coord.py::Test___str__::test_empty_string_coord -q
uv run --with towncrier towncrier build --draft --version 0.0
git diff --check origin/main...HEAD

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

2 participants