Skip to content

Commit

Permalink
More mypy error fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fenhl committed Dec 12, 2024
1 parent 120555d commit ffddf77
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions RuleParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ def visit_Tuple(self, node: ast.Tuple) -> Any:

item, count = node.elts

if not isinstance(item, ast.Name) and not (isinstance(item, ast.Constant) and isinstance(item.value, str)):
raise Exception('Parse Error: first value must be an item. Got %s' % item.__class__.__name__, None if self.current_spot is None else self.current_spot.name, ast.dump(node, False))
if isinstance(item, ast.Constant) and isinstance(item.value, str):
item = ast.Name(id=escape_name(item.value), ctx=ast.Load())
if not isinstance(item, ast.Name):
raise Exception('Parse Error: first value must be an item. Got %s' % item.__class__.__name__, None if self.current_spot is None else self.current_spot.name, ast.dump(node, False))

if not (isinstance(count, ast.Name) or (isinstance(count, ast.Constant) and isinstance(count.value, int))):
raise Exception('Parse Error: second value must be a number. Got %s' % item.__class__.__name__, None if self.current_spot is None else self.current_spot.name, ast.dump(node, False))
Expand Down
10 changes: 7 additions & 3 deletions Rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def set_rules(world: World) -> None:
world.shop_prices[location.name] = dist_location.price
location.add_rule(create_shop_rule(location))
else:
add_item_rule(location, lambda location, item: item.type == 'Shop' and item.world.id == location.world.id)
elif location.type in ['Scrub', 'GrottoScrub']:
add_item_rule(location, lambda location, item: item.type == 'Shop' and item.world is not None and location.world is not None and item.world.id == location.world.id)
elif location.type in ('Scrub', 'GrottoScrub'):
location.add_rule(create_shop_rule(location))
else:
add_item_rule(location, lambda location, item: item.type != 'Shop')
Expand Down Expand Up @@ -99,6 +99,8 @@ def required_wallets(price: Optional[int]) -> int:
if price > 99:
return 1
return 0

assert location.world is not None
return location.world.parser.parse_rule('(Progressive_Wallet, %d)' % required_wallets(location.price))


Expand Down Expand Up @@ -140,6 +142,7 @@ def set_shop_rules(world: World):
wallet2 = world.parser.parse_rule('(Progressive_Wallet, 2)')
is_adult = world.parser.parse_rule('is_adult')
for location in world.get_filled_locations():
assert location.item is not None
if location.item.type == 'Shop':
# Add wallet requirements
if location.item.name in ['Buy Fish', 'Buy Goron Tunic', 'Buy Bombchu (20)', 'Buy Bombs (30)']:
Expand All @@ -162,7 +165,7 @@ def set_shop_rules(world: World):
'Buy Red Potion for 40 Rupees',
'Buy Red Potion for 50 Rupees',
"Buy Fairy's Spirit"]:
location.add_rule(State.has_bottle)
location.add_rule(State.has_bottle) # type: ignore #TODO figure out why mypy doesn't accept this
if location.item.name in ['Buy Bombchu (10)', 'Buy Bombchu (20)', 'Buy Bombchu (5)']:
location.add_rule(found_bombchus)

Expand All @@ -181,6 +184,7 @@ def set_entrances_based_rules(worlds: Collection[World]) -> None:
if location.type == 'Shop':
# If All Locations Reachable is on, prevent shops only ever reachable as child from containing Buy Goron Tunic and Buy Zora Tunic items
if not world.check_beatable_only:
assert location.parent_region is not None
if not search.can_reach(location.parent_region, age='adult'):
forbid_item(location, 'Buy Goron Tunic')
forbid_item(location, 'Buy Zora Tunic')
4 changes: 4 additions & 0 deletions SettingsList.py
Original file line number Diff line number Diff line change
Expand Up @@ -5463,6 +5463,8 @@ def build_close_match(name: str, value_type: str, source_list: Optional[list[str
elif value_type == 'location':
source = location_table.keys()
elif value_type == 'entrance':
assert isinstance(source_list, dict)
assert isinstance(source, list)
for pool in source_list.values():
for entrance in pool:
source.append(entrance.name)
Expand All @@ -5471,6 +5473,7 @@ def build_close_match(name: str, value_type: str, source_list: Optional[list[str
elif value_type == 'setting':
source = SettingInfos.setting_infos.keys()
elif value_type == 'choice':
assert source_list is not None
source = source_list
# Ensure name and source are type string to prevent errors
close_match = difflib.get_close_matches(str(name), map(str, source), 1)
Expand Down Expand Up @@ -5500,6 +5503,7 @@ def validate_settings(settings_dict: dict[str, Any], *, check_conflicts: bool =
continue
# Ensure that the given choice is a valid choice for the setting
elif info.choice_list and choice not in info.choice_list:
assert isinstance(choice, str)
raise ValueError('%r is not a valid choice for setting %r. %s' % (choice, setting, build_close_match(choice, 'choice', info.choice_list)))
# Ensure no conflicting settings are specified
if check_conflicts and info.disable is not None:
Expand Down
10 changes: 10 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[mypy]
files = OoTRandomizer.py

#TODO also type check these modules
[mypy-Goals.*]
ignore_errors = True
[mypy-Plandomizer.*]
ignore_errors = True
[mypy-Spoiler.*]
ignore_errors = True

0 comments on commit ffddf77

Please sign in to comment.