Skip to content

Commit 392b0ce

Browse files
committed
Refactoring pass, fix or suppress warnings
Fix critical issues with samples_tcod.py
1 parent 70ec2cc commit 392b0ce

24 files changed

+170
-170
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"ALTERASE",
3434
"arange",
3535
"ARCHS",
36+
"arctan",
3637
"asarray",
3738
"ascontiguousarray",
3839
"astar",
@@ -190,6 +191,7 @@
190191
"intersphinx",
191192
"isinstance",
192193
"isort",
194+
"issubdtype",
193195
"itemsize",
194196
"itleref",
195197
"ivar",
@@ -338,6 +340,7 @@
338340
"pypiwin",
339341
"pypy",
340342
"pytest",
343+
"PYTHONHASHSEED",
341344
"PYTHONOPTIMIZE",
342345
"Pyup",
343346
"quickstart",

examples/samples_tcod.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def on_draw(self) -> None:
118118
self.slide_corner_colors()
119119
self.interpolate_corner_colors()
120120
self.darken_background_characters()
121-
self.randomize_sample_conole()
121+
self.randomize_sample_console()
122122
self.print_banner()
123123

124124
def slide_corner_colors(self) -> None:
@@ -143,7 +143,7 @@ def darken_background_characters(self) -> None:
143143
sample_console.fg[:] = sample_console.bg[:]
144144
sample_console.fg[:] //= 2
145145

146-
def randomize_sample_conole(self) -> None:
146+
def randomize_sample_console(self) -> None:
147147
# randomize sample console characters
148148
sample_console.ch[:] = np.random.randint(
149149
low=ord("a"),
@@ -406,7 +406,7 @@ def on_draw(self) -> None:
406406
rect_h = 13
407407
if self.implementation == tcod.noise.Implementation.SIMPLE:
408408
rect_h = 10
409-
sample_console.draw_semigraphics(self.img)
409+
sample_console.draw_semigraphics(np.asarray(self.img))
410410
sample_console.draw_rect(
411411
2,
412412
2,
@@ -669,7 +669,7 @@ def __init__(self) -> None:
669669
self.using_astar = True
670670
self.recalculate = False
671671
self.busy = 0.0
672-
self.oldchar = " "
672+
self.old_char = " "
673673

674674
self.map = tcod.map.Map(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)
675675
for y in range(SAMPLE_SCREEN_HEIGHT):
@@ -778,33 +778,33 @@ def on_draw(self) -> None:
778778
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
779779
if event.sym == tcod.event.KeySym.i and self.dy > 0:
780780
# destination move north
781-
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
781+
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
782782
self.dy -= 1
783-
self.oldchar = sample_console.ch[self.dx, self.dy]
783+
self.old_char = sample_console.ch[self.dx, self.dy]
784784
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
785785
if SAMPLE_MAP[self.dx, self.dy] == " ":
786786
self.recalculate = True
787787
elif event.sym == tcod.event.KeySym.k and self.dy < SAMPLE_SCREEN_HEIGHT - 1:
788788
# destination move south
789-
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
789+
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
790790
self.dy += 1
791-
self.oldchar = sample_console.ch[self.dx, self.dy]
791+
self.old_char = sample_console.ch[self.dx, self.dy]
792792
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
793793
if SAMPLE_MAP[self.dx, self.dy] == " ":
794794
self.recalculate = True
795795
elif event.sym == tcod.event.KeySym.j and self.dx > 0:
796796
# destination move west
797-
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
797+
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
798798
self.dx -= 1
799-
self.oldchar = sample_console.ch[self.dx, self.dy]
799+
self.old_char = sample_console.ch[self.dx, self.dy]
800800
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
801801
if SAMPLE_MAP[self.dx, self.dy] == " ":
802802
self.recalculate = True
803803
elif event.sym == tcod.event.KeySym.l and self.dx < SAMPLE_SCREEN_WIDTH - 1:
804804
# destination move east
805-
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
805+
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
806806
self.dx += 1
807-
self.oldchar = sample_console.ch[self.dx, self.dy]
807+
self.old_char = sample_console.ch[self.dx, self.dy]
808808
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
809809
if SAMPLE_MAP[self.dx, self.dy] == " ":
810810
self.recalculate = True
@@ -822,10 +822,10 @@ def ev_mousemotion(self, event: tcod.event.MouseMotion) -> None:
822822
mx = event.tile.x - SAMPLE_SCREEN_X
823823
my = event.tile.y - SAMPLE_SCREEN_Y
824824
if 0 <= mx < SAMPLE_SCREEN_WIDTH and 0 <= my < SAMPLE_SCREEN_HEIGHT and (self.dx != mx or self.dy != my):
825-
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
825+
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
826826
self.dx = mx
827827
self.dy = my
828-
self.oldchar = sample_console.ch[self.dx, self.dy]
828+
self.old_char = sample_console.ch[self.dx, self.dy]
829829
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
830830
if SAMPLE_MAP[self.dx, self.dy] == " ":
831831
self.recalculate = True
@@ -1139,7 +1139,7 @@ class NameGeneratorSample(Sample):
11391139
def __init__(self) -> None:
11401140
self.name = "Name generator"
11411141

1142-
self.curset = 0
1142+
self.current_set = 0
11431143
self.delay = 0.0
11441144
self.names: list[str] = []
11451145
self.sets: list[str] = []
@@ -1159,7 +1159,7 @@ def on_draw(self) -> None:
11591159
sample_console.print(
11601160
1,
11611161
1,
1162-
"%s\n\n+ : next generator\n- : prev generator" % self.sets[self.curset],
1162+
"%s\n\n+ : next generator\n- : prev generator" % self.sets[self.current_set],
11631163
fg=WHITE,
11641164
bg=None,
11651165
)
@@ -1175,18 +1175,18 @@ def on_draw(self) -> None:
11751175
self.delay += frame_length[-1]
11761176
if self.delay > 0.5:
11771177
self.delay -= 0.5
1178-
self.names.append(libtcodpy.namegen_generate(self.sets[self.curset]))
1178+
self.names.append(libtcodpy.namegen_generate(self.sets[self.current_set]))
11791179

11801180
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
11811181
if event.sym == tcod.event.KeySym.EQUALS:
1182-
self.curset += 1
1182+
self.current_set += 1
11831183
self.names.append("======")
11841184
elif event.sym == tcod.event.KeySym.MINUS:
1185-
self.curset -= 1
1185+
self.current_set -= 1
11861186
self.names.append("======")
11871187
else:
11881188
super().ev_keydown(event)
1189-
self.curset %= len(self.sets)
1189+
self.current_set %= len(self.sets)
11901190

11911191

11921192
#############################################
@@ -1294,9 +1294,9 @@ def on_draw(self) -> None:
12941294
for v in range(RES_V - int_t, RES_V):
12951295
for u in range(RES_U):
12961296
tex_v = (v + int_abs_t) / float(RES_V)
1297-
texture[u, v] = tcod.noise_get_fbm(noise2d, [u / float(RES_U), tex_v], 32.0) + tcod.noise_get_fbm(
1298-
noise2d, [1 - u / float(RES_U), tex_v], 32.0
1299-
)
1297+
texture[u, v] = libtcodpy.noise_get_fbm(
1298+
noise2d, [u / float(RES_U), tex_v], 32.0
1299+
) + libtcodpy.noise_get_fbm(noise2d, [1 - u / float(RES_U), tex_v], 32.0)
13001300

13011301
# squared distance from center,
13021302
# clipped to sensible minimum and maximum values
@@ -1324,9 +1324,9 @@ def on_draw(self) -> None:
13241324
y = random.uniform(-0.5, 0.5)
13251325
strength = random.uniform(MIN_LIGHT_STRENGTH, 1.0)
13261326

1327-
color = tcod.Color(0, 0, 0) # create bright colors with random hue
1327+
color = libtcodpy.Color(0, 0, 0) # create bright colors with random hue
13281328
hue = random.uniform(0, 360)
1329-
tcod.color_set_hsv(color, hue, 0.5, strength)
1329+
libtcodpy.color_set_hsv(color, hue, 0.5, strength)
13301330
self.lights.append(Light(x, y, TEX_STRETCH, color.r, color.g, color.b, strength))
13311331

13321332
# eliminate lights that are going to be out of view

tcod/_internal.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import warnings
88
from pathlib import Path
99
from types import TracebackType
10-
from typing import Any, AnyStr, Callable, NoReturn, SupportsInt, TypeVar, cast
10+
from typing import TYPE_CHECKING, Any, AnyStr, Callable, NoReturn, SupportsInt, TypeVar, cast
1111

1212
import numpy as np
1313
from numpy.typing import ArrayLike, NDArray
1414
from typing_extensions import Literal
1515

1616
from tcod.cffi import ffi, lib
1717

18+
if TYPE_CHECKING:
19+
import tcod.image
20+
1821
FuncType = Callable[..., Any]
1922
F = TypeVar("F", bound=FuncType)
2023
T = TypeVar("T")
@@ -198,18 +201,17 @@ def _get_cdata_from_args(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
198201
def __hash__(self) -> int:
199202
return hash(self.cdata)
200203

201-
def __eq__(self, other: Any) -> Any:
202-
try:
203-
return self.cdata == other.cdata
204-
except AttributeError:
204+
def __eq__(self, other: object) -> bool:
205+
if not isinstance(other, _CDataWrapper):
205206
return NotImplemented
207+
return bool(self.cdata == other.cdata)
206208

207-
def __getattr__(self, attr: str) -> Any:
209+
def __getattr__(self, attr: str) -> Any: # noqa: ANN401
208210
if "cdata" in self.__dict__:
209211
return getattr(self.__dict__["cdata"], attr)
210212
raise AttributeError(attr)
211213

212-
def __setattr__(self, attr: str, value: Any) -> None:
214+
def __setattr__(self, attr: str, value: Any) -> None: # noqa: ANN401
213215
if hasattr(self, "cdata") and hasattr(self.cdata, attr):
214216
setattr(self.cdata, attr, value)
215217
else:
@@ -240,7 +242,7 @@ def __init__(self, array: ArrayLike) -> None:
240242
"""Initialize an image from the given array. May copy or reference the array."""
241243
self._array: NDArray[np.uint8] = np.ascontiguousarray(array, dtype=np.uint8)
242244
height, width, depth = self._array.shape
243-
if depth != 3:
245+
if depth != 3: # noqa: PLR2004
244246
msg = f"Array must have RGB channels. Shape is: {self._array.shape!r}"
245247
raise TypeError(msg)
246248
self._buffer = ffi.from_buffer("TCOD_color_t[]", self._array)
@@ -265,7 +267,7 @@ def __init__(self, array: ArrayLike) -> None:
265267
)
266268

267269

268-
def _as_image(image: Any) -> TempImage:
270+
def _as_image(image: ArrayLike | tcod.image.Image) -> TempImage | tcod.image.Image:
269271
"""Convert this input into an Image-like object."""
270272
if hasattr(image, "image_c"):
271273
return image # type: ignore

tcod/bsp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def h(self) -> int: # noqa: D102
8888
def h(self, value: int) -> None:
8989
self.height = value
9090

91-
def _as_cdata(self) -> Any:
91+
def _as_cdata(self) -> Any: # noqa: ANN401
9292
cdata = ffi.gc(
9393
lib.TCOD_bsp_new_with_size(self.x, self.y, self.width, self.height),
9494
lib.TCOD_bsp_delete,
@@ -115,7 +115,7 @@ def __repr__(self) -> str:
115115
status,
116116
)
117117

118-
def _unpack_bsp_tree(self, cdata: Any) -> None:
118+
def _unpack_bsp_tree(self, cdata: Any) -> None: # noqa: ANN401
119119
self.x = cdata.x
120120
self.y = cdata.y
121121
self.width = cdata.w

0 commit comments

Comments
 (0)