[Python-ideas] Keyword only argument on function call
Steven D'Aprano
steve at pearwood.info
Tue Sep 11 06:53:55 EDT 2018
On Tue, Sep 11, 2018 at 10:12:56AM +0200, Chris Barker via Python-ideas wrote:
> hmm -- this is a trick -- in those cases, I find myself using *args,
> **kwargs when overloading methods. But that does hide the method signature,
> which is really unfortunate. IT works pretty well for things like GUI
> toolkits, where you might be subclassing a wx.Window, and the docs for
> wx.Window are pretty easy to find, but for you own custom classes with
> nested subclassing, it does get tricky.
Do we need to solve this in the interpreter? Surely this is an argument
for better tooling.
A sophisticated IDE should never be a *requirement* for coding in
Python, but good tools can make a big difference in the pleasantness or
otherwise of coding. Those tools don't have to be part of the language.
At least for methods, code completers ought to be able to search the MRO
for the first non-**kwargs signature and display parameters from further
up the MRO:
class Parent:
def method(self, spam):
pass
class Child(Parent):
def method(self, **kwargs):
pass
Now when I type
Child().method(<TAB>)
the IDE could search the MRO and find "spam" is the parameter. That
becomes a "quality of IDE" issue, and various editors and IDEs can
compete to have the best implementation.
Or perhaps we could have an officially blessed way to give tools a hint
as to what the real signature is.
class Child(Parent):
@signature_hint(Parent.method)
def method(self, **kwargs):
pass
Statically, that tells the IDE that "true" signature of Child.method can
be found from Parent.method; dynamically, the decorator might copy that
signature into Child.method.__signature_hint__ for runtime
introspection by tools like help().
The beauty of this is that it is independent of inheritance. We could
apply this decorator to any function, and point it to any other function
or method, or even a signature object.
@signature_hint(open)
def my_open(*args, **kwargs):
...
And being optional, it won't increase the size of any functions unless
you specifically decorate them.
--
Steve
More information about the Python-ideas
mailing list