Skip to content
22 changes: 13 additions & 9 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from itertools import chain
from operator import itemgetter, attrgetter
from types import FunctionType, MethodType
from typing import Any, Union, Literal # When python 3.9 support is dropped replace Union with |
from typing import Any, Union, Literal, Generic, TypeVar # When python 3.9 support is dropped replace Union with |

from contextlib import contextmanager
CRITICAL = 50
Expand Down Expand Up @@ -56,6 +56,8 @@
gen_types,
)

T = TypeVar("T")

# Ideally setting param_pager would be in __init__.py but param_pager is
# needed on import to create the Parameterized class, so it'd need to precede
# importing parameterized.py in __init__.py which would be a little weird.
Expand Down Expand Up @@ -229,7 +231,7 @@ def _identity_hook(obj, val):
return val


class _Undefined:
class _TUndefined:
"""
Dummy value to signal completely undefined values rather than
simple None values.
Expand All @@ -245,7 +247,7 @@ def __repr__(self):
return '<Undefined>'


Undefined = _Undefined()
Undefined = _TUndefined()


@contextmanager
Expand Down Expand Up @@ -1056,7 +1058,9 @@ def _sorter(p):
cls.__signature__ = new_sig


class Parameter(_ParameterBase):


class Parameter(Generic[T], _ParameterBase):
"""
An attribute descriptor for declaring parameters.

Expand Down Expand Up @@ -1493,7 +1497,7 @@ def _update_state(self):
values, after the slot values have been set in the inheritance procedure.
"""

def __get__(self, obj, objtype): # pylint: disable-msg=W0613
def __get__(self, obj, objtype) -> T:
"""
Return the value for this Parameter.

Expand All @@ -1517,7 +1521,7 @@ def __get__(self, obj, objtype): # pylint: disable-msg=W0613
return result

@instance_descriptor
def __set__(self, obj, val):
def __set__(self, obj, val: T) -> None:
"""
Set the value for this Parameter.

Expand Down Expand Up @@ -1700,7 +1704,7 @@ def __setstate__(self,state):


# Define one particular type of Parameter that is used in this file
class String(Parameter):
class String(Parameter[T]):
r"""
A String Parameter, with a default value and optional regular expression (regex) matching.

Expand All @@ -1721,15 +1725,15 @@ def __init__(self, default="0.0.0.0", allow_None=False, **kwargs):
@typing.overload
def __init__(
self,
default="", *, regex=None,
default: T = "", *, regex=None,
doc=None, label=None, precedence=None, instantiate=False, constant=False,
readonly=False, pickle_default_value=True, allow_None=False, per_instance=True,
allow_refs=False, nested_refs=False
):
...

@_deprecate_positional_args
def __init__(self, default=Undefined, *, regex=Undefined, **kwargs):
def __init__(self, default: T = Undefined, *, regex=Undefined, **kwargs):
super().__init__(default=default, **kwargs)
self.regex = regex
self._validate(self.default)
Expand Down
Loading
Loading