Skip to content

Commit

Permalink
Rename "select" to "dropdown", improve types (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi authored May 26, 2023
1 parent d135bc0 commit f7c4af3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/03_gui_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def main() -> None:

with server.gui_folder("Control"):
gui_show = server.add_gui_checkbox("Show Frame", initial_value=True)
gui_axis = server.add_gui_select("Axis", options=["x", "y", "z"])
gui_axis = server.add_gui_dropdown("Axis", ["x", "y", "z"])
gui_include_z = server.add_gui_checkbox("Z in dropdown", initial_value=True)

@gui_include_z.on_update
Expand Down
2 changes: 1 addition & 1 deletion viser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ._message_api import GuiButtonGroupHandle as GuiButtonGroupHandle
from ._message_api import GuiButtonHandle as GuiButtonHandle
from ._message_api import GuiHandle as GuiHandle
from ._message_api import GuiSelectHandle as GuiSelectHandle
from ._message_api import GuiDropdownHandle as GuiDropdownHandle
from ._scene_handle import SceneNodeHandle as SceneNodeHandle
from ._scene_handle import TransformControlsHandle as TransformControlsHandle
from ._viser import CameraHandle as CameraHandle
Expand Down
2 changes: 1 addition & 1 deletion viser/_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def disabled(self, disabled: bool) -> None:


@dataclasses.dataclass
class GuiSelectHandle(GuiHandle[StringType], Generic[StringType]):
class GuiDropdownHandle(GuiHandle[StringType], Generic[StringType]):
"""Handle for a dropdown-style GUI input in our visualizer.
Lets us get values, set values, and detect updates."""
Expand Down
58 changes: 22 additions & 36 deletions viser/_message_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mypy: disable-error-code="misc"
#
# TLiteralString overload on `add_gui_select()` is waiting on PEP 675 support in mypy.
# TLiteralString overloads are waiting on PEP 675 support in mypy.
# https://github.com/python/mypy/issues/12554
#
# In the meantime, it works great in Pyright/Pylance!
Expand Down Expand Up @@ -36,8 +36,8 @@
from ._gui import (
GuiButtonGroupHandle,
GuiButtonHandle,
GuiDropdownHandle,
GuiHandle,
GuiSelectHandle,
_GuiHandleState,
)
from ._scene_handle import (
Expand Down Expand Up @@ -175,8 +175,11 @@ def add_gui_button(
)._impl
)

# The explicit overloads help pyright resolve the value type to a Literal whenever
# possible.
# The TLiteralString overload tells pyright to resolve the value type to a Literal
# whenever possible.
#
# TString is helpful when the input types are generic (could be str, could be
# Literal).
@overload
def add_gui_button_group(
self,
Expand All @@ -190,17 +193,17 @@ def add_gui_button_group(
def add_gui_button_group(
self,
name: str,
options: List[str],
options: List[TString],
visible: bool = True,
) -> GuiButtonGroupHandle[str]:
) -> GuiButtonGroupHandle[TString]:
...

def add_gui_button_group(
self,
name: str,
options: List[TLiteralString] | List[str],
options: List[TLiteralString] | List[TString],
visible: bool = True,
) -> GuiButtonGroupHandle[TLiteralString] | GuiButtonGroupHandle[str]:
) -> GuiButtonGroupHandle[Any]: # Return types are specified in overloads.
"""Add a button group to the GUI. Button groups currently cannot be disabled."""
assert len(options) > 0
handle = self._add_gui_impl(
Expand Down Expand Up @@ -324,63 +327,46 @@ def add_gui_vector3(
hint=hint,
)

# The explicit overloads help pyright resolve the value type to a Literal whenever
# possible.
# See add_gui_dropdown for notes on overloads.
@overload
def add_gui_select(
def add_gui_dropdown(
self,
name: str,
options: List[TLiteralString],
initial_value: Optional[TLiteralString] = None,
disabled: bool = False,
visible: bool = True,
hint: Optional[str] = None,
) -> GuiSelectHandle[TLiteralString]:
) -> GuiDropdownHandle[TLiteralString]:
...

@overload
def add_gui_select(
def add_gui_dropdown(
self,
name: str,
options: List[TString],
initial_value: Optional[TString] = None,
disabled: bool = False,
visible: bool = True,
) -> GuiSelectHandle[TString]:
...

@overload
def add_gui_select(
self,
name: str,
options: List[str],
initial_value: Optional[str] = None,
disabled: bool = False,
visible: bool = True,
hint: Optional[str] = None,
) -> GuiSelectHandle[str]:
) -> GuiDropdownHandle[TString]:
...

def add_gui_select(
def add_gui_dropdown(
self,
name: str,
options: List[TLiteralString] | List[TString] | List[str],
initial_value: Optional[TLiteralString | TString | str] = None,
options: List[TLiteralString] | List[TString],
initial_value: Optional[TLiteralString | TString] = None,
disabled: bool = False,
visible: bool = True,
hint: Optional[str] = None,
) -> (
GuiSelectHandle[TLiteralString]
| GuiSelectHandle[TString]
| GuiSelectHandle[str]
):
) -> GuiDropdownHandle[Any]: # Output type is specified in overloads.
"""Add a dropdown to the GUI."""
assert len(options) > 0
if initial_value is None:
initial_value = options[0]

# Re-wrap the GUI handle with a select interface.
return GuiSelectHandle(
return GuiDropdownHandle(
self._add_gui_impl(
"/".join(self._gui_folder_labels + [name]),
initial_value,
Expand All @@ -393,7 +379,7 @@ def add_gui_select(
visible=visible,
hint=hint,
)._impl,
options, # type: ignore
options,
)

def add_gui_slider(
Expand Down

0 comments on commit f7c4af3

Please sign in to comment.