Skip to content

Raise TypeError when Dataset passed as data_vars to Dataset constructor#11139

Open
kkollsga wants to merge 2 commits intopydata:mainfrom
kkollsga:fix-dataset-constructor-rejects-dataset
Open

Raise TypeError when Dataset passed as data_vars to Dataset constructor#11139
kkollsga wants to merge 2 commits intopydata:mainfrom
kkollsga:fix-dataset-constructor-rejects-dataset

Conversation

@kkollsga
Copy link
Contributor

@kkollsga kkollsga commented Feb 6, 2026

Summary

Dataset(ds) silently dropped .attrs because Dataset inherits from Mapping, so the constructor iterated over data variable keys while ignoring attributes. This was never intended behavior.

This PR raises TypeError with a helpful message directing users to ds.copy() instead, as discussed in #11095 with @keewis.

Test plan

  • New test test_constructor_dataset_as_data_vars_raises verifies the error is raised
  • All existing constructor tests pass
  • Pre-commit hooks pass

🤖 Generated with Claude Code

Dataset(ds) silently dropped attrs because Dataset inherits from Mapping,
so the constructor iterated over data variable keys while ignoring attrs.
This was never intended behavior. Raise TypeError with a message directing
users to ds.copy() instead.

Closes GH11095

Co-authored-by: Claude <noreply@anthropic.com>
@kkollsga kkollsga force-pushed the fix-dataset-constructor-rejects-dataset branch from d73c212 to cf9685e Compare February 6, 2026 18:10
Copy link
Member

@jsignell jsignell left a comment

Choose a reason for hiding this comment

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

This looks good to me! Thanks @kkollsga! I'll just leave it open for a bit to see if anyone feels this needs a deprecation cycle (I do not).

@jsignell jsignell added the plan to merge Final call for comments label Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using Dataset Constructor to create a copy drops attrs

2 participants