-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for static deletion #543
Conversation
Damn, I said it could not be done in Python and obviously you immediately show it can. This is really cool but I have no idea how safe it is to take that approach, especially as we'd have to do this for all the instance methods in the lib (though there are not that many beyond retrieve). How would it work for nested resources by the way? In the sense of trying to have I'll defer to @brandur-stripe on whether this is the wrong approach versus just doing a new major revision that only has static methods. |
Classes for parent resources already have class methods for all nested resources operations, e.g. |
Wow OB, this is some serious Gandalf-level Python wizardry. Nice patch! So I would probably have said that my first preference would have been to come up with two slightly different method names for the static versus non-static action even if the difference was somewhat arbitrary ( That said, I appreciate that "delete" is probably the right word from an API standpoint (as it meshes well with |
8a9adb6
to
f04ac92
Compare
I've updated the PR with a slightly more complicated decorator/descriptor (a desecrator? 😛) but I think the result is really nice. The new decorator is applied directly on a regular instance method, and takes an alternate class method name as its parameter. When the method is called on the class, then the alternate class method is called, otherwise the regular instance method is called. |
Sorry, approved a little prematurely, but this approach seems okay to me. |
NP, thanks Brandur! We're just going to add some more tests before merging. |
f04ac92
to
707044f
Compare
707044f
to
bd2d74d
Compare
@ob-stripe I just did all the examples, PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
bd2d74d
to
d085b71
Compare
@ob-stripe I force pushed as I had forgotten to do Terminal. Please make sure I did not miss any other |
Released as 2.24.0. |
r? @brandur-stripe @remi-stripe
A very hacky and experimental way of adding support for static deletion on deletable resource classes.
Normally, Python doesn't allow you to have both a class and instance method with the same name. This PR works around this limitation by defining a special
class_or_instance_method
decorator. The decorator is defined as a non-data descriptor. Descriptors receive both the instance and the class as arguments in their__get__
method. The method checks if the instance variable (obj
) isNone
or not to decide whether the method should be called as a class method (with the type variableobjtype
as the first argument) or as an instance method (with the instance variableobj
as the first argument).The actual "class or instance method" can then check if its first argument is a type or not to decide whether it was called from a class or from an instance and behave appropriately.
Tagged as WIP because this is rather hacky and I want to get your thoughts on it first. If we're comfortable with this approach, I'll add some safeguards around arguments (right now if you call e.g.
stripe.Customer.delete()
without any arguments, you get a very unclear "tuple index out of range" error because the code tries to accessargs[0]
to get the ID) and write additional tests.