Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33043 closed Bug (fixed)

method_decorator() should preserve wrapper assignments

Reported by: vinay karanam Owned by: nobody
Component: Utilities Version: dev
Severity: Normal Keywords:
Cc: Chris Jerdonek Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

the function that is passed to the decorator is a partial object and does not have any of the attributes expected from a function i.e. __name__, __module__ etc...

consider the following case

def logger(func):
    @wraps(func)
    def inner(*args, **kwargs):
        try:
            result = func(*args, **kwargs)
        except Exception as e:
            result = str(e)
        finally:
            logger.debug(f"{func.__name__} called with args: {args} and kwargs: {kwargs} resulting: {result}")
    return inner


class Test:
    @method_decorator(logger)
    def hello_world(self):
        return "hello"

Test().test_method()

This results in the following exception
AttributeError: 'functools.partial' object has no attribute '__name__'

Change History (6)

comment:1 by Mariusz Felisiak, 3 years ago

Cc: Chris Jerdonek added

This behavior has been changed in f434f5b84f7fcea9a76a551621ecce70786e2899. Chris, can you take a look?

comment:2 by Chris Jerdonek, 3 years ago

Has patch: set

PR: https://github.com/django/django/pull/14787

Chris, can you take a look?

OK.

comment:3 by Carlton Gibson, 3 years ago

Triage Stage: UnreviewedAccepted

OK, I'm going to accept this for review. The regression test in the PR shows a change of behaviour at f434f5b84f7fcea9a76a551621ecce70786e2899 as Mariusz said.

Since the change was in Django 2.2 it would no longer qualify for a backport.

comment:4 by Tim McCurrach, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: newclosed

In 8806e88:

Fixed #33043 -- Made method_decorator() preserve wrapper assignments.

Regression in f434f5b84f7fcea9a76a551621ecce70786e2899.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 354bbf1f:

[4.0.x] Fixed #33043 -- Made method_decorator() preserve wrapper assignments.

Regression in f434f5b84f7fcea9a76a551621ecce70786e2899.

Backport of 8806e8809e023017e6958b9fa0bbd960938e0a91 from main

Note: See TracTickets for help on using tickets.
Back to Top