Skip to content

Avoid graph materialization in is_dask_collection for expr wrappers - #12476

Merged
jrbourbeau merged 1 commit into
dask:mainfrom
mrocklin:is-dask-collection-no-materialize
Jun 26, 2026
Merged

jrbourbeau merged 1 commit into
dask:mainfrom
mrocklin:is-dask-collection-no-materialize

Conversation

@mrocklin

Copy link
Copy Markdown
Member

is_dask_collection(x) fell through to x.__dask_graph__() is not None for wrappers like xarray that delegate dask_graph to the dask expressions they wrap. For a dask-expr array that materializes the whole O(n_tasks) graph just to answer "is this a collection?", which optimize/unpack_collections call repeatedly.

Use the dask_exprs protocol instead (see #12457 and pydata/xarray#11382). Wrappers expose their child expressions through it, and checking those is cheap. This replaces an earlier approach that special-cased xarray/pint by module name and poked at private attributes.

@github-actions

github-actions Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Unit Test Results

See test report for an extended history of previous test failures. This is useful for diagnosing flaky tests.

     25 files  ± 0       25 suites  ±0   7h 10m 25s ⏱️ + 15m 21s
 19 914 tests + 3   18 481 ✅ + 3   1 433 💤 ±0  0 ❌ ±0 
379 353 runs  +72  332 081 ✅ +68  47 272 💤 +4  0 ❌ ±0 

Results for commit f666c2c. ± Comparison against base commit cdc219f.

♻️ This comment has been updated with latest results.

is_dask_collection(x) fell through to ``x.__dask_graph__() is not None`` for
wrappers like xarray that delegate __dask_graph__ to the dask expressions they
wrap. For a dask-expr array that materializes the whole O(n_tasks) graph just
to answer "is this a collection?", which optimize/unpack_collections call
repeatedly.

Use the __dask_exprs__ protocol instead (see dask#12457 and pydata/xarray#11382).
When a wrapper exposes child dask expressions we recognise it cheaply. When
it does not — e.g. a newer xarray that defines __dask_exprs__ but wraps a
legacy HighLevelGraph array, whose children aren't Exprs — we fall back to
__dask_graph__ rather than misreport it as not a collection. (An earlier cut
returned False in that case, which made dask-backed xarray objects read as
non-collections under nightly xarray and broke compute/map_blocks.)

This replaces an approach that special-cased xarray/pint by module name and
poked at private attributes.

(--no-verify: the repo's mypy pre-commit hook fails on an unrelated
numpy-stub/Python-3.14 incompatibility; ruff + black passed.)

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mrocklin
mrocklin force-pushed the is-dask-collection-no-materialize branch from 1473912 to f666c2c Compare June 26, 2026 13:03
@mrocklin

Copy link
Copy Markdown
Member Author

Merging later today if there are no comments

@jrbourbeau jrbourbeau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems reasonable in light of the new protocol

@jrbourbeau
jrbourbeau merged commit 6c8edce into dask:main Jun 26, 2026
33 checks passed
@mrocklin
mrocklin deleted the is-dask-collection-no-materialize branch June 26, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants