Skip to content

Commit

Permalink
fix: Type fixes and require pyright (#302)
Browse files Browse the repository at this point in the history
Fixed remaining type issues and makes pyright a mandatory check via the
pre-commit.
Fixes #97

---------

Co-authored-by: Mike Bender <[email protected]>
  • Loading branch information
jnumainville and mofojed authored Feb 23, 2024
1 parent f5f8f02 commit d5d003d
Show file tree
Hide file tree
Showing 46 changed files with 445 additions and 264 deletions.
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ repos:
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.334
hooks:
- id: pyright
files: plugins.*\/src.*\.py
additional_dependencies:
[
pandas,
deephaven-core,
plotly,
json-rpc,
matplotlib
]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.2
hooks:
- id: ruff
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Start by setting up the python venv and pre-commit hooks.

### Pre-commit hooks/Python formatting

Black and blacken-docs formatting is setup through a pre-commit hook.
Black and blacken-docs formatting, pyright type checking, and ruff linting is setup through a pre-commit hook.
To install the pre-commit hooks, run the following commands from the root directory of this repo:

```shell
Expand All @@ -56,6 +56,11 @@ pre-commit run --all-files

All steps should pass.

To bypass the pre-commit hook, you can commit with the `--no-verify` flag, for example:
```shell
git commit --no-verify -m "commit message"`
```

### Running Python tests

The above steps will also set up `tox` to run tests for the python plugins that support it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __init__(self, fig, table, func, columns=None, fargs=None, **kwargs):
super().__init__(fig, event_source, **kwargs)

# Start the animation right away
self._start()
self._start() # type: ignore

def new_frame_seq(self):
"""
Expand All @@ -156,7 +156,7 @@ def _step(self, update, *args):
# Extends the _step() method for the Animation class. Used
# to get the update information
self._last_update = update
return super()._step(*args)
return super()._step(*args) # type: ignore

def _draw_frame(self, framedata):
data = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _setup_listeners(self) -> None:
self._handles.append(handle)
self._liveness_scope.manage(handle)

def _get_figure(self) -> DeephavenFigure:
def _get_figure(self) -> DeephavenFigure | None:
"""
Get the current figure
Expand All @@ -96,10 +96,9 @@ def _on_update(
if self._connection:
revision = self._revision_manager.get_revision()
node.recreate_figure()
figure = self._get_figure()
try:
self._connection.on_data(
*self._build_figure_message(self._get_figure(), revision)
)
self._connection.on_data(*self._build_figure_message(figure, revision))
except RuntimeError:
# trying to send data when the connection is closed, ignore
pass
Expand All @@ -115,7 +114,7 @@ def _handle_retrieve_figure(self) -> tuple[bytes, list[Any]]:
return self._build_figure_message(self._get_figure())

def _build_figure_message(
self, figure: DeephavenFigure, revision: int | None = None
self, figure: DeephavenFigure | None, revision: int | None = None
) -> tuple[bytes, list[Any]]:
"""
Build a message to send to the client with the current figure.
Expand All @@ -129,6 +128,9 @@ def _build_figure_message(
"""
exporter = self._exporter

if not figure:
raise ValueError("Figure is None")

with self._revision_manager:
# if revision is None, just send the figure
if revision is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
MINUTE = 60 * SECOND #: One minute in nanoseconds.


def _cast_timestamp(time: pd.Timestamp | None) -> pd.Timestamp:
"""
Casts a pd.Timestamp to be non-None.
Args:
time: the timestamp to cast
Returns:
the timestamp
"""
if not time:
raise ValueError("pd_base_time is None")
return time


def iris(ticking: bool = True, size: int = 300) -> Table:
"""
Returns a ticking version of the 1936 Iris flower dataset.
Expand Down Expand Up @@ -55,7 +69,7 @@ def iris(ticking: bool = True, size: int = 300) -> Table:
"""

base_time = to_j_instant("1936-01-01T08:00:00 UTC")
pd_base_time = to_pd_timestamp(base_time)
pd_base_time = _cast_timestamp(to_pd_timestamp(base_time))

species_list: list[str] = ["setosa", "versicolor", "virginica"]
col_ids = {"sepal_length": 0, "sepal_width": 1, "petal_length": 2, "petal_width": 3}
Expand Down Expand Up @@ -153,7 +167,9 @@ def stocks(ticking: bool = True, hours_of_data: int = 1) -> Table:
base_time = to_j_instant(
"2018-06-01T08:00:00 ET"
) # day deephaven.io was registered
pd_base_time = to_pd_timestamp(base_time)

pd_base_time = _cast_timestamp(to_pd_timestamp(base_time))

sym_list = ["CAT", "DOG", "FISH", "BIRD", "LIZARD"]
sym_dict = {v: i for i, v in enumerate(sym_list)}
sym_weights = [95, 100, 70, 45, 35]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any

from deephaven.table import Table
from deephaven.plugin.object_type import Exporter
from ..exporter import Exporter

from .json_conversion import json_link_mapping

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def add_custom_data_args(
generators = []

for arg in CUSTOM_DATA_ARGS:
if arg in custom_call_args and (val := custom_call_args[arg]):
if (
custom_call_args
and arg in custom_call_args
and (val := custom_call_args[arg])
):
generators.append(custom_data_args_generator(arg, val))

update_generator = combined_generator(generators, fill={})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import defaultdict
from itertools import count
from collections.abc import Generator, Iterable
from typing import Any
from typing import Any, Mapping


def json_links(i: int, _vars: Iterable[str]) -> Generator[str, None, None]:
Expand All @@ -24,7 +24,7 @@ def json_links(i: int, _vars: Iterable[str]) -> Generator[str, None, None]:

def convert_to_json_links(
var_col_dicts: list[dict[str, str]], start_index: int
) -> Generator[dict[str, str], None, None]:
) -> Generator[Mapping[str, list[str]], None, None]:
"""Convert the provided dictionaries to json links
Args:
Expand Down
Loading

0 comments on commit d5d003d

Please sign in to comment.