Skip to content

Commit

Permalink
Merge pull request #92 from ikalnytskyi/chore/mypy-var-annotated
Browse files Browse the repository at this point in the history
Turn mypy:var-annotated on
  • Loading branch information
ikalnytskyi authored Feb 11, 2024
2 parents 85fd399 + 7fb0b78 commit 51f3860
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,4 @@ disable_error_code = [
"misc",
"no-untyped-call",
"no-untyped-def",
"type-arg",
"var-annotated",
]
17 changes: 9 additions & 8 deletions src/picobox/ext/asgiscopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import picobox

if t.TYPE_CHECKING:
Store = contextvars.ContextVar[weakref.WeakKeyDictionary]
Store = weakref.WeakKeyDictionary[picobox.Scope, t.MutableMapping[t.Hashable, t.Any]]
StoreCtxVar = contextvars.ContextVar[Store]


_current_app_store: "Store" = contextvars.ContextVar(f"{__name__}.current-app-store")
_current_req_store: "Store" = contextvars.ContextVar(f"{__name__}.current-req-store")
_current_app_store: "StoreCtxVar" = contextvars.ContextVar(f"{__name__}.current-app-store")
_current_req_store: "StoreCtxVar" = contextvars.ContextVar(f"{__name__}.current-req-store")


class ScopeMiddleware:
Expand All @@ -34,7 +35,7 @@ def __init__(self, app):
# Since we want stored objects to be garbage collected as soon as the
# storing scope instance is destroyed, scope instances have to be
# weakly referenced.
self.store = weakref.WeakKeyDictionary()
self.store: "Store" = weakref.WeakKeyDictionary()

async def __call__(self, scope, receive, send):
"""Define scopes and invoke the ASGI application."""
Expand All @@ -56,7 +57,7 @@ async def __call__(self, scope, receive, send):
class _asgiscope(picobox.Scope):
"""A base class for ASGI scopes."""

_store_cvar: "Store"
_store_cvar: "StoreCtxVar"

@property
def _store(self) -> t.MutableMapping[t.Hashable, t.Any]:
Expand All @@ -72,10 +73,10 @@ def _store(self) -> t.MutableMapping[t.Hashable, t.Any]:
)

try:
store = store[self]
scope_store = store[self]
except KeyError:
store = store.setdefault(self, {})
return store
scope_store = store.setdefault(self, {})
return scope_store

def set(self, key: t.Hashable, value: t.Any) -> None:
self._store[key] = value
Expand Down
17 changes: 9 additions & 8 deletions src/picobox/ext/wsgiscopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
if t.TYPE_CHECKING:
from _typeshed.wsgi import StartResponse, WSGIApplication, WSGIEnvironment

Store = contextvars.ContextVar[weakref.WeakKeyDictionary]
Store = weakref.WeakKeyDictionary[picobox.Scope, t.MutableMapping[t.Hashable, t.Any]]
StoreCtxVar = contextvars.ContextVar[Store]


_current_app_store: "Store" = contextvars.ContextVar(f"{__name__}.current-app-store")
_current_req_store: "Store" = contextvars.ContextVar(f"{__name__}.current-req-store")
_current_app_store: "StoreCtxVar" = contextvars.ContextVar(f"{__name__}.current-app-store")
_current_req_store: "StoreCtxVar" = contextvars.ContextVar(f"{__name__}.current-req-store")


class ScopeMiddleware:
Expand All @@ -36,7 +37,7 @@ def __init__(self, app: "WSGIApplication") -> None:
# Since we want stored objects to be garbage collected as soon as the
# storing scope instance is destroyed, scope instances have to be
# weakly referenced.
self.store = weakref.WeakKeyDictionary()
self.store: "Store" = weakref.WeakKeyDictionary()

def __call__(
self,
Expand All @@ -63,7 +64,7 @@ def __call__(
class _wsgiscope(picobox.Scope):
"""A base class for WSGI scopes."""

_store_cvar: "Store"
_store_cvar: "StoreCtxVar"

@property
def _store(self) -> t.MutableMapping[t.Hashable, t.Any]:
Expand All @@ -79,10 +80,10 @@ def _store(self) -> t.MutableMapping[t.Hashable, t.Any]:
)

try:
store = store[self]
scope_store = store[self]
except KeyError:
store = store.setdefault(self, {})
return store
scope_store = store.setdefault(self, {})
return scope_store

def set(self, key: t.Hashable, value: t.Any) -> None:
self._store[key] = value
Expand Down

0 comments on commit 51f3860

Please sign in to comment.