Skip to content

Commit 46117f1

Browse files
authored
fix: ⚡️ ScopeState could be created before the precondition was checked
1 parent d2b5056 commit 46117f1

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

injection/_core/module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import threading
34
from abc import ABC, abstractmethod
45
from collections import OrderedDict, deque
56
from collections.abc import (
@@ -27,7 +28,6 @@
2728
)
2829
from inspect import signature as inspect_signature
2930
from logging import Logger, getLogger
30-
from threading import Lock
3131
from types import MethodType
3232
from typing import (
3333
Any,
@@ -982,7 +982,7 @@ class InjectMetadata[**P, T](Caller[P, T], EventListener):
982982

983983
def __init__(self, wrapped: Callable[P, T], /, threadsafe: bool) -> None:
984984
self.__dependencies = Dependencies.empty()
985-
self.__lock = Lock() if threadsafe else nullcontext()
985+
self.__lock = threading.Lock() if threadsafe else nullcontext()
986986
self.__owner = None
987987
self.__tasks = deque()
988988
self.__wrapped = wrapped

injection/_core/scope.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,18 @@ def get_scope(name, default=...): # type: ignore[no-untyped-def]
157157
def _bind_scope(name: str, scope: Scope, shared: bool) -> Iterator[None]:
158158
if shared:
159159
is_already_defined = bool(get_active_scopes(name))
160-
state = __SHARED_SCOPES[name]
160+
states = __SHARED_SCOPES
161161

162162
else:
163163
is_already_defined = bool(get_scope(name, default=None))
164-
state = __CONTEXTUAL_SCOPES[name]
164+
states = __CONTEXTUAL_SCOPES
165165

166166
if is_already_defined:
167167
raise ScopeAlreadyDefinedError(
168168
f"Scope `{name}` is already defined in the current context."
169169
)
170170

171-
with state.bind(scope):
171+
with states[name].bind(scope):
172172
yield
173173

174174

uv.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)