|
2 | 2 | import contextlib
|
3 | 3 | import contextvars
|
4 | 4 | import threading
|
5 |
| -from typing import Any, Dict, Union |
| 5 | +from typing import Any, Union |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class _CVar:
|
9 | 9 | """Storage utility for Local."""
|
10 | 10 |
|
11 | 11 | def __init__(self) -> None:
|
12 |
| - self._data: "contextvars.ContextVar[Dict[str, Any]]" = contextvars.ContextVar( |
13 |
| - "asgiref.local" |
14 |
| - ) |
| 12 | + self._data: dict[str, contextvars.ContextVar[Any]] = {} |
15 | 13 |
|
16 |
| - def __getattr__(self, key): |
17 |
| - storage_object = self._data.get({}) |
| 14 | + def __getattr__(self, key: str) -> Any: |
18 | 15 | try:
|
19 |
| - return storage_object[key] |
| 16 | + var = self._data[key] |
20 | 17 | except KeyError:
|
21 | 18 | raise AttributeError(f"{self!r} object has no attribute {key!r}")
|
22 | 19 |
|
| 20 | + try: |
| 21 | + return var.get() |
| 22 | + except LookupError: |
| 23 | + raise AttributeError(f"{self!r} object has no attribute {key!r}") |
| 24 | + |
23 | 25 | def __setattr__(self, key: str, value: Any) -> None:
|
24 | 26 | if key == "_data":
|
25 | 27 | return super().__setattr__(key, value)
|
26 | 28 |
|
27 |
| - storage_object = self._data.get({}) |
28 |
| - storage_object[key] = value |
29 |
| - self._data.set(storage_object) |
| 29 | + var = self._data.get(key) |
| 30 | + if var is None: |
| 31 | + self._data[key] = var = contextvars.ContextVar(key) |
| 32 | + var.set(value) |
30 | 33 |
|
31 | 34 | def __delattr__(self, key: str) -> None:
|
32 |
| - storage_object = self._data.get({}) |
33 |
| - if key in storage_object: |
34 |
| - del storage_object[key] |
35 |
| - self._data.set(storage_object) |
| 35 | + if key in self._data: |
| 36 | + del self._data[key] |
36 | 37 | else:
|
37 | 38 | raise AttributeError(f"{self!r} object has no attribute {key!r}")
|
38 | 39 |
|
|
0 commit comments