Skip to content

Commit

Permalink
Type hints and other cleanup.
Browse files Browse the repository at this point in the history
Adds type hints to all class/instance attributes and function definitions to make it easier to know what variables contain while working on the code.

These are likely far from perfect as I learned while working through all of them, but at the very least nothing should be broken.
  • Loading branch information
Cuphat committed May 19, 2023
1 parent 044eb12 commit cbf50e8
Show file tree
Hide file tree
Showing 52 changed files with 3,921 additions and 3,717 deletions.
27 changes: 15 additions & 12 deletions CI.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# This script is called by GitHub Actions, see .github/workflows/python.yml
# To fix code style errors, run: python3 ./CI.py --fix --no_unit_tests

import argparse
import json
import sys
from io import StringIO
import unittest
import os.path
import pathlib
import argparse
import sys
import unittest
from io import StringIO
from typing import NoReturn


import Unittest as Tests
from SettingsList import logic_tricks, validate_settings
from Utils import data_path


def error(msg, can_fix):
def error(msg: str, can_fix: bool) -> None:
if not hasattr(error, "count"):
error.count = 0
print(msg, file=sys.stderr)
Expand All @@ -25,7 +27,7 @@ def error(msg, can_fix):
error.cannot_fix = True


def run_unit_tests():
def run_unit_tests() -> None:
# Run Unit Tests
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream)
Expand All @@ -38,7 +40,7 @@ def run_unit_tests():
error('Unit Tests had an error, see output above.', False)


def check_presets_formatting(fix_errors=False):
def check_presets_formatting(fix_errors: bool = False) -> None:
# Check the code style of presets_default.json
with open(data_path('presets_default.json'), encoding='utf-8') as f:
presets = json.load(f)
Expand All @@ -59,7 +61,8 @@ def check_presets_formatting(fix_errors=False):
json.dump(presets, file, indent=4)
print(file=file)

def check_hell_mode_tricks(fix_errors=False):

def check_hell_mode_tricks(fix_errors: bool = False) -> None:
# Check for tricks missing from Hell Mode preset.
with open(data_path('presets_default.json'), encoding='utf-8') as f:
presets = json.load(f)
Expand All @@ -79,11 +82,11 @@ def check_hell_mode_tricks(fix_errors=False):
print(file=file)


def check_code_style(fix_errors=False):
def check_code_style(fix_errors: bool = False) -> None:
# Check for code style errors
repo_dir = pathlib.Path(os.path.dirname(os.path.realpath(__file__)))

def check_file_format(path):
def check_file_format(path: pathlib.Path):
fixed = ''
with path.open(encoding='utf-8', newline='') as file:
path = path.relative_to(repo_dir)
Expand Down Expand Up @@ -129,7 +132,7 @@ def check_file_format(path):
check_file_format(repo_dir / 'data' / 'presets_default.json')


def run_ci_checks():
def run_ci_checks() -> NoReturn:
parser = argparse.ArgumentParser()
parser.add_argument('--no_unit_tests', help="Skip unit tests", action='store_true')
parser.add_argument('--only_unit_tests', help="Only run unit tests", action='store_true')
Expand All @@ -147,7 +150,7 @@ def run_ci_checks():
exit_ci(args.fix)


def exit_ci(fix_errors=False):
def exit_ci(fix_errors: bool = False) -> NoReturn:
if hasattr(error, "count") and error.count:
print(f'CI failed with {error.count} errors.', file=sys.stderr)
if fix_errors:
Expand Down
Loading

0 comments on commit cbf50e8

Please sign in to comment.