Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### Unreleased

- Fixed `wraps` for callables without `__dict__` and classes with read-only attribute mappings.
Fixes [#94](https://github.com/smarie/python-makefun/issues/94).

### 1.16.0 - Support for 3.14, dropped support for < 3.9

- Removed official support for python versions `<3.9`. These versions will not run in CI anymore.
Expand Down
2 changes: 1 addition & 1 deletion src/makefun/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ def _get_args_for_wrapping(wrapped, new_sig, remove_args, prepend_args, append_a
co_name = code.co_name

# attributes: start from the wrapped dict, add '__wrapped__' if needed, and override with all attrs.
all_attrs = copy(getattr_partial_aware(wrapped, '__dict__'))
all_attrs = dict(getattr_partial_aware(wrapped, '__dict__', {}))
# PEP362: always set `__wrapped__`, and if signature was changed, set `__signature__` too
all_attrs["__wrapped__"] = wrapped
if has_new_sig:
Expand Down
92 changes: 89 additions & 3 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import inspect
import sys

Expand Down Expand Up @@ -128,13 +129,18 @@ def test_wraps_dict():
def foo():
pass

foo._mark = True
foo._mark = []
foo._label = 'original'

@wraps(foo)
@wraps(foo, _label='wrapper')
def g():
pass

assert g._mark is True
assert g._mark is foo._mark
assert g._label == 'wrapper'
assert foo._label == 'original'
assert g.__wrapped__ is foo
assert not hasattr(foo, '__wrapped__')


def test_issue_62():
Expand Down Expand Up @@ -284,6 +290,86 @@ def test_issue_91():
assert is_identifier("hello__bag")


@pytest.mark.parametrize('wrapped, args, expected', [
(len, ('abc',), 3),
(str.upper, ('abc',), 'ABC'),
(functools.partial(len), ('abc',), 3),
(functools.partial(str.upper), ('abc',), 'ABC'),
])
def test_wraps_builtin_issue_94(wrapped, args, expected):
"""Builtins and their partials do not require a writable attribute dict."""
@wraps(wrapped, _mark=True)
def wrapper(*args, **kwargs):
return wrapped(*args, **kwargs)

assert wrapper(*args) == expected
assert signature(wrapper) == signature(wrapped)
assert wrapper.__wrapped__ is wrapped
assert wrapper._mark is True
assert not hasattr(wrapped, '_mark')


def test_wraps_setattr_issue_94():
"""The wrapper_descriptor from the original report can be decorated."""
class Wrapped(object):
@wraps(object.__setattr__)
def __setattr__(self, name, value):
object.__setattr__(self, name, value + 1)

instance = Wrapped()
instance.value = 3
assert instance.value == 4
assert signature(Wrapped.__setattr__) == signature(object.__setattr__)
assert Wrapped.__setattr__.__wrapped__ is object.__setattr__


@pytest.mark.parametrize('use_partial', [False, True])
def test_wraps_slots_issue_94(use_partial):
"""Callable instances with slots also work through functools.partial."""
class Wrapped(object):
__slots__ = ()

def __call__(self, value):
return value + 1

wrapped = Wrapped()
if use_partial:
wrapped = functools.partial(wrapped)

@wraps(wrapped)
def wrapper(*args, **kwargs):
return wrapped(*args, **kwargs)

assert wrapper(3) == 4
assert signature(wrapper) == signature(wrapped)
assert wrapper.__wrapped__ is wrapped


def test_wraps_class_issue_94():
"""A class's read-only attribute mapping is copied without modification."""
class Wrapped(object):
"""Wrapped class documentation."""
_mark = []

def __init__(self, value):
self.value = value

original_attrs = dict(Wrapped.__dict__)

@wraps(Wrapped)
def wrapper(*args, **kwargs):
return Wrapped(*args, **kwargs)

instance = wrapper(3)
assert isinstance(instance, Wrapped)
assert instance.value == 3
assert signature(wrapper) == signature(Wrapped)
assert wrapper.__wrapped__ is Wrapped
assert wrapper.__doc__ == Wrapped.__doc__
assert wrapper._mark is Wrapped._mark
assert dict(Wrapped.__dict__) == original_attrs


def test_issue_98():
class A(str):
def __str__(self):
Expand Down