Skip to content

Commit

Permalink
Mypy support
Browse files Browse the repository at this point in the history
  • Loading branch information
theCapypara committed Nov 14, 2021
1 parent d5fe967 commit dbc4d27
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 53 deletions.
22 changes: 19 additions & 3 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ name: Build, test and publish
on: [push, pull_request]

jobs:
typechecks:
runs-on: ubuntu-latest
name: Type checks
strategy:
max-parallel: 5
matrix:
python-version: [ "3.8", "3.9", "3.10" ]
steps:
- uses: actions/checkout@v1
- uses: theCapypara/mypy-check@master
name: Run type checks
with:
mypy_flags: '--config-file mypy.ini'
requirements: '-r requirements.txt'
python_version: '${{ matrix.python-version }}'

build:
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
name: Build the Python wheel
steps:
# For tags we assume the version in setup.py is correct!
Expand All @@ -29,7 +45,7 @@ jobs:
- name: Build Python wheels
uses: RalfG/[email protected]_x86_64
with:
python-versions: 'cp36-cp36m cp37-cp37m cp38-cp38 cp39-cp39 cp310-cp310'
python-versions: 'cp38-cp38 cp39-cp39 cp310-cp310'
- name: Upload wheels
uses: actions/upload-artifact@v2
with:
Expand All @@ -38,7 +54,7 @@ jobs:
deploy:
if: startsWith(github.ref, 'refs/tags/')
needs: build
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
name: Deploy wheels to PyPI
steps:
- name: Download wheels
Expand Down
54 changes: 54 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[mypy]
warn_unused_configs = True
mypy_path = .
explicit_package_bases = True
namespace_packages = True
files = skytemple_randomizer/**/*.py

[mypy-chara_wan.*]
ignore_missing_imports = True

[mypy-bultins.*]
ignore_missing_imports = True

[mypy-dungeon_eos.*]
ignore_missing_imports = True

[mypy-igraph]
ignore_missing_imports = True

[mypy-ndspy.*]
ignore_missing_imports = True

[mypy-explorerscript.*]
ignore_missing_imports = True

[mypy-PIL.*]
ignore_missing_imports = True

[mypy-pil]
ignore_missing_imports = True

[mypy-desmume]
ignore_missing_imports = True

[mypy-Foundation]
ignore_missing_imports = True

[mypy-appdirs]
ignore_missing_imports = True

[skytemple_rust.pmd_wan]
ignore_missing_imports = True

[mypy-gi.*]
ignore_missing_imports = True

[mypy-skytemple_icons]
ignore_missing_imports = True

[mypy-pypresence]
ignore_missing_imports = True

[mypy-jsonschema]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ explorerscript==0.1.0
PyYAML==5.4.1
jsonschema==4.1.2
packaging
types-PyYAML
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
'Development Status :: 4 - Beta',
'Programming Language :: Python',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
Expand Down
24 changes: 12 additions & 12 deletions skytemple_randomizer/frontend/gtk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,43 +148,43 @@ def _handle(self, typ: type, field_name=None):
return typ(self._handle(int, field_name))
elif typ == bool:
assert field_name, "Field name must be set for primitive"
w: Gtk.Switch = self._ui_get('switch_' + field_name)
w = self._ui_get('switch_' + field_name)
return w.get_active()
elif typ == int:
assert field_name, "Field name must be set for primitive"
w: Gtk.ComboBox = self._ui_get('cb_' + field_name)
w = self._ui_get('cb_' + field_name)
return int(w.get_active_id())
elif typ == IntRange:
assert field_name, "Field name must be set for primitive"
w: Gtk.Scale = self._ui_get('scale_' + field_name)
w = self._ui_get('scale_' + field_name)
return typ(int(w.get_value()))
elif typ == str:
assert field_name, "Field name must be set for primitive"
try:
w: Gtk.Entry = self._ui_get('entry_' + field_name)
w = self._ui_get('entry_' + field_name)
return w.get_text()
except ValueError:
w: Gtk.TextView = self._ui_get('text_' + field_name)
w = self._ui_get('text_' + field_name)
buffer = w.get_buffer()
return buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), False)
elif typ == Dict[int, DungeonSettingsConfig]:
w: Gtk.TreeView = self._ui_get('tree_' + field_name)
w = self._ui_get('tree_' + field_name)
s: Gtk.ListStore = w.get_model()
d = {}
for idx, name, randomize, monster_houses, randomize_weather, unlock, enemy_iq in s:
d[idx] = {'randomize': randomize, 'monster_houses': monster_houses,
'randomize_weather': randomize_weather, 'unlock': unlock, 'enemy_iq': enemy_iq}
return d
elif typ == List[int]:
w: Gtk.TreeView = self._ui_get('tree_' + field_name)
s: Gtk.ListStore = w.get_model()
d = []
w = self._ui_get('tree_' + field_name)
s = w.get_model()
dd: List[int] = []
for idx, name, use in s:
if use:
d.append(idx)
return d
dd.append(idx)
return dd
else:
raise TypeError(f"Unknown type for {self.__name__}: {typ}")
raise TypeError(f"Unknown type for {self.__name__}: {typ}") # type: ignore

def _ui_get(self, n):
w: Gtk.Switch = self.builder.get_object(n)
Expand Down
8 changes: 4 additions & 4 deletions skytemple_randomizer/frontend/server_based/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from asyncio import sleep
from datetime import datetime
from functools import partial
from typing import Callable, Dict
from typing import Callable, Dict, Any

import tornado.web
from ndspy.rom import NintendoDSRom
Expand All @@ -43,7 +43,7 @@
from skytemple_randomizer.randomizer_thread import RandomizerThread
from skytemple_randomizer.status import Status

PORT = os.getenv('PORT', 44235)
PORT = int(os.getenv('PORT', 44235))
LOOP = asyncio.get_event_loop()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -79,7 +79,7 @@ def get_additional_scripts(self):

class RomStorage:
# we are never cleaning this. fine for Android, not fine for a REAL web server based approach!
roms: Dict[any, NintendoDSRom] = {}
roms: Dict[Any, NintendoDSRom] = {}


class UploadHandler(tornado.web.RequestHandler):
Expand Down Expand Up @@ -110,7 +110,7 @@ async def get(self, *args, **kwargs):

# noinspection PyAbstractClass
class WebsocketHandler(websocket.WebSocketHandler):
def __init__(self, application: tornado.web.Application, request: httputil.HTTPServerRequest, **kwargs: any):
def __init__(self, application: tornado.web.Application, request: httputil.HTTPServerRequest, **kwargs):
super().__init__(application, request, **kwargs)
self.randomization_is_running = False
self.rom = None
Expand Down
3 changes: 2 additions & 1 deletion skytemple_randomizer/randomizer/dungeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def _resize_floor_list(self, dungeons: Dict[int, DungeonDefinition], new_dungeon
# ---------------- Deal with dungeon errors; copied from SkyTemple dungeon module

def _fix_error(self, e: DungeonValidatorError):
assert self.mappa
if isinstance(e, DungeonTotalFloorCountInvalidError):
self.dungeons[e.dungeon_id].number_floors_in_group = e.expected_floor_count_in_group
elif isinstance(e, InvalidFloorListReferencedError) or isinstance(e, FloorReusedError):
Expand Down Expand Up @@ -520,7 +521,7 @@ def _mappa_generate_and_insert_new_floor_list(self):

def _mappa_generate_new_floor(self) -> MappaFloor:
"""Copies the first floor of test dungeon and returns it"""
return MappaFloor.from_xml(self.mappa.floor_lists[0][0].to_xml())
return MappaFloor.from_xml(self.mappa.floor_lists[0][0].to_xml()) # type: ignore


def check_consecutive(l):
Expand Down
6 changes: 4 additions & 2 deletions skytemple_randomizer/randomizer/dungeon_unlocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
#
# You should have received a copy of the GNU General Public License
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
from typing import List

from explorerscript.source_map import SourceMap
from explorerscript.ssb_converting.compiler.label_finalizer import LabelFinalizer
from explorerscript.ssb_converting.compiler.label_jump_to_remover import OpsLabelJumpToRemover
from explorerscript.ssb_converting.compiler.utils import strip_last_label, Counter
from explorerscript.ssb_converting.decompiler.label_jump_to_resolver import OpsLabelJumpToResolver
from explorerscript.ssb_converting.ssb_data_types import SsbOpParamConstString
from explorerscript.ssb_converting.ssb_data_types import SsbOpParamConstString, SsbOperation
from explorerscript.ssb_converting.ssb_special_ops import SsbLabel, SsbLabelJump
from skytemple_files.common.types.file_types import FileType
from skytemple_files.script.ssb.model import Ssb, SkyTempleSsbOperation
Expand All @@ -37,7 +39,7 @@ def step_count(self) -> int:
def run(self, status: Status):
status.step('Unlocking dungeons...')

new_ops = []
new_ops: List[SsbOperation] = []
coro_id = self.static_data.script_data.common_routine_info__by_name['EVENT_DIVIDE'].id
ops = self.static_data.script_data.op_codes__by_name

Expand Down
6 changes: 3 additions & 3 deletions skytemple_randomizer/randomizer/fixed_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ def _assign_dungeon_floor_regular_tileset(self, floor_list: List[MappaFloor], fl
def _get_special_in_floor(self, floor: FixedFloor):
l = []
for action in floor.actions:
if isinstance(action, EntityRule) or \
if isinstance(action, EntityRule) or (isinstance(action, TileRule) and ( \
action.tr_type == TileRuleType.LEADER_SPAWN or \
action.tr_type == TileRuleType.ATTENDANT1_SPAWN or \
action.tr_type == TileRuleType.ATTENDANT2_SPAWN or \
action.tr_type == TileRuleType.ATTENDANT3_SPAWN:
action.tr_type == TileRuleType.ATTENDANT3_SPAWN)):
l.append(action)
return l

Expand All @@ -123,7 +123,7 @@ def _get_random_room(self, entities_and_special_tiles_to_preserve: List[FixedFlo
while len(entities_and_special_tiles_to_preserve) > 0:
index = randrange(0, len(actions))
if actions[index] == FLOOR:
actions[index] = entities_and_special_tiles_to_preserve.pop()
actions[index] = entities_and_special_tiles_to_preserve.pop() # type: ignore

return width, height, actions

Expand Down
4 changes: 3 additions & 1 deletion skytemple_randomizer/randomizer/iq_tactics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
from random import choice, randrange

from typing import List

from skytemple_files.common.util import get_binary_from_rom_ppmdu, set_binary_in_rom_ppmdu
from skytemple_files.hardcoded.iq import HardcodedIq, IqGroupsSkills
from skytemple_files.hardcoded.tactics import HardcodedTactics
Expand Down Expand Up @@ -78,7 +80,7 @@ def run(self, status: Status):

new_iq_gains = []
for l in iq_gains:
li = []
li: List[int] = []
new_iq_gains.append(li)
for e in l:
li.append(randrange(1, 6))
Expand Down
4 changes: 3 additions & 1 deletion skytemple_randomizer/randomizer/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
from random import choice

from typing import Dict

from skytemple_files.common.types.file_types import FileType
from skytemple_randomizer.randomizer.abstract import AbstractRandomizer
from skytemple_randomizer.randomizer.util.util import replace_text_script, random_txt_line, get_all_string_files
Expand All @@ -40,7 +42,7 @@ def run(self, status: Status):

rename_dungeon_map_all = {}
for lang, strings in get_all_string_files(self.rom, self.static_data):
rename_dungeon_map = {}
rename_dungeon_map: Dict[str, str] = {}
rename_dungeon_map_all[lang] = rename_dungeon_map
for main, sele, sdba, bann in zip(
range(dunge_names_main.begin, dunge_names_main.end),
Expand Down
2 changes: 1 addition & 1 deletion skytemple_randomizer/randomizer/moveset.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def run(self, status: Status):

if self.config['pokemon']['tm_hm_movesets']:
status.step("Randomizing TM/HM movesets...")
item_p: ItemP = FileType.ITEM_P.deserialize(self.rom.getFileByName('BALANCE/item_p.bin'))
item_p = FileType.ITEM_P.deserialize(self.rom.getFileByName('BALANCE/item_p.bin'))
move_ids = []
for item in item_p.item_list:
if item.category == 5:
Expand Down
6 changes: 3 additions & 3 deletions skytemple_randomizer/randomizer/npc.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def run(self, status: Status):
status.step("Replacing main text that mentions NPCs...")
names_mapped_all = {}
for lang, string_file in get_all_string_files(self.rom, self.static_data):
names_mapped = {}
names_mapped: Dict[str, str] = {}
names_mapped_all[lang] = names_mapped
for old, new in mapped_actors.items():
old_base = old % 600
Expand Down Expand Up @@ -81,9 +81,9 @@ def _randomize_actors(self, string_file, pokemon_string_data) -> Dict[int, int]:
)
md = FileType.MD.deserialize(self.rom.getFileByName('BALANCE/monster.md'))

mapped = {}
mapped: Dict[int, int] = {}
# We want to map actors with the same name to the same ID
mapped_for_names = {}
mapped_for_names: Dict[str, int] = {}
old_entid_bases = [actor.entid % NUM_ENTITIES for actor in actor_list.list]
for actor in actor_list.list:
if actor.entid > 0:
Expand Down
3 changes: 2 additions & 1 deletion skytemple_randomizer/randomizer/portrait_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import urllib.request

from ndspy.rom import NintendoDSRom
from typing import List, Tuple

from skytemple_files.common.ppmdu_config.data import Pmd2Data
from skytemple_files.common.types.file_types import FileType
Expand All @@ -43,7 +44,7 @@
class PortraitDownloader(AbstractRandomizer):
def __init__(self, config: RandomizerConfig, rom: NintendoDSRom, static_data: Pmd2Data, seed: str, frontend: AbstractFrontend):
super().__init__(config, rom, static_data, seed, frontend)
self._debugs = []
self._debugs: List[Tuple[str, str, int, str, str]] = []

def step_count(self) -> int:
if self.config['improvements']['download_portraits']:
Expand Down
Loading

0 comments on commit dbc4d27

Please sign in to comment.