Skip to content

Commit

Permalink
try quoted hints for traitlets 5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
nrbgt committed Sep 15, 2023
1 parent fc993f6 commit 4bf1f72
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 157 deletions.
16 changes: 10 additions & 6 deletions src/ipyforcegraph/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
# Copyright (c) 2023 ipyforcegraph contributors.
# Distributed under the terms of the Modified BSD License.

from typing import TYPE_CHECKING

import ipywidgets as W
import traitlets as T

from . import _types as _t
from .constants import EXTENSION_NAME, EXTENSION_SPEC_VERSION

if TYPE_CHECKING:
from . import _types as _t


class ForceBase(W.Widget):
"""The base class for all ``IPyForceGraph`` widgets."""

_model_name: _t.Tstr = T.Unicode("ForceBaseModel").tag(sync=True)
_model_module: _t.Tstr = T.Unicode(EXTENSION_NAME).tag(sync=True)
_model_module_version: _t.Tstr = T.Unicode(EXTENSION_SPEC_VERSION).tag(sync=True)
_view_module: _t.Tstr = T.Unicode(EXTENSION_NAME).tag(sync=True)
_view_module_version: _t.Tstr = T.Unicode(EXTENSION_SPEC_VERSION).tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("ForceBaseModel").tag(sync=True)
_model_module: "_t.Tstr" = T.Unicode(EXTENSION_NAME).tag(sync=True)
_model_module_version: "_t.Tstr" = T.Unicode(EXTENSION_SPEC_VERSION).tag(sync=True)
_view_module: "_t.Tstr" = T.Unicode(EXTENSION_NAME).tag(sync=True)
_view_module_version: "_t.Tstr" = T.Unicode(EXTENSION_SPEC_VERSION).tag(sync=True)
2 changes: 2 additions & 0 deletions src/ipyforcegraph/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
Tbool = T.Bool[bool, Union[bool, int]]

Tdict_any = T.Instance[Dict[Any, Any]]

Tenum_str_str = T.Enum[str, str]
32 changes: 17 additions & 15 deletions src/ipyforcegraph/behaviors/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
# Copyright (c) 2023 ipyforcegraph contributors.
# Distributed under the terms of the Modified BSD License.

from typing import Any, Optional, Union
from typing import TYPE_CHECKING, Any, Optional, Union

import ipywidgets as W
import traitlets as T

from .. import _types as _t
from .._base import ForceBase
from ..trait_utils import JSON_TYPES, coerce

if TYPE_CHECKING:
from .. import _types as _t

TFeature = Optional[Union["Column", "Nunjucks", str]]
TNumFeature = Optional[Union["Column", "Nunjucks", str, int, float]]
TBoolFeature = Optional[Union["Column", "Nunjucks", str, bool]]
Expand All @@ -36,9 +38,9 @@ class DEFAULT_RANK:
class Behavior(ForceBase):
"""The base class for all IPyForceGraph graph behaviors."""

_model_name: _t.Tstr = T.Unicode("BehaviorModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("BehaviorModel").tag(sync=True)

rank: _t.Tint = T.Int(
rank: "_t.Tint" = T.Int(
DEFAULT_RANK.behavior,
help=("order in which behaviors are applied: lower numbers are applied first."),
).tag(sync=True)
Expand All @@ -47,16 +49,16 @@ class Behavior(ForceBase):
class BaseD3Force(Behavior):
"""A base for all ``d3-force-3d`` force wrappers."""

_model_name: _t.Tstr = T.Unicode("BaseD3ForceModel").tag(sync=True)
active: _t.Tbool = T.Bool(True, help="whether the force is currently active").tag(
_model_name: "_t.Tstr" = T.Unicode("BaseD3ForceModel").tag(sync=True)
active: "_t.Tbool" = T.Bool(True, help="whether the force is currently active").tag(
sync=True
)


class ShapeBase(ForceBase):
"""A base class from which all :mod:`~ipyforcegraph.behaviors.shapes` inherit."""

_model_name: _t.Tstr = T.Unicode("ShapeBaseModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("ShapeBaseModel").tag(sync=True)


class DynamicValue(ForceBase):
Expand All @@ -66,11 +68,11 @@ class DynamicValue(ForceBase):

JSON_DATA_TYPES = JSON_TYPES.get_supported_types()

value: _t.Tstr = T.Unicode(
value: "_t.Tstr" = T.Unicode(
"", help="the source used to compute the value for the trait"
).tag(sync=True)

coerce: _t.Tstr_maybe = T.Unicode(
coerce: "_t.Tstr_maybe" = T.Unicode(
help="name of a JSON Schema ``type`` into which to coerce the final value",
allow_none=True,
).tag(sync=True)
Expand Down Expand Up @@ -98,7 +100,7 @@ def _validate_coercer(self, proposal: T.Bunch) -> Optional[str]:
class Column(DynamicValue):
"""A column from a :class:`~ipyforcegraph.sources.dataframe.DataFrameSource`."""

_model_name: _t.Tstr = T.Unicode("ColumnModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("ColumnModel").tag(sync=True)


class Nunjucks(DynamicValue):
Expand Down Expand Up @@ -141,7 +143,7 @@ class Nunjucks(DynamicValue):
rgb({{ c }},0,0)
"""

_model_name: _t.Tstr = T.Unicode("NunjucksModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("NunjucksModel").tag(sync=True)


def _make_trait(
Expand Down Expand Up @@ -178,7 +180,7 @@ def _make_trait(
class HasScale(ShapeBase):
"""A shape that has ``scale_on_zoom``."""

_model_name: _t.Tstr = T.Unicode("HasScaleModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("HasScaleModel").tag(sync=True)

scale_on_zoom: TBoolFeature = _make_trait(
"whether font size/stroke respects the global scale. Has no impact on `link` shapes.",
Expand All @@ -193,7 +195,7 @@ def _validate_scale_bools(self, proposal: T.Bunch) -> Any:
class HasFillAndStroke(HasScale):
"""A shape that has ``fill`` and ``stroke``."""

_model_name: _t.Tstr = T.Unicode("HasFillModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("HasFillModel").tag(sync=True)
fill: TFeature = _make_trait("the fill color of a shape")
stroke: TFeature = _make_trait("the stroke color of a shape")
stroke_width: TNumFeature = _make_trait("the stroke width of a shape", numeric=True)
Expand All @@ -215,7 +217,7 @@ def _validate_has_fill_and_stroke_arrays(self, proposal: T.Bunch) -> Any:
class HasOffsets(ShapeBase):
"""A shape that can be offset in the horizontal, vertical, or elevation dimensions."""

_model_name: _t.Tstr = T.Unicode("HasOffsetsModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("HasOffsetsModel").tag(sync=True)

offset_x: float = _make_trait(
"the relative horizontal offset from the middle of the shape in ``px``",
Expand All @@ -238,7 +240,7 @@ def _validate_offset_numerics(self, proposal: T.Bunch) -> Any:
class HasDimensions(HasFillAndStroke, HasOffsets):
"""A shape that has ``width``, ``height`` and ``depth``."""

_model_name: _t.Tstr = T.Unicode("HasDimensionsModel").tag(sync=True)
_model_name: "_t.Tstr" = T.Unicode("HasDimensionsModel").tag(sync=True)

width: TNumFeature = _make_trait("the width of a shape in ``px``", numeric=True)
height: TNumFeature = _make_trait("the height of a shape in ``px``", numeric=True)
Expand Down
Loading

0 comments on commit 4bf1f72

Please sign in to comment.