bpo-32380 Fix singledispatch interaction with methods by emmatyping · Pull Request #4987 · python/cpython (original) (raw)

Hey @ncoghlan and @ethanhs, I'd been lurking the cpython PRs and had some time today, so I made a quick gist of a possible descriptor-aware implementation of singlemethoddispatch. It's far from complete, but it does work for instance, abstract and class methods. (I haven't tried static methods or partial methods yet). I thought I'd link to it here in case it can provide you with some direction / inspiration.

Nice to haves (for my implementation, at least), would be a mechanism which wraps up any methods registered by the decorated method in the same decorators in which that method is wrapped; e.g., if Foo.bar is a classmethod, then Foo.bar.register(int, baz) should make baz a class method too. On the other hand, that's complicated to implement and possibly not even the desired behavior, so I'm putting it on the back-burner for now.

Anyway, here's the gist. Heavily inspired by the partialmethod implementation, as you'll see: https://gist.github.com/ianhoffman/e25899e8fca3c1ef8772f331b14031d0