Decorators

File 12 appendix/decorator.py

from functools import wraps


def deco_without_wraps(func):
    def _func(*args, **kwargs):
        return func(*args, **kwargs)

    return _func


def deco_with_wraps(func):
    @wraps(func)
    def _func(*args, **kwargs):
        return func(*args, **kwargs)

    return _func

File 13 appendix/decorated.py

"""Decorator examples."""
import pytest

from appendix.decorator import deco_with_wraps, deco_without_wraps


@deco_without_wraps
def func_without_wraps():
    """Decorated function without `wraps`."""


@deco_with_wraps
def func_with_wraps():
    """Decorated function with `wraps`."""


@deco_with_wraps
@deco_with_wraps
def func_with_wraps_double():
    """Doubly decorated function with `wraps`."""


@pytest.fixture
def fixture():
    """Fixture."""
    yield 1


@pytest.fixture
@deco_with_wraps
def fixture_with_wraps():
    """Fixture."""
    yield 1
import inspect

from appendix.decorated import func_with_wraps, func_without_wraps

funcs = [func_with_wraps, func_without_wraps]
for func in funcs:
    sourcefile = inspect.getsourcefile(func)
    is_wrapped = hasattr(func, '__wrapped__')
    print(f"[{func.__name__}]: {sourcefile}, {is_wrapped}")

[1] 2020-08-03 20:47:45 (45.0ms) python3 (543ms)

[func_with_wraps]: ../../examples\appendix\decorator.py, True
[_func]: ../../examples\appendix\decorator.py, False
## ![mkapi](appendix.decorated|all)
module

appendix.decorated

Decorator examples.

Functions
function

appendix.decorated.func_with_wraps()

Decorated function with wraps.

function

appendix.decorated.func_with_wraps_double()

Doubly decorated function with wraps.

generator

appendix.decorated.fixture()

Fixture.

generator

appendix.decorated.fixture_with_wraps()

Fixture.