Skip to content

Commit

Permalink
Update snakegame to pass pyright (#280)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
masenf authored Nov 25, 2024
1 parent cd12801 commit 7d45ceb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 0 additions & 2 deletions form-designer/form_designer/components/form_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion form-designer/form_designer/form_designer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion form-designer/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
reflex>=0.5.0
reflex>=0.6.5
reflex-local-auth>=0.2.0
2 changes: 1 addition & 1 deletion github-stats/github_stats/github_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion snakegame/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
reflex>=0.6.0
reflex>=0.6.5
25 changes: 18 additions & 7 deletions snakegame/snakegame/snakegame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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]]:
Expand All @@ -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):
Expand Down

0 comments on commit 7d45ceb

Please sign in to comment.