Python allows to call a static method not only from a class, but also from an instance:
class X:
@staticmethod
def f():
print('f')
x = X()
X.f()
x.f() # same as above
This may be convenient when we only have an instance to work with; after all, who wants to write x.__class__.f() instead of x.f().
But I found that many readers of the code (including myself) tend to interpret x.f() as if it’s an instance method. That is, they assume that whatever is done either uses or changes x. In some cases, this even resulted in bugs (where the developer incorrectly interpreted the semantics of f).
So I was thinking to adopt a convention where all static methods are called only using the class object. Are there any static analysis tools that would warn me if this convention is violated?
I don’t think this amount of static checking is pythonic, but…
And you can test: