diff --git a/worlds/zillion/__init__.py b/worlds/zillion/__init__.py index c1dbcadf361a..212f6a9e53b7 100644 --- a/worlds/zillion/__init__.py +++ b/worlds/zillion/__init__.py @@ -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 @@ -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): @@ -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 @@ -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 @@ -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) @@ -183,7 +182,7 @@ 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) @@ -191,7 +190,7 @@ def create_regions(self) -> None: 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) @@ -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) @@ -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: @@ -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) @@ -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: diff --git a/worlds/zillion/client.py b/worlds/zillion/client.py index af81885ab247..d629df583a81 100644 --- a/worlds/zillion/client.py +++ b/worlds/zillion/client.py @@ -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 @@ -40,7 +40,7 @@ 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): @@ -48,7 +48,7 @@ class ZillionContext(CommonContext): 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]" @@ -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 @@ -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) @@ -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) @@ -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() @@ -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") @@ -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") @@ -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 diff --git a/worlds/zillion/gen_data.py b/worlds/zillion/gen_data.py index 13cbee9ced20..214073396153 100644 --- a/worlds/zillion/gen_data.py +++ b/worlds/zillion/gen_data.py @@ -1,6 +1,5 @@ from dataclasses import dataclass import json -from typing import Dict, Tuple from zilliandomizer.game import Game as ZzGame @@ -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 diff --git a/worlds/zillion/id_maps.py b/worlds/zillion/id_maps.py index 084d27d6cb4b..25762f99cd6b 100644 --- a/worlds/zillion/id_maps.py +++ b/worlds/zillion/id_maps.py @@ -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, @@ -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 = { @@ -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() } @@ -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 " @@ -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] diff --git a/worlds/zillion/logic.py b/worlds/zillion/logic.py index a14910a200e5..f3d1814a9e9b 100644 --- a/worlds/zillion/logic.py +++ b/worlds/zillion/logic.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/worlds/zillion/options.py b/worlds/zillion/options.py index ce21cc735e32..22a698472265 100644 --- a/worlds/zillion/options.py +++ b/worlds/zillion/options.py @@ -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 @@ -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", @@ -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"], @@ -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 diff --git a/worlds/zillion/patch.py b/worlds/zillion/patch.py index 2e09e926e135..0eee3315f4a1 100644 --- a/worlds/zillion/patch.py +++ b/worlds/zillion/patch.py @@ -1,5 +1,5 @@ import os -from typing import BinaryIO, Optional +from typing import BinaryIO import zipfile from typing_extensions import override @@ -24,8 +24,8 @@ class ZillionPatch(APAutoPatchInterface): """ JSON encoded """ def __init__(self, - path: Optional[str] = None, - player: Optional[int] = None, + path: str | None = None, + player: int | None = None, player_name: str = "", server: str = "", *, @@ -56,7 +56,7 @@ def patch(self, target: str) -> None: write_rom_from_gen_data(self.gen_data_str, target) -def get_base_rom_path(file_name: Optional[str] = None) -> str: +def get_base_rom_path(file_name: str | None = None) -> str: from . import ZillionSettings, ZillionWorld settings: ZillionSettings = ZillionWorld.settings if not file_name: diff --git a/worlds/zillion/region.py b/worlds/zillion/region.py index 1193fbbe63a1..40565f008263 100644 --- a/worlds/zillion/region.py +++ b/worlds/zillion/region.py @@ -1,5 +1,3 @@ -from typing import Optional - from typing_extensions import override from zilliandomizer.logic_components.regions import Region as ZzRegion @@ -32,7 +30,7 @@ def __init__(self, zz_loc: ZzLocation, player: int, name: str, - parent: Optional[Region] = None) -> None: + parent: Region | None = None) -> None: loc_id = loc_name_to_id[name] super().__init__(player, name, loc_id, parent) self.zz_loc = zz_loc diff --git a/worlds/zillion/test/TestReproducibleRandom.py b/worlds/zillion/test/TestReproducibleRandom.py index a92fae240709..352165449a8b 100644 --- a/worlds/zillion/test/TestReproducibleRandom.py +++ b/worlds/zillion/test/TestReproducibleRandom.py @@ -1,4 +1,3 @@ -from typing import cast from . import ZillionTestBase from .. import ZillionWorld @@ -9,7 +8,8 @@ class SeedTest(ZillionTestBase): def test_reproduce_seed(self) -> None: self.world_setup(42) - z_world = cast(ZillionWorld, self.multiworld.worlds[1]) + z_world = self.multiworld.worlds[1] + assert isinstance(z_world, ZillionWorld) r = z_world.zz_system.randomizer assert r randomized_requirements_first = tuple( @@ -18,7 +18,8 @@ def test_reproduce_seed(self) -> None: ) self.world_setup(42) - z_world = cast(ZillionWorld, self.multiworld.worlds[1]) + z_world = self.multiworld.worlds[1] + assert isinstance(z_world, ZillionWorld) r = z_world.zz_system.randomizer assert r randomized_requirements_second = tuple( diff --git a/worlds/zillion/test/__init__.py b/worlds/zillion/test/__init__.py index fe62bae34c9e..a669442364fe 100644 --- a/worlds/zillion/test/__init__.py +++ b/worlds/zillion/test/__init__.py @@ -1,4 +1,3 @@ -from typing import cast from test.bases import WorldTestBase from .. import ZillionWorld @@ -13,8 +12,9 @@ def ensure_gun_3_requirement(self) -> None: This makes sure that gun 3 is required by making all the canisters in O-7 (including key word canisters) require gun 3. """ - zz_world = cast(ZillionWorld, self.multiworld.worlds[1]) - assert zz_world.zz_system.randomizer - for zz_loc_name, zz_loc in zz_world.zz_system.randomizer.locations.items(): + z_world = self.multiworld.worlds[1] + assert isinstance(z_world, ZillionWorld) + assert z_world.zz_system.randomizer + for zz_loc_name, zz_loc in z_world.zz_system.randomizer.locations.items(): if zz_loc_name.startswith("r15c6"): zz_loc.req.gun = 3