Skip to content

refactor(python): use cached_property and MappingProxyType for read-only collections (#284)#286

Merged
weinbe58 merged 2 commits intomainfrom
phil/284-mapping-proxy
Mar 20, 2026
Merged

refactor(python): use cached_property and MappingProxyType for read-only collections (#284)#286
weinbe58 merged 2 commits intomainfrom
phil/284-mapping-proxy

Conversation

@weinbe58
Copy link
Copy Markdown
Member

Summary

Closes #284. Applies @cached_property + MappingProxyType / tuple() patterns to mutable collection properties across the codebase.

Changes

python/bloqade/lanes/visualize/artist.py

  • All 6 PlotParameters properties (atom_plot_args, gate_spot_args, slm_plot_args, aod_line_args, aod_marker_args, atom_label_args) converted from @property to @cached_property returning MappingProxyType[str, Any]
  • Avoids recreating dicts on every access and prevents accidental mutation

python/bloqade/lanes/device.py

  • DetectorResult.detectors and DetectorResult.observables now return tuple() instead of exposing mutable internal lists directly

python/bloqade/lanes/layout/arch.py

  • Added comment explaining paths is intentionally mutable (populated incrementally after construction)

python/tests/visualize/test_artist.py

  • Updated assertions to check MappingProxyType instead of dict

Test plan

  • 424 Python tests pass
  • All pre-commit hooks pass

Related

🤖 Generated with Claude Code

…ty access

Convert PlotParameters dict-returning properties in artist.py to
@cached_property with MappingProxyType for immutable, cached access.
Wrap DetectorResult.detectors and .observables in tuple() to prevent
mutation of internal lists. Add comment to ArchSpec.paths explaining
why it remains mutable (populated incrementally after construction).

Closes #284

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 20, 2026 18:28
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 20, 2026

Codecov Report

❌ Patch coverage is 96.87500% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
python/bloqade/lanes/arch/gemini/impls.py 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors several Python APIs to return cached, read-only views of collection-like properties (via @cached_property, MappingProxyType, and tuple()), reducing repeated allocations and discouraging accidental mutation of internal state.

Changes:

  • Converted PlotParameters dict-returning properties to @cached_property returning MappingProxyType.
  • Updated DetectorResult.detectors / DetectorResult.observables to return tuples instead of the underlying mutable lists.
  • Adjusted tests and added documentation note clarifying ArchSpec.paths is intentionally mutable.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
python/bloqade/lanes/visualize/artist.py Cache and wrap plot-argument dicts in MappingProxyType to avoid reallocation and mutation.
python/bloqade/lanes/device.py Return tuples for detector/observable shot collections to reduce external mutation surface.
python/bloqade/lanes/layout/arch.py Document that paths is intentionally mutable and incrementally populated.
python/tests/visualize/test_artist.py Update tests to expect MappingProxyType for plot-argument properties.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 20, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
5052 4315 85% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
python/bloqade/lanes/arch/gemini/impls.py 84% 🟢
python/bloqade/lanes/device.py 88% 🟢
python/bloqade/lanes/layout/arch.py 90% 🟢
python/bloqade/lanes/visualize/artist.py 76% 🟢
TOTAL 84% 🟢

updated for commit: d76874b by action🐍

…284)

- Add from __future__ import annotations to artist.py for Python 3.10
  compatibility with MappingProxyType[str, Any] annotations
- Make PlotParameters frozen=True to keep cached_property coherent
- Deep-copy inner lists in DetectorResult: return tuple(tuple(shot))
  instead of tuple(self._list) to prevent mutation of inner lists
- Make ArchSpec.paths read-only with MappingProxyType — refactor
  impls.py to merge path dicts before construction instead of
  mutating after
- Add caching identity check to test_plot_parameters_properties

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@weinbe58 weinbe58 enabled auto-merge (squash) March 20, 2026 19:21
@weinbe58 weinbe58 disabled auto-merge March 20, 2026 19:21
@weinbe58 weinbe58 merged commit bf28857 into main Mar 20, 2026
20 checks passed
@weinbe58 weinbe58 deleted the phil/284-mapping-proxy branch March 20, 2026 19:21
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.

refactor: use MappingProxyType and cached_property for mutable collection properties

2 participants