Say I have some function a.
def a(*,a_arg1:str,a_arg2:int):
...#Does something
Then I want to create another function which includes some of the functionality of a, and should therefore take its key word arguments. I can do it like so:
def b(*,b_arg1:str,**kwargs):
a(**kwargs)
This works if I only have one function I with to inherit arguments from, otherwise the kwargs unpacking needs to be a bit more complicated to avoid exceptions for extra arguments. This can probably be solved fairly easily with inspect and is not much of an issue. The other main issue, however, is that pylance doesn’t know what kwargs are supposed to be and will not show the hints for those arguments while coding. Which I very much like in my mess of code.
The other main solution is to just add every key word argument individually, but this comes across the issue of it being very difficult to edit. I would need to change a‘s arguments, b‘s arguments and wherever in b a is called.
What I am asking is, is there a function in python, lets call it magic, where I can do something like:
@magic(a)
def b(*,b_arg1:str,**kwargs):
a(**kwargs)
And it clues pylance in that a‘s arguments are not only appended to b‘s, but that they are requires or need certain types as necessary. This magic function doesn’t actually have to do anything, but clue in pylance to what is contained in **kwargs in a way that can edited easily.
It isn’t critical to my program or anything, but it would certainly make things a lot easier and faster with the way I tend to code.
I have looked at stuff which seems like it could (by name alone) be what I am looking for, like functools.wraps. functools.partial is kinda close to what I want, but I need it in the opposite direction, adding on more arguments.
Autumn Battisti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.