8000 type: Infer parameters types for `__init__(default...)`, `__get__` and `__set__` by hoxbro · Pull Request #985 · holoviz/param · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

type: Infer parameters types for __init__(default...), __get__ and __set__ #985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
18 changes: 11 additions & 7 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 @@ -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
0