From 64ac619b464eedd19cabaeb3c749886359ddcbf5 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sat, 26 Mar 2022 01:12:54 +0100 Subject: [PATCH] Core: use assert correctly (#345) Core: add some more types to State and add count() method --- BaseClasses.py | 9 ++++++--- Main.py | 4 ++-- Options.py | 14 +++++++------- worlds/alttp/EntranceShuffle.py | 4 ++-- worlds/alttp/ItemPool.py | 4 ++-- worlds/factorio/Technologies.py | 2 +- 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/BaseClasses.py b/BaseClasses.py index 4d9b1259..5e0f91b5 100644 --- a/BaseClasses.py +++ b/BaseClasses.py @@ -644,15 +644,18 @@ class CollectionState(): self.events.add(event) self.collect(event.item, True, event) - def has(self, item: str, player: int, count: int = 1): + def has(self, item: str, player: int, count: int = 1) -> bool: return self.prog_items[item, player] >= count - def has_all(self, items: Set[str], player: int): + def has_all(self, items: Set[str], player: int) -> bool: return all(self.prog_items[item, player] for item in items) - def has_any(self, items: Set[str], player: int): + def has_any(self, items: Set[str], player: int) -> bool: return any(self.prog_items[item, player] for item in items) + def count(self, item: str, player: int) -> int: + return self.prog_items[item, player] + def has_group(self, item_name_group: str, player: int, count: int = 1): found: int = 0 for item_name in self.world.worlds[player].item_name_groups[item_name_group]: diff --git a/Main.py b/Main.py index c7a4b8f6..f02c3f4d 100644 --- a/Main.py +++ b/Main.py @@ -360,8 +360,8 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No locations_data: Dict[int, Dict[int, Tuple[int, int, int]]] = {player: {} for player in world.player_ids} for location in world.get_filled_locations(): if type(location.address) == int: - # item code None should be event, location.address should then also be None - assert location.item.code is not None + assert location.item.code is not None, "item code None should be event, " \ + "location.address should then also be None" locations_data[location.player][location.address] = \ location.item.code, location.item.player, location.item.flags if location.name in world.start_location_hints[location.player]: diff --git a/Options.py b/Options.py index ac12cf4d..b26d55df 100644 --- a/Options.py +++ b/Options.py @@ -17,8 +17,8 @@ class AssembleOptions(type): new_options = {name[7:].lower(): option_id for name, option_id in attrs.items() if name.startswith("option_")} - assert "random" not in new_options # Choice option 'random' cannot be manually assigned. - assert len(new_options) == len(set(new_options.values())) # same ID cannot be used twice. Try alias? + assert "random" not in new_options, "Choice option 'random' cannot be manually assigned." + assert len(new_options) == len(set(new_options.values())), "same ID cannot be used twice. Try alias?" attrs["name_lookup"].update({option_id: name for name, option_id in new_options.items()}) options.update(new_options) @@ -104,7 +104,7 @@ class Toggle(Option): default = 0 def __init__(self, value: int): - assert value == 0 or value == 1 + assert value == 0 or value == 1, "value of Toggle can only be 0 or 1" self.value = value @classmethod @@ -178,10 +178,10 @@ class Choice(Option): if isinstance(other, self.__class__): return other.value == self.value elif isinstance(other, str): - assert other in self.options + assert other in self.options, "compared against a str that could never be equal." return other == self.current_key elif isinstance(other, int): - assert other in self.name_lookup + assert other in self.name_lookup, "compared against an int that could never be equal." return other == self.value elif isinstance(other, bool): return other == bool(self.value) @@ -192,10 +192,10 @@ class Choice(Option): if isinstance(other, self.__class__): return other.value != self.value elif isinstance(other, str): - assert other in self.options + assert other in self.options , "compared against a str that could never be equal." return other != self.current_key elif isinstance(other, int): - assert other in self.name_lookup + assert other in self.name_lookup, "compared against am int that could never be equal." return other != self.value elif isinstance(other, bool): return other != bool(self.value) diff --git a/worlds/alttp/EntranceShuffle.py b/worlds/alttp/EntranceShuffle.py index 8b9e8bfa..c2b56d6f 100644 --- a/worlds/alttp/EntranceShuffle.py +++ b/worlds/alttp/EntranceShuffle.py @@ -2242,7 +2242,7 @@ def crossed_shuffle_dungeons(world, player: int): connect_caves(world, dungeon_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player) connect_caves(world, dungeon_entrances, [], dungeon_exits, player) - assert not dungeon_exits # make sure all exits are accounted for + assert not dungeon_exits , "make sure all exits are accounted for" def inverted_crossed_shuffle_dungeons(world, player: int): @@ -2293,7 +2293,7 @@ def inverted_crossed_shuffle_dungeons(world, player: int): connect_mandatory_exits(world, dungeon_entrances, dungeon_exits, lw_dungeon_entrances_must_exit, player) connect_caves(world, dungeon_entrances, [], dungeon_exits, player) - assert not dungeon_exits # make sure all exits are accounted for + assert not dungeon_exits, "make sure all exits are accounted for" def unbias_some_entrances(world, Dungeon_Exits, Cave_Exits, Old_Man_House, Cave_Three_Exits): def shuffle_lists_in_list(ls): diff --git a/worlds/alttp/ItemPool.py b/worlds/alttp/ItemPool.py index 782cfade..e00c6667 100644 --- a/worlds/alttp/ItemPool.py +++ b/worlds/alttp/ItemPool.py @@ -545,7 +545,7 @@ def get_pool_core(world, player: int): pool.extend(diff.alwaysitems) def place_item(loc, item): - assert loc not in placed_items + assert loc not in placed_items, "cannot place item twice" placed_items[loc] = item # provide boots to major glitch dependent seeds @@ -681,7 +681,7 @@ def make_custom_item_pool(world, player): treasure_hunt_icon = None def place_item(loc, item): - assert loc not in placed_items + assert loc not in placed_items, "cannot place item twice" placed_items[loc] = item # Correct for insanely oversized item counts and take initial steps to handle undersized pools. diff --git a/worlds/factorio/Technologies.py b/worlds/factorio/Technologies.py index dfb6c056..7ffd1d17 100644 --- a/worlds/factorio/Technologies.py +++ b/worlds/factorio/Technologies.py @@ -419,7 +419,7 @@ progressive_technology_table: Dict[str, Technology] = {} for root in sorted_rows: progressive = progressive_rows[root] - assert all(tech in tech_table for tech in progressive) + assert all(tech in tech_table for tech in progressive), "declared a progressive technology without base technology" factorio_id += 1 progressive_technology = Technology(root, technology_table[progressive_rows[root][0]].ingredients, factorio_id, progressive,