Suppose there is a method:
def train_model(self, out_dir='./out/',
test_size=0.2, train_size=None,
random_state=None, shuffle=True, stratify=None,
epochs=DEFAULT_EPOCHS, batch_size=DEFAULT_BATCH_SIZE):
...
self.model.train(test_size=test_size, train_size=train_size, random_state=random_state, shuffle=shuffle, stratify=stratify, epochs=epochs, batch_size=batch_size)
And inside this function another method with the same signature will be called, then I have to pass all the params manually. I don't want to use kwargs
in train_model
as it's a public method that may used by others, so I hope to keep the typing information. I don't know if there are methods to allow me to keep the typing information in kwargs of outer function.
The same functionality in TypeScript can be achieved using the Parameters utility types. For example,
function sum(a: int, b: int) {
return a + b;
}
type SumParamsType = Paramters<typeof sum>
// Then you can use the SumPramsType in other places.
A failed example of Python:
from typing import TypeVar
T = TypeVar('T')
def f1(a=1, b=2, c=3):
return a+b+c
# Is there anything like T=Parameters(type(f1)) in Python?
def f2(z=0, **kwargs: T):
return z+f1(**kwargs)
# T cannot capture the kwargs of f1 (of course it won't)
And this doesn't works either:
def f1(a=1, b=2, c=3):
return a+b+c
def f2(z=0, **kwargs: f1.__annotations__['kwargs']):
return z + f1(**kwargs)
# kwargs has the type Any