Skip to content

feat: ✨ Add threadsafe parameter to instance getters#250

Merged
remimd merged 2 commits into
devfrom
threading
May 26, 2025
Merged

feat: ✨ Add threadsafe parameter to instance getters#250
remimd merged 2 commits into
devfrom
threading

Conversation

@remimd
Copy link
Copy Markdown
Member

@remimd remimd commented May 26, 2025

No description provided.

remimd added 2 commits May 26, 2025 10:23
@remimd remimd requested a review from Copilot May 26, 2025 09:27
@remimd remimd self-assigned this May 26, 2025
Copy link
Copy Markdown

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

This PR adds a threadsafe flag to all instance‐retrieval methods, introduces a shared get_lock helper for locking, updates FastAPI integration to expose a InjectThreadSafe alias, and adjusts tests to expect NotImplemented as the missing‐instance sentinel.

  • Introduce threadsafe parameter on get_instance, aget_instance, and their lazy variants
  • Centralize locking via injection._core.common.threading.get_lock and replace direct RLock/nullcontext usages
  • Extend FastAPIInject with threadsafe=True alias (InjectThreadSafe) and update stubs/tests for new default behavior

Reviewed Changes

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

Show a summary per file
File Description
tests/test_singleton.py update default NoneNotImplemented assertions
tests/test_injectable.py same update for injectable tests
tests/core/test_module.py sync/async instance and lazy tests now expect NotImplemented
injection/ext/fastapi.pyi add InjectThreadSafe type alias
injection/ext/fastapi.py support threadsafe in FastAPIInject, expose InjectThreadSafe
injection/_core/common/threading.py new get_lock(threadsafe) helper
injection/_core/scope.py replace inline RLock/nullcontext with get_lock
injection/_core/module.py add threadsafe args to find/get/aget/aget_lazy/get_lazy
injection/_core/common/lazy.py remove unused alazy helper
injection/_core/common/asynchronous.py minor formatting import change
injection/init.pyi update stubs for threadsafe parameter and default return type
Comments suppressed due to low confidence (3)

tests/core/test_module.py:92

  • Add tests that explicitly pass threadsafe=True (and False) to each getter (e.g., aget_instance, get_instance, lazy variants) to verify that concurrent access is correctly locked or unlocked.
async def test_aget_instance_with_no_injectable_return_not_implemented(

injection/_core/common/threading.py:1

  • [nitpick] Consider renaming this module (e.g., lock_utils.py or synchronization.py) to avoid confusion with Python's standard threading module.
from contextlib import nullcontext

injection/ext/fastapi.pyi:7

  • Add an __all__ export in this stub to include InjectThreadSafe so users of the stub file see the alias in auto‐completion and documentation.
type InjectThreadSafe[T, *Metadata] = Inject[T, *Metadata]

@remimd remimd merged commit 684424d into dev May 26, 2025
3 checks passed
@remimd remimd deleted the threading branch May 26, 2025 09:32
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.

2 participants