From ed33213268c4cda0079649a410cfbfc679a90313 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 4 Mar 2021 05:26:12 -0500 Subject: [PATCH] Wrap function rather than decorating method. Avoids varying stack depths. --- importlib_metadata/__init__.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 5dc51d3e..44160596 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -12,7 +12,6 @@ import functools import itertools import posixpath -import contextlib import collections.abc from ._compat import ( @@ -193,13 +192,9 @@ def _from_text_for(cls, text, dist): return cls(ep._for(dist) for ep in EntryPoint._from_text(text)) -class Flake8Bypass(warnings.catch_warnings, contextlib.ContextDecorator): - def __enter__(self): - super().__enter__() - is_flake8 = any( - 'flake8' in str(frame.filename) for frame in inspect.stack()[:5] - ) - is_flake8 and warnings.simplefilter('ignore', DeprecationWarning) +def flake8_bypass(func): + is_flake8 = any('flake8' in str(frame.filename) for frame in inspect.stack()[:5]) + return func if not is_flake8 else lambda: None class DeprecatedDict(dict): @@ -221,7 +216,7 @@ class DeprecatedDict(dict): >>> list(dd.values()) ['bar'] >>> len(recwarn) - 2 + 1 """ _warn = functools.partial( @@ -235,9 +230,8 @@ def __getitem__(self, name): self._warn() return super().__getitem__(name) - @Flake8Bypass() def get(self, name, default=None): - self._warn() + flake8_bypass(self._warn)() return super().get(name, default) def __iter__(self):