GithubHelp home page GithubHelp logo

decorate classmethods about boltons HOT 3 CLOSED

mahmoud avatar mahmoud commented on May 18, 2024 4
decorate classmethods

from boltons.

Comments (3)

mahmoud avatar mahmoud commented on May 18, 2024

Haha, sorry to disappoint, but after some experimentation, it's quite clear that this API doesn't make sense. Have a look at this test:

def test_wraps_wrappers():
    call_list = []

    def call_list_appender(func):
        @wraps(func)
        def appender(*a, **kw):
            call_list.append((a, kw))
            return func(*a, **kw)
        return appender

    class Num(object):
        def __init__(self, num):
            self.num = num

        @call_list_appender
        @classmethod
        def added(cls, x, y=1):
            return cls(x + y)

    assert Num(1).num == 1
    assert Num.added(0, 2).num == 2

    assert len(call_list) == 1

All the details aside, the func that is passed to call_list_appender is a classmethod instance. Classmethods are not callable objects. Done deal. Pretty sure this is mentioned in Graham's gigantic decorator posts, too.

The solve is to simply reverse the order of the decoration in your code, and add better exception messaging to boltons, of course:

...
        @classmethod
        @call_list_appender
        def added(cls, x, y=1):
            return cls(x + y)
...

The above works fine for me. This is one of those corner cases that makes wrapt so complex, and another reason why I only call out wraps as a replacement for decorator.py, not wrapt. I'll leave this issue open until I have those boltons exceptions in place.

from boltons.

hynek avatar hynek commented on May 18, 2024

Well, you could simplify this to the fact that it’s about descriptors in general. :) I only used classmethods as a concrete example. And yeah, shit’s complicated but descriptors happen.

from boltons.

hynek avatar hynek commented on May 18, 2024

(please ping me when you release this fix so I can update the error message in the post :))

from boltons.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.