Hi all, Is it feasible to add a bound parameter to ParamSpec? Sometimes, manually specifying the bound can greatly reduce the difficulty of using ParamSpec.
For example, I have a base class defined like this:
class IntView:
def __init__(self, init_data: int):
...
Then I want to inherit IntView like this:
class RangedIntView(IntView):
def __init__(self, init_data: int, min_value: int, max_value: int):
super().__init__(init_data)
self._min_value = min_value
self._max_value = max_value
Since the parameters of IntView.__init__ may be changed in the future, I want to use *args and **kwargs in RangedIntView.__init__ so I don’t need to adjust RangeIntView.__init__'s parameters then. Like this:
class RangedIntView(IntView):
def __init__(
self,
min_value: int,
max_value: int,
*args,
some_key_arg: bool = False,
**kwargs,
):
super().__init__(*args, **kwargs)
self._min_value = min_value
self._max_value = max_value
self._some_key_arg = some_key_args
Now I want to add type hints to RangedIntView.__init__. I want to borrow the type hints from IntView.__init__. Something like this:
P = ParamSpec("P", bound=IntView.__init__)
class RangedIntView(IntView):
def __init__(
self,
min_value: int,
max_value: int,
*args: P.args,
some_key_arg: bool = False,
**kwargs: P.kwargs,
):
super().__init__(*args, **kwargs)
self._min_value = min_value
self._max_value = max_value
self._some_key_arg = some_key_args
We can further add a Super then we can reuse this ParamSpec:
from typing import Super
P = ParamSpec("P", bound=Super)
class RangedIntView(IntView):
def __init__(
self,
min_value: int,
max_value: int,
*args: P.args,
some_key_arg: bool = False,
**kwargs: P.kwargs,
):
super().__init__(*args, **kwargs)
self._min_value = min_value
self._max_value = max_value
self._some_key_arg = some_key_args
def some_other_function(self, arg_1: int, *args: P.args, **kwargs: P.kwargs):
# do something with args_1
super().some_other_function(*args, **kwargs)
Feedback appreciated. Thanks!