Skip to content

Commit

Permalink
Context: initialize token at __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Jan 4, 2025
1 parent 318cae0 commit e638af3
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/marshmallow/experimental/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
class Context(contextlib.AbstractContextManager, typing.Generic[_T]):
def __init__(self, context: _T) -> None:
self.context = context
self.token: contextvars.Token | None = None

def __enter__(self) -> None:
self.token = _CURRENT_CONTEXT.set(self.context)

def __exit__(self, *args, **kwargs) -> None:
_CURRENT_CONTEXT.reset(self.token)
_CURRENT_CONTEXT.reset(typing.cast(contextvars.Token, self.token))

@classmethod
def get(cls, default=...) -> _T:
Expand Down

0 comments on commit e638af3

Please sign in to comment.