I'm trying to write a documentation testing tool which checks whether the docstrings match the actual function signature (among other things). However, I've run into a bit of a roadblock. I can't find a way to figure out whether a given function belongs to a class or not.
import inspect
def func1():
pass
class Foo:
def method(self):
pass
print(inspect.ismethod(func1)) # prints False
print(inspect.ismethod(Foo().method)) # prints True
print(inspect.ismethod(Foo.method)) # prints False - I want something that prints True here
The problem is that methods usually have self
as their first parameter, and this is never documented (for obvious reasons). This would cause my tests to fail, since they'd encounter a parameter that's not documented.
I would prefer to not explicitly check if the first parameter is called self
and skip it, because a) nothing prevents a function from having a parameter called self
and b) the name self
itself is a matter of convention (so you could have a method with a first param that's called this_is_definitely_not_self
) and my tool would fail in either case. I also can't just initialize an object and check if the function is a method, because this would fail with any class that takes required parameters in its __init__
.
So the question is, is there any way I can detect if a function is a method before it is actually bound to an object? Or do I need to resign myself to just checking if the first parameter is called self
?
Aucun commentaire:
Enregistrer un commentaire