Skip to content

Commit

Permalink
Add types for all Widget and Pane class variables
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jun 10, 2022
1 parent 0749371 commit c89d8e4
Show file tree
Hide file tree
Showing 42 changed files with 792 additions and 382 deletions.
8 changes: 7 additions & 1 deletion panel/interact.py
Expand Up @@ -8,11 +8,14 @@
Copyright (c) Jupyter Development Team and PyViz Development Team.
Distributed under the terms of the Modified BSD License.
"""
from __future__ import annotations

import types

from collections import OrderedDict
from inspect import getcallargs
from numbers import Integral, Real
from typing import TYPE_CHECKING

try: # Python >= 3.3
from collections.abc import Iterable, Mapping
Expand Down Expand Up @@ -42,6 +45,9 @@
TextInput, Widget,
)

if TYPE_CHECKING:
from bokeh.model import Model


def _get_min_max_value(min, max, value=None, step=None):
"""Return min, max, value given input values with possible None."""
Expand Down Expand Up @@ -208,7 +214,7 @@ def update_pane(change):
watcher = widget.param.watch(update_pane, pname)
self._callbacks.append(watcher)

def _cleanup(self, root):
def _cleanup(self, root: Model | None = None) -> None:
self._inner_layout._cleanup(root)
super()._cleanup(root)

Expand Down
5 changes: 3 additions & 2 deletions panel/io/location.py
Expand Up @@ -79,7 +79,8 @@ def _get_model(
return model

def get_root(
self, doc: Optional[Document] = None, comm: Optional[Comm] = None, preprocess: bool = True
self, doc: Optional[Document] = None, comm: Optional[Comm] = None,
preprocess: bool = True
) -> 'Model':
doc = init_doc(doc)
root = self._get_model(doc, comm=comm)
Expand All @@ -88,7 +89,7 @@ def get_root(
self._documents[doc] = root
return root

def _cleanup(self, root: Optional['Model']) -> None:
def _cleanup(self, root: Model | None = None) -> None:
if root:
if root.document in self._documents:
del self._documents[root.document]
Expand Down
3 changes: 2 additions & 1 deletion panel/io/notifications.py
Expand Up @@ -64,7 +64,8 @@ def __init__(self, **params):
self._notification_watchers = {}

def get_root(
self, doc: Optional['Document'] = None, comm: Optional['Comm'] = None, preprocess: bool = True
self, doc: Optional[Document] = None, comm: Optional[Comm] = None,
preprocess: bool = True
) -> 'Model':
root = super().get_root(doc, comm, preprocess)
self._documents[doc] = root
Expand Down
7 changes: 5 additions & 2 deletions panel/layout/accordion.py
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import ClassVar, Mapping
from typing import TYPE_CHECKING, ClassVar, Mapping

import param

Expand All @@ -9,6 +9,9 @@
from .base import NamedListPanel
from .card import Card

if TYPE_CHECKING:
from bokeh.model import Model


class Accordion(NamedListPanel):
"""
Expand Down Expand Up @@ -126,7 +129,7 @@ def _get_objects(self, model, old_objects, doc, root, comm=None):
self._update_active()
return new_models

def _cleanup(self, root):
def _cleanup(self, root: Model | None = None) -> None:
for panel in self._panels.values():
panel._cleanup(root)
super()._cleanup(root)
Expand Down
12 changes: 8 additions & 4 deletions panel/layout/base.py
Expand Up @@ -641,7 +641,7 @@ def _process_param_change(self, params: Dict[str, Any]) -> Dict[str, Any]:
params['css_classes'] = css_classes
return super()._process_param_change(params)

def _cleanup(self, root: 'Model' | None):
def _cleanup(self, root: Model | None = None) -> None:
if root is not None and root.ref['id'] in state._fake_roots:
state._fake_roots.remove(root.ref['id'])
super()._cleanup(root)
Expand Down Expand Up @@ -676,7 +676,7 @@ def _process_param_change(self, params: Dict[str, Any]) -> Dict[str, Any]:
params['css_classes'] = css_classes
return super()._process_param_change(params)

def _cleanup(self, root: 'Model' | None) -> None:
def _cleanup(self, root: Model | None = None) -> None:
if root is not None and root.ref['id'] in state._fake_roots:
state._fake_roots.remove(root.ref['id'])
super()._cleanup(root)
Expand Down Expand Up @@ -764,9 +764,13 @@ class WidgetBox(ListPanel):
be specified as a two-tuple of the form (vertical, horizontal)
or a four-tuple (top, right, bottom, left).""")

_source_transforms = {'disabled': None, 'horizontal': None}
_source_transforms: ClassVar[Mapping[str, str | None]] = {
'disabled': None, 'horizontal': None
}

_rename: ClassVar[Mapping[str, str | None]] = {'objects': 'children', 'horizontal': None}
_rename: ClassVar[Mapping[str, str | None]] = {
'objects': 'children', 'horizontal': None
}

@property
def _bokeh_model(self) -> Type['Model']: # type: ignore
Expand Down
17 changes: 12 additions & 5 deletions panel/layout/card.py
@@ -1,12 +1,17 @@
from __future__ import annotations

from typing import ClassVar, Mapping
from typing import (
TYPE_CHECKING, ClassVar, List, Mapping, Type,
)

import param

from ..models import Card as BkCard
from .base import Column, ListPanel, Row

if TYPE_CHECKING:
from bokeh.model import Model


class Card(Column):
"""
Expand Down Expand Up @@ -63,11 +68,13 @@ class Card(Column):
A title to be displayed in the Card header, will be overridden
by the header if defined.""")

_bokeh_model = BkCard
_bokeh_model: ClassVar[Type[Model]] = BkCard

_linked_props = ['collapsed']
_linked_props: ClassVar[List[str]] = ['collapsed']

_rename: ClassVar[Mapping[str, str | None]] = dict(Column._rename, title=None, header=None, title_css_classes=None)
_rename: ClassVar[Mapping[str, str | None]] = dict(
Column._rename, title=None, header=None, title_css_classes=None
)

def __init__(self, *objects, **params):
self._header_layout = Row(css_classes=['card-header-row'],
Expand All @@ -77,7 +84,7 @@ def __init__(self, *objects, **params):
self.param.watch(self._update_header, ['title', 'header', 'title_css_classes'])
self._update_header()

def _cleanup(self, root):
def _cleanup(self, root: Model | None = None) -> None:
super()._cleanup(root)
self._header_layout._cleanup(root)

Expand Down
16 changes: 13 additions & 3 deletions panel/layout/grid.py
Expand Up @@ -7,7 +7,9 @@

from collections import OrderedDict, namedtuple
from functools import partial
from typing import ClassVar, Mapping
from typing import (
TYPE_CHECKING, Any, ClassVar, Dict, Mapping, Optional,
)

import numpy as np
import param
Expand All @@ -19,6 +21,11 @@
ListPanel, Panel, _col, _row,
)

if TYPE_CHECKING:
from bokeh.document import Document
from bokeh.model import Model
from pyviz_comms import Comm


class GridBox(ListPanel):
"""
Expand Down Expand Up @@ -155,7 +162,10 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
self._link_props(model, self._linked_props, doc, root, comm)
return model

def _update_model(self, events, msg, root, model, doc, comm=None):
def _update_model(
self, events: Dict[str, param.parameterized.Event], msg: Dict[str, Any],
root: Model, model: Model, doc: Document, comm: Optional[Comm]
) -> None:
from ..io import state

msg = dict(msg)
Expand Down Expand Up @@ -327,7 +337,7 @@ def _object_grid(self):
grid[y, x] = {((y0, x0, y1, x1), obj)}
return grid

def _cleanup(self, root):
def _cleanup(self, root: Model | None = None) -> None:
super()._cleanup(root)
for p in self.objects.values():
p._cleanup(root)
Expand Down
7 changes: 5 additions & 2 deletions panel/layout/tabs.py
Expand Up @@ -4,7 +4,7 @@
from __future__ import annotations

from collections import defaultdict
from typing import ClassVar, Mapping
from typing import TYPE_CHECKING, ClassVar, Mapping

import param

Expand All @@ -14,6 +14,9 @@
from ..viewable import Layoutable
from .base import NamedListPanel

if TYPE_CHECKING:
from bokeh.model import Model


class Tabs(NamedListPanel):
"""
Expand Down Expand Up @@ -74,7 +77,7 @@ def _update_names(self, event):
self.param.active.bounds = (0, len(event.new)-1)
super()._update_names(event)

def _cleanup(self, root):
def _cleanup(self, root: Model | None = None) -> None:
super()._cleanup(root)
if root.ref['id'] in self._panels:
del self._panels[root.ref['id']]
Expand Down
6 changes: 3 additions & 3 deletions panel/pane/alert.py
Expand Up @@ -5,7 +5,7 @@
"""
from __future__ import annotations

from typing import ClassVar, Mapping
from typing import Any, ClassVar, Mapping

import param

Expand All @@ -28,12 +28,12 @@ class Alert(Markdown):

alert_type = param.ObjectSelector("primary", objects=ALERT_TYPES)

priority = 0
priority: ClassVar[float | bool | None] = 0

_rename: ClassVar[Mapping[str, str | None]] = dict(Markdown._rename, alert_type=None)

@classmethod
def applies(cls, obj):
def applies(cls, obj: Any) -> float | bool | None:
priority = Markdown.applies(obj)
return 0 if priority else False

Expand Down
28 changes: 14 additions & 14 deletions panel/pane/base.py
Expand Up @@ -8,7 +8,7 @@

from functools import partial
from typing import (
TYPE_CHECKING, Any, Callable, List, Optional, Type, TypeVar,
TYPE_CHECKING, Any, Callable, ClassVar, List, Optional, Type, TypeVar,
)

import param
Expand Down Expand Up @@ -121,19 +121,19 @@ class PaneBase(Reactive):
# numerical priority is selected. The default is an intermediate value.
# If set to None, applies method will be called to get a priority
# value for a specific object type.
priority: float | bool | None = 0.5
priority: ClassVar[float | bool | None] = 0.5

# Whether applies requires full set of keywords
_applies_kw = False
_applies_kw: ClassVar[bool] = False

# Whether the Pane layout can be safely unpacked
_unpack: bool = True
_unpack: ClassVar[bool] = True

# Declares whether Pane supports updates to the Bokeh model
_updates: bool = False
_updates: ClassVar[bool] = False

# List of parameters that trigger a rerender of the Bokeh model
_rerender_params: List[str] = ['object']
_rerender_params: ClassVar[List[str]] = ['object']

__abstract = True

Expand Down Expand Up @@ -179,7 +179,7 @@ def _synced_params(self) -> List[str]:
return [p for p in self.param if p not in ignored_params]

def _update_object(
self, ref: str, doc: 'Document', root: 'Model', parent: 'Model', comm: Optional['Comm']
self, ref: str, doc: 'Document', root: Model, parent: Model, comm: Optional[Comm]
) -> None:
old_model = self._models[ref][0]
if self._updates:
Expand Down Expand Up @@ -246,7 +246,7 @@ def _update_pane(self, *events) -> None:
else:
cb()

def _update(self, ref: Optional[str] = None, model: Optional['Model'] = None) -> None:
def _update(self, ref: Optional[str] = None, model: Optional[Model] = None) -> None:
"""
If _updates=True this method is used to update an existing
Bokeh model instead of replacing the model entirely. The
Expand Down Expand Up @@ -290,9 +290,9 @@ def clone(self: T, object: Optional[Any] = None, **params) -> T:
return type(self)(object, **params)

def get_root(
self, doc: Optional['Document'] = None, comm: Optional['Comm'] = None,
self, doc: Optional[Document] = None, comm: Optional[Comm] = None,
preprocess: bool = True
) -> 'Model':
) -> Model:
"""
Returns the root model and applies pre-processing hooks
Expand Down Expand Up @@ -460,9 +460,9 @@ def _update_inner(self, new_object: Any) -> None:
self._internal = internal

def _get_model(
self, doc: 'Document', root: Optional['Model'] = None,
parent: Optional['Model'] = None, comm: Optional['Comm'] = None
) -> 'Model':
self, doc: Document, root: Optional[Model] = None,
parent: Optional[Model] = None, comm: Optional[Comm] = None
) -> Model:
if root:
ref = root.ref['id']
if ref in self._models:
Expand All @@ -473,7 +473,7 @@ def _get_model(
self._models[ref] = (model, parent)
return model

def _cleanup(self, root: 'Model' | None = None) -> None:
def _cleanup(self, root: Model | None = None) -> None:
self._inner_layout._cleanup(root)
super()._cleanup(root)

Expand Down

0 comments on commit c89d8e4

Please sign in to comment.