Skip to content

Commit

Permalink
python 3.10 typing update
Browse files Browse the repository at this point in the history
  • Loading branch information
beauxq committed Nov 28, 2024
1 parent 1831f1f commit 0287b9c
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 69 deletions.
31 changes: 15 additions & 16 deletions worlds/zillion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import functools
import settings
import threading
import typing
from typing import Any, Dict, List, Set, Tuple, Optional, Union
from typing import Any, ClassVar
import os
import logging

Expand Down Expand Up @@ -49,7 +48,7 @@ class RomStart(str):
"""

rom_file: RomFile = RomFile(RomFile.copy_to)
rom_start: typing.Union[RomStart, bool] = RomStart("retroarch")
rom_start: RomStart | bool = RomStart("retroarch")


class ZillionWebWorld(WebWorld):
Expand Down Expand Up @@ -78,7 +77,7 @@ class ZillionWorld(World):
options_dataclass = ZillionOptions
options: ZillionOptions # type: ignore

settings: typing.ClassVar[ZillionSettings] # type: ignore
settings: ClassVar[ZillionSettings] # type: ignore
# these type: ignore are because of this issue: https://github.com/python/typing/discussions/1486

topology_present = True # indicate if world type has any meaningful layout/pathing
Expand All @@ -91,7 +90,7 @@ class ZillionWorld(World):

class LogStreamInterface:
logger: logging.Logger
buffer: List[str]
buffer: list[str]

def __init__(self, logger: logging.Logger) -> None:
self.logger = logger
Expand All @@ -110,19 +109,19 @@ def flush(self) -> None:

lsi: LogStreamInterface

id_to_zz_item: Optional[Dict[int, ZzItem]] = None
id_to_zz_item: dict[int, ZzItem] | None = None
zz_system: System
_item_counts: "Counter[str]" = Counter()
_item_counts: Counter[str] = Counter()
"""
These are the items counts that will be in the game,
which might be different from the item counts the player asked for in options
(if the player asked for something invalid).
"""
my_locations: List[ZillionLocation] = []
my_locations: list[ZillionLocation] = []
""" This is kind of a cache to avoid iterating through all the multiworld locations in logic. """
slot_data_ready: threading.Event
""" This event is set in `generate_output` when the data is ready for `fill_slot_data` """
logic_cache: Union[ZillionLogicCache, None] = None
logic_cache: ZillionLogicCache | None = None

def __init__(self, world: MultiWorld, player: int) -> None:
super().__init__(world, player)
Expand Down Expand Up @@ -183,15 +182,15 @@ def create_regions(self) -> None:

start = self.zz_system.randomizer.regions["start"]

all_regions: Dict[str, ZillionRegion] = {}
all_regions: dict[str, ZillionRegion] = {}
for here_zz_name, zz_r in self.zz_system.randomizer.regions.items():
here_name = "Menu" if here_zz_name == "start" else zz_reg_name_to_reg_name(here_zz_name)
all_regions[here_name] = ZillionRegion(zz_r, here_name, here_name, p, w)
self.multiworld.regions.append(all_regions[here_name])

limited_skill = Req(gun=3, jump=3, skill=self.zz_system.randomizer.options.skill, hp=940, red=1, floppy=126)
queue = deque([start])
done: Set[str] = set()
done: set[str] = set()
while len(queue):
zz_here = queue.popleft()
here_name = "Menu" if zz_here.name == "start" else zz_reg_name_to_reg_name(zz_here.name)
Expand Down Expand Up @@ -277,7 +276,7 @@ def stage_generate_basic(multiworld: MultiWorld, *args: Any) -> None: # noqa: A
to_stay: Chars = "JJ"
if "JJ" in item_pool:
group_players = set(group["players"])
players_start_chars: List[Tuple[int, Chars]] = []
players_start_chars: list[tuple[int, Chars]] = []
for player in group_players:
z_world = multiworld.worlds[player]
assert isinstance(z_world, ZillionWorld)
Expand All @@ -289,7 +288,7 @@ def stage_generate_basic(multiworld: MultiWorld, *args: Any) -> None: # noqa: A
elif start_char_counts["Champ"] > start_char_counts["Apple"]:
to_stay = "Champ"
else: # equal
choices: Tuple[Chars, ...] = ("Apple", "Champ")
choices: tuple[Chars, ...] = ("Apple", "Champ")
to_stay = multiworld.random.choice(choices)

for p, sc in players_start_chars:
Expand All @@ -316,10 +315,10 @@ def finalize_item_locations(self) -> GenData:

assert self.zz_system.randomizer, "generate_early hasn't been called"

# debug_zz_loc_ids: Dict[str, int] = {}
# debug_zz_loc_ids: dict[str, int] = {}
empty = zz_items[4]
multi_item = empty # a different patcher method differentiates empty from ap multi item
multi_items: Dict[str, Tuple[str, str]] = {} # zz_loc_name to (item_name, player_name)
multi_items: dict[str, tuple[str, str]] = {} # zz_loc_name to (item_name, player_name)
for z_loc in self.multiworld.get_locations(self.player):
assert isinstance(z_loc, ZillionLocation)
# debug_zz_loc_ids[z_loc.zz_loc.name] = id(z_loc.zz_loc)
Expand All @@ -342,7 +341,7 @@ def finalize_item_locations(self) -> GenData:
# print(id_)
# print("size:", len(debug_zz_loc_ids))

# debug_loc_to_id: Dict[str, int] = {}
# debug_loc_to_id: dict[str, int] = {}
# regions = self.zz_randomizer.regions
# for region in regions.values():
# for loc in region.locations:
Expand Down
28 changes: 14 additions & 14 deletions worlds/zillion/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io
import pkgutil
import platform
from typing import Any, ClassVar, Coroutine, Dict, List, Optional, Protocol, Tuple, cast
from typing import Any, ClassVar, Coroutine, Protocol, cast

from CommonClient import CommonContext, server_loop, gui_enabled, \
ClientCommandProcessor, logger, get_base_parser
Expand Down Expand Up @@ -40,15 +40,15 @@ def __call__(self) -> object: ...


class SetRoomCallback(Protocol):
def __call__(self, rooms: List[List[int]]) -> object: ...
def __call__(self, rooms: list[list[int]]) -> object: ...


class ZillionContext(CommonContext):
game = "Zillion"
command_processor = ZillionCommandProcessor
items_handling = 1 # receive items from other players

known_name: Optional[str]
known_name: str | None
""" This is almost the same as `auth` except `auth` is reset to `None` when server disconnects, and this isn't. """

from_game: "asyncio.Queue[events.EventFromGame]"
Expand All @@ -57,11 +57,11 @@ class ZillionContext(CommonContext):
""" local checks watched by server """
next_item: int
""" index in `items_received` """
ap_id_to_name: Dict[int, str]
ap_id_to_zz_id: Dict[int, int]
ap_id_to_name: dict[int, str]
ap_id_to_zz_id: dict[int, int]
start_char: Chars = "JJ"
rescues: Dict[int, RescueInfo] = {}
loc_mem_to_id: Dict[int, int] = {}
rescues: dict[int, RescueInfo] = {}
loc_mem_to_id: dict[int, int] = {}
got_room_info: asyncio.Event
""" flag for connected to server """
got_slot_data: asyncio.Event
Expand Down Expand Up @@ -121,7 +121,7 @@ def reset_game_state(self) -> None:
self.items_received.clear()

@override
def on_deathlink(self, data: Dict[str, Any]) -> None:
def on_deathlink(self, data: dict[str, Any]) -> None:
self.to_game.put_nowait(events.DeathEventToGame())
return super().on_deathlink(data)

Expand Down Expand Up @@ -155,8 +155,8 @@ class MapPanel(Widget):
MAP_WIDTH: ClassVar[int] = 281

map_background: CoreImage
_number_textures: List[Texture] = []
rooms: List[List[int]] = []
_number_textures: list[Texture] = []
rooms: list[list[int]] = []

def __init__(self, **kwargs: Any) -> None: # noqa: ANN401
super().__init__(**kwargs)
Expand Down Expand Up @@ -218,7 +218,7 @@ def toggle_map_width(self) -> None:
self.map_widget.width = 0
self.container.do_layout()

def set_rooms(self, rooms: List[List[int]]) -> None:
def set_rooms(self, rooms: list[list[int]]) -> None:
self.map_widget.rooms = rooms
self.map_widget.update_map()

Expand All @@ -229,7 +229,7 @@ def set_rooms(self, rooms: List[List[int]]) -> None:
self.ui_task = asyncio.create_task(run_co, name="UI")

@override
def on_package(self, cmd: str, args: Dict[str, Any]) -> None:
def on_package(self, cmd: str, args: dict[str, Any]) -> None:
self.room_item_numbers_to_ui()
if cmd == "Connected":
logger.info("logged in to Archipelago server")
Expand Down Expand Up @@ -289,7 +289,7 @@ def on_package(self, cmd: str, args: Dict[str, Any]) -> None:
if "keys" not in args:
logger.warning(f"invalid Retrieved packet to ZillionClient: {args}")
return
keys = cast(Dict[str, Optional[str]], args["keys"])
keys = cast(dict[str, str | None], args["keys"])
doors_b64 = keys.get(f"zillion-{self.auth}-doors", None)
if doors_b64:
logger.info("received door data from server")
Expand Down Expand Up @@ -372,7 +372,7 @@ def process_items_received(self) -> None:
self.next_item = len(self.items_received)


def name_seed_from_ram(data: bytes) -> Tuple[str, str]:
def name_seed_from_ram(data: bytes) -> tuple[str, str]:
""" returns player name, and end of seed string """
if len(data) == 0:
# no connection to game
Expand Down
3 changes: 1 addition & 2 deletions worlds/zillion/gen_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dataclasses import dataclass
import json
from typing import Dict, Tuple

from zilliandomizer.game import Game as ZzGame

Expand All @@ -9,7 +8,7 @@
class GenData:
""" data passed from generation to patcher """

multi_items: Dict[str, Tuple[str, str]]
multi_items: dict[str, tuple[str, str]]
""" zz_loc_name to (item_name, player_name) """
zz_game: ZzGame
game_id: bytes
Expand Down
27 changes: 14 additions & 13 deletions worlds/zillion/id_maps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import defaultdict
from typing import Dict, Iterable, Mapping, Tuple, TypedDict
from collections.abc import Iterable, Mapping
from typing import TypedDict

from zilliandomizer.logic_components.items import (
Item as ZzItem,
Expand Down Expand Up @@ -40,13 +41,13 @@
_zz_empty = zz_item_name_to_zz_item["empty"]


def make_id_to_others(start_char: Chars) -> Tuple[
Dict[int, str], Dict[int, int], Dict[int, ZzItem]
def make_id_to_others(start_char: Chars) -> tuple[
dict[int, str], dict[int, int], dict[int, ZzItem]
]:
""" returns id_to_name, id_to_zz_id, id_to_zz_item """
id_to_name: Dict[int, str] = {}
id_to_zz_id: Dict[int, int] = {}
id_to_zz_item: Dict[int, ZzItem] = {}
id_to_name: dict[int, str] = {}
id_to_zz_id: dict[int, int] = {}
id_to_zz_item: dict[int, ZzItem] = {}

if start_char == "JJ":
name_to_zz_item = {
Expand Down Expand Up @@ -91,7 +92,7 @@ def make_room_name(row: int, col: int) -> str:
return f"{chr(ord('A') + row - 1)}-{col + 1}"


loc_name_to_id: Dict[str, int] = {
loc_name_to_id: dict[str, int] = {
name: id_ + base_id
for name, id_ in pretty_loc_name_to_id.items()
}
Expand All @@ -113,17 +114,17 @@ class ClientRescue(TypedDict):

class ZillionSlotInfo(TypedDict):
start_char: Chars
rescues: Dict[str, ClientRescue]
loc_mem_to_id: Dict[int, int]
rescues: dict[str, ClientRescue]
loc_mem_to_id: dict[int, int]
""" memory location of canister to Archipelago location id number """


def get_slot_info(regions: Iterable[RegionData],
start_char: Chars,
loc_name_to_pretty: Mapping[str, str]) -> ZillionSlotInfo:
items_placed_in_map_index: Dict[int, int] = defaultdict(int)
rescue_locations: Dict[int, RescueInfo] = {}
loc_memory_to_loc_id: Dict[int, int] = {}
items_placed_in_map_index: dict[int, int] = defaultdict(int)
rescue_locations: dict[int, RescueInfo] = {}
loc_memory_to_loc_id: dict[int, int] = {}
for region in regions:
for loc in region.locations:
assert loc.item, ("There should be an item placed in every location before "
Expand All @@ -142,7 +143,7 @@ def get_slot_info(regions: Iterable[RegionData],
loc_memory_to_loc_id[loc_memory] = pretty_loc_name_to_id[loc_name_to_pretty[loc.name]]
items_placed_in_map_index[map_index] += 1

rescues: Dict[str, ClientRescue] = {}
rescues: dict[str, ClientRescue] = {}
for i in (0, 1):
if i in rescue_locations:
ri = rescue_locations[i]
Expand Down
13 changes: 7 additions & 6 deletions worlds/zillion/logic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, FrozenSet, Mapping, Tuple, List, Counter as _Counter
from collections import Counter
from collections.abc import Mapping

from BaseClasses import CollectionState

Expand Down Expand Up @@ -35,7 +36,7 @@ def set_randomizer_locs(cs: CollectionState, p: int, zz_r: Randomizer) -> int:
return _hash


def item_counts(cs: CollectionState, p: int) -> Tuple[Tuple[str, int], ...]:
def item_counts(cs: CollectionState, p: int) -> tuple[tuple[str, int], ...]:
"""
the zilliandomizer items that player p has collected
Expand All @@ -44,11 +45,11 @@ def item_counts(cs: CollectionState, p: int) -> Tuple[Tuple[str, int], ...]:
return tuple((item_name, cs.count(item_name, p)) for item_name in item_name_to_id)


_cache_miss: Tuple[None, FrozenSet[Location]] = (None, frozenset())
_cache_miss: tuple[None, frozenset[Location]] = (None, frozenset())


class ZillionLogicCache:
_cache: Dict[int, Tuple[_Counter[str], FrozenSet[Location]]]
_cache: dict[int, tuple[Counter[str], frozenset[Location]]]
""" `{ hash: (counter_from_prog_items, accessible_zz_locations) }` """
_player: int
_zz_r: Randomizer
Expand All @@ -60,7 +61,7 @@ def __init__(self, player: int, zz_r: Randomizer, id_to_zz_item: Mapping[int, It
self._zz_r = zz_r
self._id_to_zz_item = id_to_zz_item

def cs_to_zz_locs(self, cs: CollectionState) -> FrozenSet[Location]:
def cs_to_zz_locs(self, cs: CollectionState) -> frozenset[Location]:
"""
given an Archipelago `CollectionState`,
returns frozenset of accessible zilliandomizer locations
Expand All @@ -76,7 +77,7 @@ def cs_to_zz_locs(self, cs: CollectionState) -> FrozenSet[Location]:
return locs

# print("cache miss")
have_items: List[Item] = []
have_items: list[Item] = []
for name, count in counts:
have_items.extend([self._id_to_zz_item[item_name_to_id[name]]] * count)
# have_req is the result of converting AP CollectionState to zilliandomizer collection state
Expand Down
8 changes: 4 additions & 4 deletions worlds/zillion/options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import Counter
from dataclasses import dataclass
from typing import ClassVar, Dict, Literal, Tuple, TypeGuard
from typing import ClassVar, Literal, TypeGuard

from Options import Choice, DefaultOnToggle, NamedRange, OptionGroup, PerGameCommonOptions, Range, Removed, Toggle

Expand Down Expand Up @@ -107,7 +107,7 @@ class ZillionStartChar(Choice):
display_name = "start character"
default = "random"

_name_capitalization: ClassVar[Dict[int, Chars]] = {
_name_capitalization: ClassVar[dict[int, Chars]] = {
option_jj: "JJ",
option_apple: "Apple",
option_champ: "Champ",
Expand Down Expand Up @@ -305,7 +305,7 @@ class ZillionOptions(PerGameCommonOptions):
]


def convert_item_counts(ic: "Counter[str]") -> ZzItemCounts:
def convert_item_counts(ic: Counter[str]) -> ZzItemCounts:
tr: ZzItemCounts = {
ID.card: ic["ID Card"],
ID.red: ic["Red ID Card"],
Expand All @@ -319,7 +319,7 @@ def convert_item_counts(ic: "Counter[str]") -> ZzItemCounts:
return tr


def validate(options: ZillionOptions) -> "Tuple[ZzOptions, Counter[str]]":
def validate(options: ZillionOptions) -> tuple[ZzOptions, Counter[str]]:
"""
adjusts options to make game completion possible
Expand Down
Loading

0 comments on commit 0287b9c

Please sign in to comment.