Skip to content

Commit 6e57885

Browse files
committed
Resolve several Ruff and Mypy warnings
Mostly applying and checking unsafe fixes
1 parent 2e8efdd commit 6e57885

37 files changed

+197
-158
lines changed

build_libtcod.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from cffi import FFI
1616

17+
# ruff: noqa: T201
18+
1719
sys.path.append(str(Path(__file__).parent)) # Allow importing local modules.
1820

1921
import build_sdl
@@ -50,14 +52,14 @@ class ParsedHeader:
5052

5153
def __init__(self, path: Path) -> None:
5254
"""Initialize and organize a header file."""
53-
self.path = path = path.resolve(True)
55+
self.path = path = path.resolve(strict=True)
5456
directory = path.parent
5557
depends = set()
5658
header = self.path.read_text(encoding="utf-8")
5759
header = RE_COMMENT.sub("", header)
5860
header = RE_CPLUSPLUS.sub("", header)
5961
for dependency in RE_INCLUDE.findall(header):
60-
depends.add((directory / str(dependency)).resolve(True))
62+
depends.add((directory / str(dependency)).resolve(strict=True))
6163
header = RE_PREPROCESSOR.sub("", header)
6264
header = RE_TAGS.sub("", header)
6365
header = RE_VAFUNC.sub("", header)
@@ -91,7 +93,7 @@ def walk_includes(directory: str) -> Iterator[ParsedHeader]:
9193
if file in HEADER_PARSE_EXCLUDES:
9294
continue
9395
if file.endswith(".h"):
94-
yield ParsedHeader(Path(path, file).resolve(True))
96+
yield ParsedHeader(Path(path, file).resolve(strict=True))
9597

9698

9799
def resolve_dependencies(

build_sdl.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
from pathlib import Path
1515
from typing import Any
1616

17-
import pcpp # type: ignore
17+
import pcpp # type: ignore[import-untyped]
1818
import requests
1919

2020
# This script calls a lot of programs.
21-
# ruff: noqa: S603, S607
21+
# ruff: noqa: S603, S607, T201
2222

2323
BIT_SIZE, LINKAGE = platform.architecture()
2424

@@ -141,7 +141,7 @@ def unpack_sdl2(version: str) -> Path:
141141
return sdl2_path
142142

143143

144-
class SDLParser(pcpp.Preprocessor): # type: ignore
144+
class SDLParser(pcpp.Preprocessor): # type: ignore[misc]
145145
"""A modified preprocessor to output code in a format for CFFI."""
146146

147147
def __init__(self) -> None:
@@ -159,7 +159,7 @@ def get_output(self) -> str:
159159
buffer.write(f"#define {name} ...\n")
160160
return buffer.getvalue()
161161

162-
def on_include_not_found(self, is_malformed: bool, is_system_include: bool, curdir: str, includepath: str) -> None:
162+
def on_include_not_found(self, is_malformed: bool, is_system_include: bool, curdir: str, includepath: str) -> None: # noqa: ARG002, FBT001
163163
"""Remove bad includes such as stddef.h and stdarg.h."""
164164
raise pcpp.OutputDirective(pcpp.Action.IgnoreAndRemove)
165165

@@ -190,7 +190,7 @@ def on_directive_handle(
190190
self,
191191
directive: Any, # noqa: ANN401
192192
tokens: list[Any],
193-
if_passthru: bool,
193+
if_passthru: bool, # noqa: FBT001
194194
preceding_tokens: list[Any],
195195
) -> Any: # noqa: ANN401
196196
"""Catch and store definitions."""
@@ -204,7 +204,7 @@ def on_directive_handle(
204204

205205
check_sdl_version()
206206

207-
if sys.platform in ["win32", "darwin"]:
207+
if sys.platform == "win32" or sys.platform == "darwin":
208208
SDL2_PARSE_PATH = unpack_sdl2(SDL2_PARSE_VERSION)
209209
SDL2_BUNDLE_PATH = unpack_sdl2(SDL2_BUNDLE_VERSION)
210210

docs/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
THIS_DIR = Path(__file__).parent
2929

30+
# ruff: noqa: ERA001
31+
3032
# -- General configuration ------------------------------------------------
3133

3234
# If your documentation needs a minimal Sphinx version, state it here.
@@ -79,7 +81,7 @@
7981
)
8082
release = git_describe.stdout.strip()
8183
assert release
82-
print("release version: %r" % release)
84+
print(f"release version: {release!r}")
8385

8486
# The short X.Y version.
8587
match_version = re.match(r"([0-9]+\.[0-9]+).*?", release)

docs/tcod/getting-started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ integer increments.
5555
print(event) # Print event names and attributes.
5656
match event:
5757
case tcod.event.Quit():
58-
raise SystemExit()
58+
raise SystemExit
5959
# The window will be closed after the above with-block exits.
6060
6161
@@ -113,7 +113,7 @@ clearing the console every frame and replacing it only on resizing the window.
113113
print(event) # Print event names and attributes.
114114
match event:
115115
case tcod.event.Quit():
116-
raise SystemExit()
116+
raise SystemExit
117117
case tcod.event.WindowResized(type="WindowSizeChanged"):
118118
pass # The next call to context.new_console may return a different size.
119119

examples/distribution/PyInstaller/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def main() -> None:
3030
context.present(console)
3131
for event in tcod.event.wait():
3232
if isinstance(event, tcod.event.Quit):
33-
raise SystemExit()
33+
raise SystemExit
3434

3535

3636
if __name__ == "__main__":

examples/distribution/cx_Freeze/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def main() -> None:
2020
context.present(console)
2121
for event in tcod.event.wait():
2222
if isinstance(event, tcod.event.Quit):
23-
raise SystemExit()
23+
raise SystemExit
2424

2525

2626
if __name__ == "__main__":

examples/eventget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def main() -> None:
4141
context.convert_event(event) # Set tile coordinates for event.
4242
print(repr(event))
4343
if isinstance(event, tcod.event.Quit):
44-
raise SystemExit()
44+
raise SystemExit
4545
if isinstance(event, tcod.event.WindowResized) and event.type == "WindowSizeChanged":
4646
console = context.new_console()
4747
if isinstance(event, tcod.event.ControllerDevice):

examples/framerate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def main() -> None:
138138
for event in tcod.event.get():
139139
context.convert_event(event) # Set tile coordinates for event.
140140
if isinstance(event, tcod.event.Quit):
141-
raise SystemExit()
141+
raise SystemExit
142142
if isinstance(event, tcod.event.MouseWheel):
143143
desired_fps = max(1, desired_fps + event.y)
144144

examples/samples_tcod.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
import time
1515
import warnings
1616
from pathlib import Path
17-
from typing import Any
17+
from typing import TYPE_CHECKING, Any
1818

1919
import numpy as np
20-
from numpy.typing import NDArray
2120

2221
import tcod.cffi
2322
import tcod.context
@@ -29,6 +28,9 @@
2928
from tcod import libtcodpy
3029
from tcod.sdl.video import WindowFlags
3130

31+
if TYPE_CHECKING:
32+
from numpy.typing import NDArray
33+
3234
# ruff: noqa: S311
3335

3436
if not sys.warnoptions:
@@ -96,13 +98,13 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
9698
libtcodpy.sys_save_screenshot()
9799
print("png")
98100
elif event.sym == tcod.event.KeySym.ESCAPE:
99-
raise SystemExit()
101+
raise SystemExit
100102
elif event.sym in RENDERER_KEYS:
101103
# Swap the active context for one with a different renderer.
102104
init_context(RENDERER_KEYS[event.sym])
103105

104106
def ev_quit(self, event: tcod.event.Quit) -> None:
105-
raise SystemExit()
107+
raise SystemExit
106108

107109

108110
class TrueColorSample(Sample):
@@ -307,7 +309,7 @@ def on_draw(self) -> None:
307309
sample_console.print(
308310
2,
309311
2,
310-
"%s (ENTER to change)" % self.FLAG_NAMES[self.bk_flag & 0xFF],
312+
f"{self.FLAG_NAMES[self.bk_flag & 0xFF]} (ENTER to change)",
311313
fg=WHITE,
312314
bg=None,
313315
)
@@ -431,26 +433,26 @@ def on_draw(self) -> None:
431433
sample_console.print(2, 2 + cur_func, text, fg=WHITE, bg=LIGHT_BLUE)
432434
else:
433435
sample_console.print(2, 2 + cur_func, text, fg=GREY, bg=None)
434-
sample_console.print(2, 11, "Y/H : zoom (%2.1f)" % self.zoom, fg=WHITE, bg=None)
436+
sample_console.print(2, 11, f"Y/H : zoom ({self.zoom:2.1f})", fg=WHITE, bg=None)
435437
if self.implementation != tcod.noise.Implementation.SIMPLE:
436438
sample_console.print(
437439
2,
438440
12,
439-
"E/D : hurst (%2.1f)" % self.hurst,
441+
f"E/D : hurst ({self.hurst:2.1f})",
440442
fg=WHITE,
441443
bg=None,
442444
)
443445
sample_console.print(
444446
2,
445447
13,
446-
"R/F : lacunarity (%2.1f)" % self.lacunarity,
448+
f"R/F : lacunarity ({self.lacunarity:2.1f})",
447449
fg=WHITE,
448450
bg=None,
449451
)
450452
sample_console.print(
451453
2,
452454
14,
453-
"T/G : octaves (%2.1f)" % self.octaves,
455+
f"T/G : octaves ({self.octaves:2.1f})",
454456
fg=WHITE,
455457
bg=None,
456458
)
@@ -991,7 +993,7 @@ def on_draw(self) -> None:
991993
walls = "OFF"
992994
if bsp_room_walls:
993995
walls = "ON"
994-
sample_console.print(1, 6, "2 : room walls %s" % walls, fg=WHITE, bg=None)
996+
sample_console.print(1, 6, f"2 : room walls {walls}", fg=WHITE, bg=None)
995997
# render the level
996998
for y in range(SAMPLE_SCREEN_HEIGHT):
997999
for x in range(SAMPLE_SCREEN_WIDTH):
@@ -1161,7 +1163,7 @@ def on_draw(self) -> None:
11611163
sample_console.print(
11621164
1,
11631165
1,
1164-
"%s\n\n+ : next generator\n- : prev generator" % self.sets[self.current_set],
1166+
f"{self.sets[self.current_set]}\n\n+ : next generator\n- : prev generator",
11651167
fg=WHITE,
11661168
bg=None,
11671169
)
@@ -1214,8 +1216,8 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
12141216

12151217
# the coordinates of all tiles in the screen, as numpy arrays.
12161218
# example: (4x3 pixels screen)
1217-
# xc = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
1218-
# yc = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]
1219+
# xc = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] # noqa: ERA001
1220+
# yc = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] # noqa: ERA001
12191221
if numpy_available:
12201222
(xc, yc) = np.meshgrid(range(SCREEN_W), range(SCREEN_H))
12211223
# translate coordinates of all pixels to center
@@ -1504,7 +1506,7 @@ def handle_events() -> None:
15041506

15051507
SAMPLES[cur_sample].dispatch(event)
15061508
if isinstance(event, tcod.event.Quit):
1507-
raise SystemExit()
1509+
raise SystemExit
15081510

15091511

15101512
def draw_samples_menu() -> None:
@@ -1518,7 +1520,7 @@ def draw_samples_menu() -> None:
15181520
root_console.print(
15191521
2,
15201522
46 - (len(SAMPLES) - i),
1521-
" %s" % sample.name.ljust(19),
1523+
f" {sample.name.ljust(19)}",
15221524
fg,
15231525
bg,
15241526
alignment=libtcodpy.LEFT,

examples/sdl-hello-world.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def render_text(renderer: tcod.sdl.render.Renderer, text: str) -> tcod.sdl.rende
1717
"""Render text, upload it to VRAM, then return it as an SDL Texture."""
1818
# Use Pillow to render the font.
1919
_left, _top, right, bottom = font.getbbox(text)
20-
width, height = right, bottom
20+
width, height = int(right), int(bottom)
2121
image = Image.new("RGBA", (width, height))
2222
draw = ImageDraw.Draw(image)
2323
draw.text((0, 0), text, font=font)
@@ -43,7 +43,7 @@ def main() -> None:
4343
renderer.present()
4444
for event in tcod.event.get():
4545
if isinstance(event, tcod.event.Quit):
46-
raise SystemExit()
46+
raise SystemExit
4747

4848

4949
if __name__ == "__main__":

0 commit comments

Comments
 (0)