Skip to content

Fix non-empty slices of frozen Boxes - #365

Open
anticipate218 wants to merge 1 commit into
cdgriffith:developfrom
anticipate218:fix/frozen-box-slices
Open

anticipate218 wants to merge 1 commit into
cdgriffith:developfrom
anticipate218:fix/frozen-box-slices

Conversation

@anticipate218

Copy link
Copy Markdown

Problem

Non-empty slices of a frozen Box raise BoxError: Box is frozen instead of returning a frozen subset:

from box import Box

source = Box(first=1, second=2, frozen_box=True)
source[:1]  # currently raises BoxError

Both slice paths create an already-frozen destination and then assign the selected entries into it. Python 3.11 reaches the TypeError path because slices are unhashable; Python 3.12+ normally reaches the KeyError path.

Change

Pass the selected key/value pairs into the configured Box class's constructor, so they are populated before construction finishes. Use an iterable of pairs rather than a mapping to preserve explicit None entries with default_box=True. Keep key selection outside the generator expression for compatibility with the Cython build.

The regressions cover mutable/frozen Boxes, ConfigBox, full/partial/reversed/empty slices, nested frozen values, hashes, default-box and dotted options, recast calls, and an explicitly stored slice key on Python 3.12+. This does not change the separate merge/union operators or BoxList slicing.

Validation

Against develop at e6d83100b32019eca1f53bc14544e8e353229217, on Windows 11:

  • Original implementation with the new tests: 9 failed, 14 passed on Python 3.13; 9 failed, 13 passed, 1 skipped on Python 3.11.
  • Final pure-Python implementation, complete suite: 1,247 passed, 1 skipped on Python 3.13.5; 1,246 passed, 2 skipped on Python 3.11.18.
  • Python 3.13 pure-Python coverage: 100% statements and branches, with the repository's coverage configuration and 100% gate unchanged.
  • Built sdists and Cython 3.3.1 wheels with python -m build --no-isolation on both interpreters. Installed each wheel into a separate target and verified that tests imported its .pyd, not the source checkout. The complete suites produced the same passing counts as above.
  • twine check passed for both sdists and wheels.
  • Full ruff check ., ruff format --check ., mypy box, and configured pre-commit checks for the changed files passed.

Tests ran with Python UTF-8 mode. Other operating systems, PyPy, and the remaining CPython versions were not run locally. An initial candidate exposed a Cython super()/generator compatibility failure; the final implementation above fixes it and both compiled suites were rerun successfully.

This branch has not been deployed

No deployments
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.

1 participant