Skip to content

Commit 6df1b1f

Browse files
committed
Clean up and enable linting for tests and scripts.
Add type hints to deprecated parser functions. These functions can now take str. Fix Python version mypy setting. For now this will be set to Python 3.8 globally and other tests will be used to check earlier versions. Remove dev scripts, these were old experiments for tdl. Associate PyInstaller spec files with Python.
1 parent 791c766 commit 6df1b1f

25 files changed

+294
-760
lines changed

.github/workflows/python-lint.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Install Python dependencies
2424
run: |
2525
python -m pip install --upgrade pip
26-
python -m pip install flake8 mypy black isort
26+
python -m pip install flake8 mypy black isort pytest
2727
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
2828
- name: Fake initialize package
2929
run: |
@@ -32,23 +32,23 @@ jobs:
3232
uses: liskin/gh-problem-matcher-wrap@v1
3333
with:
3434
linters: flake8
35-
run: flake8 scripts/ tcod/
35+
run: flake8 scripts/ tcod/ tests/
3636
- name: MyPy
3737
if: always()
3838
uses: liskin/gh-problem-matcher-wrap@v1
3939
with:
4040
linters: mypy
41-
run: mypy --show-column-numbers scripts/ tcod/
41+
run: mypy --show-column-numbers .
4242
- name: isort
4343
uses: liskin/gh-problem-matcher-wrap@v1
4444
with:
4545
linters: isort
46-
run: isort scripts/ tcod/ tests/ *.py --check
46+
run: isort scripts/ tcod/ tests/ --check
4747
- name: isort (examples)
4848
uses: liskin/gh-problem-matcher-wrap@v1
4949
with:
5050
linters: isort
5151
run: isort examples/ --check --thirdparty tcod
5252
- name: Black
5353
run: |
54-
black --check examples/ scripts/ tcod/ tests/ *.py
54+
black --check examples/ scripts/ tcod/ tests/ *.py

.vscode/settings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"python.linting.flake8Enabled": true,
1212
"python.linting.mypyEnabled": true,
1313
"python.linting.mypyArgs": [
14-
"--python-version 3.8", // Python 3.8 supported by examples.
1514
"--follow-imports=silent",
1615
"--show-column-numbers"
1716
],
18-
"python.formatting.provider": "black"
17+
"python.formatting.provider": "black",
18+
"files.associations": {
19+
"*.spec": "python",
20+
}
1921
}

build_libtcod.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import subprocess
99
import sys
1010
import zipfile
11-
from typing import Any, Dict, Iterable, Iterator, List, Set, Tuple
11+
from typing import Any, Dict, Iterable, Iterator, List, Set, Tuple, Union
1212

1313
try:
1414
from urllib import urlretrieve # type: ignore
@@ -165,7 +165,7 @@ def unpack_sdl2(version: str) -> str:
165165
if sdl2_arc.endswith(".zip"):
166166
with zipfile.ZipFile(sdl2_arc) as zf:
167167
zf.extractall("dependencies/")
168-
else:
168+
elif sys.platform == "darwin":
169169
assert sdl2_arc.endswith(".dmg")
170170
subprocess.check_call(["hdiutil", "mount", sdl2_arc])
171171
subprocess.check_call(["mkdir", "-p", sdl2_dir])
@@ -190,7 +190,7 @@ def unpack_sdl2(version: str) -> str:
190190
sources = [] # type: List[str]
191191

192192
libraries = []
193-
library_dirs = []
193+
library_dirs: List[str] = []
194194
define_macros = [("Py_LIMITED_API", 0x03060000)] # type: List[Tuple[str, Any]]
195195

196196
sources += walk_sources("tcod/")
@@ -281,7 +281,7 @@ def fix_header(filepath: str) -> None:
281281
tdl_build = os.environ.get("TDL_BUILD", "RELEASE").upper()
282282

283283
MSVC_CFLAGS = {"DEBUG": ["/Od"], "RELEASE": ["/GL", "/O2", "/GS-", "/wd4996"]}
284-
MSVC_LDFLAGS = {"DEBUG": [], "RELEASE": ["/LTCG"]}
284+
MSVC_LDFLAGS: Dict[str, List[str]] = {"DEBUG": [], "RELEASE": ["/LTCG"]}
285285
GCC_CFLAGS = {
286286
"DEBUG": ["-std=c99", "-Og", "-g", "-fPIC"],
287287
"RELEASE": [
@@ -348,7 +348,7 @@ def fix_header(filepath: str) -> None:
348348
'''
349349

350350

351-
def find_sdl_attrs(prefix: str) -> Iterator[Tuple[str, Any]]:
351+
def find_sdl_attrs(prefix: str) -> Iterator[Tuple[str, Union[int, str, Any]]]:
352352
"""Return names and values from `tcod.lib`.
353353
354354
`prefix` is used to filter out which names to copy.
@@ -378,9 +378,7 @@ def parse_sdl_attrs(prefix: str, all_names: List[str]) -> Tuple[str, str]:
378378
all_names.append(name)
379379
names.append("%s = %s" % (name, value))
380380
lookup.append('%s: "%s"' % (value, name))
381-
names = "\n".join(names)
382-
lookup = "{\n %s,\n}" % (",\n ".join(lookup),)
383-
return names, lookup
381+
return "\n".join(names), "{\n %s,\n}" % (",\n ".join(lookup),)
384382

385383

386384
EXCLUDE_CONSTANTS = [
@@ -418,7 +416,6 @@ def update_module_all(filename: str, new_all: str) -> None:
418416

419417
def generate_enums(prefix: str) -> Iterator[str]:
420418
"""Generate attribute assignments suitable for a Python enum."""
421-
prefix_len = len(prefix) - len("SDL_") + 1
422419
for name, value in sorted(find_sdl_attrs(prefix), key=lambda item: item[1]):
423420
name = name.split("_", 1)[1]
424421
if name.isdigit():
@@ -471,10 +468,10 @@ def write_library_constants() -> None:
471468
f.write("%s = %r\n" % (name[5:], color))
472469
all_names.append(name[5:])
473470

474-
all_names = ",\n ".join('"%s"' % name for name in all_names)
475-
f.write("\n__all__ = [\n %s,\n]\n" % (all_names,))
476-
update_module_all("tcod/__init__.py", all_names)
477-
update_module_all("tcod/libtcodpy.py", all_names)
471+
all_names_merged = ",\n ".join('"%s"' % name for name in all_names)
472+
f.write("\n__all__ = [\n %s,\n]\n" % (all_names_merged,))
473+
update_module_all("tcod/__init__.py", all_names_merged)
474+
update_module_all("tcod/libtcodpy.py", all_names_merged)
478475

479476
with open("tcod/event_constants.py", "w") as f:
480477
all_names = []
@@ -490,8 +487,8 @@ def write_library_constants() -> None:
490487

491488
f.write("\n# --- SDL wheel ---\n")
492489
f.write("%s\n_REVERSE_WHEEL_TABLE = %s\n" % parse_sdl_attrs("SDL_MOUSEWHEEL", all_names))
493-
all_names = ",\n ".join('"%s"' % name for name in all_names)
494-
f.write("\n__all__ = [\n %s,\n]\n" % (all_names,))
490+
all_names_merged = ",\n ".join('"%s"' % name for name in all_names)
491+
f.write("\n__all__ = [\n %s,\n]\n" % (all_names_merged,))
495492

496493
with open("tcod/event.py", "r") as f:
497494
event_py = f.read()

dev/benchmark.py

Lines changed: 0 additions & 117 deletions
This file was deleted.

dev/noise_gen.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

dev/samples.py

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)