Skip to content

Commit

Permalink
Extract list method passthrough into a factory function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Aug 29, 2021
1 parent 7e95bd2 commit 5875f39
Showing 1 changed file with 20 additions and 39 deletions.
59 changes: 20 additions & 39 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,52 +290,33 @@ class DeprecatedList(list):
stacklevel=pypy_partial(2),
)

def __setitem__(self, *args, **kwargs):
self._warn()
return super().__setitem__(*args, **kwargs)

def __delitem__(self, *args, **kwargs):
self._warn()
return super().__delitem__(*args, **kwargs)

def append(self, *args, **kwargs):
self._warn()
return super().append(*args, **kwargs)

def reverse(self, *args, **kwargs):
self._warn()
return super().reverse(*args, **kwargs)

def extend(self, *args, **kwargs):
self._warn()
return super().extend(*args, **kwargs)

def pop(self, *args, **kwargs):
self._warn()
return super().pop(*args, **kwargs)

def remove(self, *args, **kwargs):
self._warn()
return super().remove(*args, **kwargs)

def __iadd__(self, *args, **kwargs):
self._warn()
return super().__iadd__(*args, **kwargs)
def _wrap_deprecated_method(method_name: str): # type: ignore
def wrapped(self, *args, **kwargs):
self._warn()
return getattr(super(), method_name)(*args, **kwargs)

return wrapped

for method_name in [
'__setitem__',
'__delitem__',
'append',
'reverse',
'extend',
'pop',
'remove',
'__iadd__',
'insert',
'sort',
]:
locals()[method_name] = _wrap_deprecated_method(method_name)

def __add__(self, other):
if not isinstance(other, tuple):
self._warn()
other = tuple(other)
return self.__class__(tuple(self) + other)

def insert(self, *args, **kwargs):
self._warn()
return super().insert(*args, **kwargs)

def sort(self, *args, **kwargs):
self._warn()
return super().sort(*args, **kwargs)

def __eq__(self, other):
if not isinstance(other, tuple):
self._warn()
Expand Down

0 comments on commit 5875f39

Please sign in to comment.