Skip to content

Commit

Permalink
Wrap function rather than decorating method. Avoids varying stack dep…
Browse files Browse the repository at this point in the history
…ths.
  • Loading branch information
jaraco committed Mar 4, 2021
1 parent 7e7fc8c commit ed33213
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import functools
import itertools
import posixpath
import contextlib
import collections.abc

from ._compat import (
Expand Down Expand Up @@ -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):
Expand All @@ -221,7 +216,7 @@ class DeprecatedDict(dict):
>>> list(dd.values())
['bar']
>>> len(recwarn)
2
1
"""

_warn = functools.partial(
Expand All @@ -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):
Expand Down

0 comments on commit ed33213

Please sign in to comment.