From 6380803507dc96bf7b578b74c4a680e5b02031ff Mon Sep 17 00:00:00 2001 From: Axy Date: Sat, 19 Sep 2026 16:27:33 +0200 Subject: [PATCH] Type cleanup --- pyproject.toml | 3 ++- src/pacman/ecs/__init__.py | 14 ++++---------- src/pacman/ecs/hierarchy.py | 9 ++++----- src/pacman/ecs/schedule.py | 2 +- src/pacman/ecs/world.py | 29 +++++++++++++++++++---------- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c1a0c92..1375c27 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,8 @@ build-backend = "uv_build" [tool.ruff] line-length = 79 -lint.select = ["E", "F", "UP", "B", "SIM", "I", "ARG", "N", "D", "ANN", "PYI"] +#lint.select = ["E", "F", "UP", "B", "SIM", "I", "ARG", "N", "D", "ANN", "PYI"] +lint.select = ["E", "F", "UP", "B", "SIM", "I", "ARG", "N", "ANN", "PYI"] [tool.ruff.lint.pydocstyle] convention = "google" diff --git a/src/pacman/ecs/__init__.py b/src/pacman/ecs/__init__.py index e4a15e6..1bee6e4 100644 --- a/src/pacman/ecs/__init__.py +++ b/src/pacman/ecs/__init__.py @@ -1,22 +1,16 @@ -from collections.abc import Callable, Generator, Iterable from dataclasses import dataclass from pathlib import Path -from time import time -from typing import Any, Literal, cast, overload import pygame from pygame import Vector2, Vector3 from pacman.ecs.hierarchy import ( - Children, Parent, hierachy_plugins, traverse_hierarchy, ) from pacman.ecs.schedule import ( MainSchedule, - Schedule, - ScheduleLabel, StartupSchedule, SystemSet, UpdateSchedule, @@ -161,9 +155,9 @@ class WiggleSprite: def move_sprites(world: World) -> None: import math - time = pygame.time.get_ticks() / 100 + tick = pygame.time.get_ticks() / 100 for coord, _ in world[tuple[Coord2D, WiggleSprite]]: - pos = max(time - coord.z * 0.3, 0.0) + pos = max(tick - coord.z * 0.3, 0.0) coord.y = 500.0 + math.sin(pos) * 30.0 @@ -174,9 +168,9 @@ class WiggleRoot: def move_root(world: World) -> None: import math - time = pygame.time.get_ticks() / 100 + tick = pygame.time.get_ticks() / 100 for coord, _ in world[tuple[Coord2D, WiggleRoot]]: - coord.x = 30.0 + math.cos(time) * 30.0 + coord.x = 30.0 + math.cos(tick) * 30.0 if __name__ == "__main__": diff --git a/src/pacman/ecs/hierarchy.py b/src/pacman/ecs/hierarchy.py index d2f5d93..89bb858 100644 --- a/src/pacman/ecs/hierarchy.py +++ b/src/pacman/ecs/hierarchy.py @@ -10,8 +10,7 @@ class Children: @property def children(self) -> Generator[Entity]: - for child in self._children: - yield child + yield from self._children __no_hook = False @@ -21,7 +20,8 @@ def get_parents(world: World, entity: Entity) -> Generator[Entity]: """Generate parents of entity including self""" yield entity while Parent in world[entity]: - entity: Entity = world[entity][Parent]._parent + nxt: Entity = world[entity][Parent]._parent + entity = nxt yield entity @@ -81,7 +81,6 @@ def parent_insert_hook( def parent_remove_hook( world: World, entity: Entity, component: Parent ) -> None: - global __no_hook if __no_hook: return world[component.parent][Children]._children.remove(entity) @@ -101,7 +100,7 @@ def traverse_hierarchy[*T]( params = sorted( get_args(query), key=lambda e: len(world._components.get(e, {})) ) - work = [] + work: list[Entity] = [] for e in world.query(query, without=tuple[Parent, Children]): yield (None, e) for entity, children in world.query( diff --git a/src/pacman/ecs/schedule.py b/src/pacman/ecs/schedule.py index dd15d01..d9742f4 100644 --- a/src/pacman/ecs/schedule.py +++ b/src/pacman/ecs/schedule.py @@ -79,7 +79,7 @@ class Schedule[T]: pre: Iterable[T | SystemSet], post: Iterable[T | SystemSet], group: Iterable[SystemSet], - ): + ) -> None: def get_pre(e: T | SystemSet) -> T | DAGLabel: if isinstance(e, SystemSet): return e._pre diff --git a/src/pacman/ecs/world.py b/src/pacman/ecs/world.py index 28d090b..bdbf8c2 100644 --- a/src/pacman/ecs/world.py +++ b/src/pacman/ecs/world.py @@ -1,3 +1,4 @@ +import contextlib from collections.abc import Callable, Generator from dataclasses import dataclass from typing import Any, Literal, cast, get_args, overload @@ -19,7 +20,7 @@ class Resource[T]: storage: type[T] -class SkipSystem(Exception): +class SkipSystemError(Exception): pass @@ -125,12 +126,15 @@ class World: @overload def __getitem__[T](self, arg: Resource[T]) -> T: ... + @overload def __getitem__[*T]( self, arg: type[tuple[*T]] ) -> Generator[tuple[*T]]: ... + @overload def __getitem__(self, arg: Entity) -> EntityThunk: ... + def __getitem__( self, arg: Any, @@ -143,8 +147,10 @@ class World: @overload def __setitem__[T](self, key: Resource[T], val: T) -> None: ... + @overload def __setitem__[*T](self, key: Entity, val: tuple[*T]) -> None: ... + def __setitem__(self, key: Any, val: Any) -> None: if isinstance(key, Entity) and isinstance(val, tuple): if key in self: @@ -159,8 +165,10 @@ class World: @overload def __delitem__[T](self, key: Resource[T]) -> None: ... + @overload def __delitem__(self, key: Entity) -> None: ... + def __delitem__[T](self, key: Entity | Resource[T]) -> None: if isinstance(key, Entity): components = self._entities[key] @@ -187,14 +195,12 @@ class World: if schedule not in self._schedules: return for system in self._schedules[schedule].traverse(self._run_cond): - try: + with contextlib.suppress(SkipSystemError): system(self) - except SkipSystem: - pass def res_s[T](self, ty: type[T]) -> T: if Resource(ty) not in self: - raise SkipSystem() + raise SkipSystemError() return self[Resource(ty)] def run( @@ -233,10 +239,13 @@ class World: d[ty] = hook else: old = d[ty] - d[ty] = lambda world, entity, component: ( - old(world, entity, component), - hook(world, entity, component), - )[1] + + def new(world: World, entity: Entity, component: T) -> None: + old(world, entity, component) + hook(world, entity, component) + pass + + d[ty] = new return self def with_plugins(self, *plugins: Plugin) -> "World": @@ -290,7 +299,7 @@ class Systems: ) sched.add_ordering((label,), self._pre, self._post, self._in_set) if self._chain: - prev = () + prev: tuple[SystemSet | System] | tuple[()] = () for system in systems: sched.add_ordering((system,), prev, (), (label,)) prev = (system,) -- 2.53.0