Python 3.12 Compatibility Issue #1159
Open
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.
Summary
This PR updates the
rsortfield definitions insuite2p/detection/stats.pyto replace mutable default values withdefault_factory. As of Python 3.12,dataclassenforces that default fields cannot be mutable type, so it is not currently possible to run suite2p in Python 3.12 or newer. Implementing this change ensures compatibility with Python 3.12 and prevents unintended shared state across instances.Changes
Updated
rsortinsuite2p/detection/stats.pyto usedefault_factory = lambda: np.sort(distance_kernel(radius=30).flatten())instead ofdefault=np.sort(distance_kernel(radius=30).flatten()).Verified compatibility with Python 3.12, which now enforces stricter checks on mutable defaults. Also verified backward compatibility with Python 3.9. The
default_factorparameter was introduced in Python 3.7 with thedataclassesmodule, so implementation of this pull request will not affect any previous versions of Python that are able to runsuite2p.Reasoning
Python 3.12 introduced a stricter enforcement of mutable defaults in dataclass fields, raising a
ValueErrorwhen using a mutable default directly. This fix ensures proper instance-level initialization while maintaining the intended behavior.Testing
Verified that the updated implementation runs correctly without
ValueErrorin Python 3.12.