From 7d45ceb6821f62453b297946cce7ce92f64b638e Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Mon, 25 Nov 2024 10:41:13 -0800 Subject: [PATCH] Update snakegame to pass pyright (#280) * Update snakegame to pass pyright * Fix form-designer for 0.6.5 * github_stats: update color_mode.button component This component no longer takes positional arguments (and the arguments that were previously being passed were incorrect) * form-designer: remove explicit Field cast * bump to release version --- .../form_designer/components/form_editor.py | 2 -- form-designer/form_designer/form_designer.py | 2 +- form-designer/requirements.txt | 2 +- github-stats/github_stats/github_stats.py | 2 +- snakegame/requirements.txt | 2 +- snakegame/snakegame/snakegame.py | 25 +++++++++++++------ 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/form-designer/form_designer/components/form_editor.py b/form-designer/form_designer/components/form_editor.py index 2b890fed..b5026999 100644 --- a/form-designer/form_designer/components/form_editor.py +++ b/form-designer/form_designer/components/form_editor.py @@ -53,8 +53,6 @@ def set_name(self, name: str): def update_field(self, field: Field): if not self._user_has_access(): return - field.pop("options", None) # Remove options, relationship - field = Field(**field) with rx.session() as session: session.add(self.form) session.commit() diff --git a/form-designer/form_designer/form_designer.py b/form-designer/form_designer/form_designer.py index a188b7db..8c097300 100644 --- a/form-designer/form_designer/form_designer.py +++ b/form-designer/form_designer/form_designer.py @@ -26,7 +26,7 @@ # Adding a dummy route to register the dynamic route vars. with contextlib.suppress(ValueError): - app.add_page(lambda: rx.fragment(on_click=False), route="/_dummy/[form_id]/[field_id]") + app.add_page(lambda: rx.fragment(on_click=rx.event.noop()), route="/_dummy/[form_id]/[field_id]") # Authentication via reflex-local-auth app.add_page( diff --git a/form-designer/requirements.txt b/form-designer/requirements.txt index c40a2714..62099b28 100644 --- a/form-designer/requirements.txt +++ b/form-designer/requirements.txt @@ -1,2 +1,2 @@ -reflex>=0.5.0 +reflex>=0.6.5 reflex-local-auth>=0.2.0 diff --git a/github-stats/github_stats/github_stats.py b/github-stats/github_stats/github_stats.py index 6b83bdc2..003fb6b4 100644 --- a/github-stats/github_stats/github_stats.py +++ b/github-stats/github_stats/github_stats.py @@ -92,7 +92,7 @@ def data_pretty(self) -> str: def index() -> rx.Component: return rx.fragment( - rx.color_mode.button(rx.color_mode.icon(), float="right"), + rx.color_mode.button(position="top-right"), rx.vstack( rx.heading("Github Stats", font_size="2em"), rx.hstack( diff --git a/snakegame/requirements.txt b/snakegame/requirements.txt index 9cd7c052..1de84caa 100644 --- a/snakegame/requirements.txt +++ b/snakegame/requirements.txt @@ -1 +1 @@ -reflex>=0.6.0 +reflex>=0.6.5 diff --git a/snakegame/snakegame/snakegame.py b/snakegame/snakegame/snakegame.py index ccea2fed..0229d540 100644 --- a/snakegame/snakegame/snakegame.py +++ b/snakegame/snakegame/snakegame.py @@ -3,7 +3,9 @@ from typing import Dict import reflex as rx -from reflex.event import EventType, key_event +from reflex.constants.colors import Color +from reflex.event import EventSpec +from reflex.utils.imports import ImportDict N = 19 # There is a N*N grid for ground of snake GRID_EMPTY = 0 @@ -43,7 +45,7 @@ class Colors(rx.State): # Why is this not just a global? Because we index into the dict with state # vars in an rx.foreach, so this dict needs to be accessible in the compiled # frontend. - c: dict[int, str] = { + c: dict[int, Color] = { GRID_EMPTY: rx.color("gray", 5), GRID_SNAKE: rx.color("grass", 9), GRID_FOOD: rx.color("blue", 9), @@ -65,6 +67,7 @@ class State(rx.State): running: bool = False _n_tasks: int = 0 + @rx.event def play(self): """Start / resume the game.""" if not self.running: @@ -74,10 +77,12 @@ def play(self): self.running = True return State.loop + @rx.event def pause(self): """Signal the game to pause.""" self.running = False + @rx.event def flip_switch(self, start): """Toggle whether the game is running or paused.""" if start: @@ -93,7 +98,7 @@ def _last_move(self): """Returns the last queued direction the snake head should move in.""" return self.moves[-1] if self.moves else self.dir - @rx.background + @rx.event(background=True) async def loop(self): """The main game loop, implemented as a singleton background task. @@ -147,26 +152,31 @@ async def loop(self): # Decrement task counter, since we're about to return self._n_tasks -= 1 + @rx.event def arrow_up(self): """Queue a move up.""" if self._last_move() != HEAD_D: self.moves.append(HEAD_U) + @rx.event def arrow_left(self): """Queue a move left.""" if self._last_move() != HEAD_R: self.moves.append(HEAD_L) + @rx.event def arrow_right(self): """Queue a move right.""" if self._last_move() != HEAD_L: self.moves.append(HEAD_R) + @rx.event def arrow_down(self): """Queue a move down.""" if self._last_move() != HEAD_U: self.moves.append(HEAD_D) + @rx.event def arrow_rel_left(self): """Queue a move left relative to the current direction.""" last_move = self._last_move() @@ -179,6 +189,7 @@ def arrow_rel_left(self): elif last_move == HEAD_R: self.arrow_up() + @rx.event def arrow_rel_right(self): """Queue a move right relative to the current direction.""" last_move = self._last_move() @@ -202,9 +213,9 @@ class GlobalKeyWatcher(rx.Fragment): """ # List of keys to trigger on - key_map: Dict[str, EventType[key_event]] = {} + key_map: Dict[str, EventSpec] = {} - def add_imports(self) -> dict[str, str]: + def add_imports(self) -> ImportDict: return {"react": "useEffect"} def add_hooks(self) -> list[str | rx.Var[str]]: @@ -228,9 +239,9 @@ def add_hooks(self) -> list[str | rx.Var[str]]: """, ] - def render(self) -> str: + def render(self) -> dict: # This component has no visual element. - return "" + return {} def colored_box(grid_square_type: int):