Skip to content

Commit

Permalink
Merge pull request #12 from NoamNol/patch-1
Browse files Browse the repository at this point in the history
Use ContextVar instead of threading.local
  • Loading branch information
vmalloc authored May 23, 2024
2 parents cd52e13 + a98c3bb commit be36635
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions vintage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
from contextlib import contextmanager
from contextvars import ContextVar
import functools
import threading
import warnings

from six import string_types


class _Local(threading.local):
enabled = True

_local = _Local()
_deprecation_warning_enabled: ContextVar[bool] = ContextVar('_deprecation_warning_enabled', default=True)


@contextmanager
def get_no_deprecations_context():
"""Disables deprecation messages temporarily
"""
prev_enabled = _local.enabled
_local.enabled = False
previous_enabled = _deprecation_warning_enabled.set(False)
try:
yield
finally:
_local.enabled = prev_enabled
_deprecation_warning_enabled.reset(previous_enabled)


def warn_deprecation(message, frame_correction=0):
if _local.enabled:
if _deprecation_warning_enabled.get():
warnings.warn(message, DeprecationWarning, stacklevel=2+frame_correction)


Expand Down

0 comments on commit be36635

Please sign in to comment.