Fix pickling regression with scipy backend after multiple opens#11413
Open
C1-BA-B1-F3 wants to merge 1 commit into
Open
Fix pickling regression with scipy backend after multiple opens#11413C1-BA-B1-F3 wants to merge 1 commit into
C1-BA-B1-F3 wants to merge 1 commit into
Conversation
When opening multiple scipy-backed datasets from file-like objects, the class was recreated inside on each call, overwriting the previous class definition on . This broke pickle's class-identity check for instances created by earlier calls. Fix: Only set the class on once (first call). Subsequent calls reuse the same class definition, ensuring pickle stability. Closes pydata#11323
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.
Description
This PR fixes a pickling regression introduced in 2026.4.0 when using the scipy backend with file-like objects.
Problem: When opening multiple scipy-backed datasets from file-like objects (e.g.,
BytesIO), theflush_only_netcdf_fileclass was recreated inside_open_scipy_netcdfon each call. Each new class definition overwrote the previous one on_PickleWorkaround, breaking pickle's class-identity check for instances created by earlier calls.Fix: Only set the class on
_PickleWorkaroundonce (on the first call). Subsequent calls reuse the same class definition, ensuring pickle stability.This is a minimal, focused fix that:
_initializedflag to_PickleWorkaroundRelated Issues
Testing
Added a new test
test_pickle_open_dataset_after_multiple_opensthat verifies pickling works correctly after opening multiple datasets from the same bytes source.All existing scipy backend tests continue to pass.
Minimal Complete Verifiable Example (now fixed):