Skip to content

Respect class-level singleton decorators in multibindings - #332

Open
boyfeiyu wants to merge 1 commit into
python-injector:masterfrom
boyfeiyu:BOYF-485-decorated-singleton-multibind
Open

boyfeiyu wants to merge 1 commit into
python-injector:masterfrom
boyfeiyu:BOYF-485-decorated-singleton-multibind

Conversation

@boyfeiyu

@boyfeiyu boyfeiyu commented Nov 5, 2026

Copy link
Copy Markdown

Repeated contributions of the same @singleton class currently create separate objects within multibound lists and dictionaries. Respect the class decorator by sharing a class-specific cache key within each collection, and apply the scope to dictionary values before wrapping them with their keys.

Explicit binding scopes keep their existing per-contribution behavior, including scope=singleton and scope=noscope. Ordinary bindings, other provider kinds, and custom scopes retain their existing behavior.

Fixes #298. The regression test comes from @davidparsson's open draft #292, which currently contains the test without a production fix. This change does not implement the parent/child collection extension proposed in #324.

Validation on CPython 3.12.14 / macOS arm64:

  • Full suite: 152 passed, 97.19% coverage.
  • The original list regression and a separate replay of its dictionary assertion fail on current master and pass with this change.
  • 25 additional independent checks passed, including construction counts, repeated/concurrent lookups, explicit scopes, dictionary keys, and injector isolation.
  • mypy 1.7.1, Black 26.10.1, and check-manifest --no-build-isolation passed, using the repository's declared tool versions.

The other Python/PyPy and Linux CI environments have not been run locally.

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.

Multibind should respect the @singleton decorator

1 participant