From 75219626374d5fad0dc7cfd0142cf3b138724d53 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Sat, 21 Nov 2026 19:18:26 +0800 Subject: [PATCH] Fix wraps for callables without attribute dicts --- docs/changelog.md | 5 +++ src/makefun/main.py | 2 +- tests/test_issues.py | 92 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 4 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 5d9320f..b628ed0 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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. diff --git a/src/makefun/main.py b/src/makefun/main.py index e682155..91770a6 100644 --- a/src/makefun/main.py +++ b/src/makefun/main.py @@ -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: diff --git a/tests/test_issues.py b/tests/test_issues.py index b2c5e49..3d63a84 100644 --- a/tests/test_issues.py +++ b/tests/test_issues.py @@ -1,3 +1,4 @@ +import functools import inspect import sys @@ -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(): @@ -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):