Skip to content

Commit

Permalink
Replace functools.wraps with six.wraps
Browse files Browse the repository at this point in the history
Previously we have been using functools.wraps. However if any decorator
that uses functools.wraps is used, some code that relies on __wrapped__
being present, e.g. pytest, will fail on Python2. Switching to six.wraps
will solve this problem, as it introduces this change as well and solves
the missing __wrapped__ attribute.

Signed-off-by: Vinzenz Feenstra <vfeenstr@redhat.com>
  • Loading branch information
vinzenz committed Oct 27, 2020
1 parent 287d684 commit a69c164
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions leapp/utils/clicmd.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import functools
import os
import sys

from argparse import ArgumentParser, _SubParsersAction, RawDescriptionHelpFormatter

import six

from leapp.exceptions import CommandDefinitionError, UsageError, CommandError


Expand Down Expand Up @@ -277,7 +278,7 @@ def wrapper(f):


def _ensure_command(wrapped):
@functools.wraps(wrapped)
@six.wraps(wrapped)
def wrapper(f):
if not hasattr(f, 'command'):
f.command = Command('')
Expand Down
9 changes: 5 additions & 4 deletions leapp/utils/deprecation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime
import functools
import inspect
import warnings
from collections import namedtuple

import six

from leapp.models import Model


Expand Down Expand Up @@ -31,7 +32,7 @@ def decorator(item):
' Use the decorator on the affected methods instead of the current class.'
)

@functools.wraps(target_item)
@six.wraps(target_item)
def process_wrapper(*args, **kwargs):
# we need to remove later just items that we add right now
# added_items == new items we add now to ...
Expand Down Expand Up @@ -106,14 +107,14 @@ def do_warn():
if inspect.isclass(item):
old_init = item.__init__

@functools.wraps(item.__init__, assigned=('__name__', '__doc__'))
@six.wraps(item.__init__, assigned=('__name__', '__doc__'))
def wrapper(*args, **kwargs):
do_warn()
return old_init(*args, **kwargs)
item.__init__ = wrapper
result = item
else:
@functools.wraps(item)
@six.wraps(item)
def wrapper(*args, **kwargs):
do_warn()
return item(*args, **kwargs)
Expand Down
12 changes: 12 additions & 0 deletions tests/scripts/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import leapp.utils.audit
from leapp.messaging.inprocess import InProcessMessaging
from leapp.repository.scan import scan_repo
from leapp.utils.deprecation import suppress_deprecation, deprecated


@pytest.fixture(scope='module')
Expand All @@ -34,3 +35,14 @@ def test_deprecations(repository):
entries = [entry for entry in entries
if datetime.datetime.strptime(entry['stamp'].rstrip('Z'), '%Y-%m-%dT%H:%M:%S.%f') > start]
assert entries and len(entries) == 5


# This is test to show the current suppress_deprecation limitation
@deprecated(since='2011-11-11', message='never to be seen')
def foobar():
pass


@suppress_deprecation(foobar)
def test_suppress_with_fixture(monkeypatch):
assert monkeypatch

0 comments on commit a69c164

Please sign in to comment.