Skip to content

Commit 1613480

Browse files
committed
Fix or suppress all Ruff warnings in test scripts.
1 parent 49ddf4c commit 1613480

File tree

11 files changed

+103
-94
lines changed

11 files changed

+103
-94
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ select = [
174174
ignore = [
175175
"E501", # line-too-long
176176
"S101", # assert
177+
"S301", # suspicious-pickle-usage
178+
"S311", # suspicious-non-cryptographic-random-usage
177179
"ANN101", # missing-type-self
178180
"ANN102", # missing-type-cls
179181
"D203", # one-blank-line-before-class

tests/conftest.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
"""Test directory configuration."""
12
import random
23
import warnings
3-
from typing import Any, Callable, Iterator, Union
4+
from typing import Callable, Iterator, Union
45

56
import pytest
67

78
import tcod
89

10+
# ruff: noqa: D103
911

10-
def pytest_addoption(parser: Any) -> None:
12+
13+
def pytest_addoption(parser: pytest.Parser) -> None:
1114
parser.addoption("--no-window", action="store_true", help="Skip tests which need a rendering context.")
1215

1316

1417
@pytest.fixture(scope="session", params=["SDL", "SDL2"])
15-
def session_console(request: Any) -> Iterator[tcod.console.Console]:
18+
def session_console(request: pytest.FixtureRequest) -> Iterator[tcod.console.Console]:
1619
if request.config.getoption("--no-window"):
1720
pytest.skip("This test needs a rendering context.")
1821
FONT_FILE = "libtcod/terminal.png"
@@ -27,7 +30,7 @@ def session_console(request: Any) -> Iterator[tcod.console.Console]:
2730
yield con
2831

2932

30-
@pytest.fixture(scope="function")
33+
@pytest.fixture()
3134
def console(session_console: tcod.console.Console) -> tcod.console.Console:
3235
console = session_console
3336
tcod.console_flush()
@@ -81,6 +84,6 @@ def ch_latin1_str() -> str:
8184
"latin1_str",
8285
]
8386
)
84-
def ch(request: Any) -> Callable[[], Union[int, str]]:
85-
"""Test with multiple types of ascii/latin1 characters"""
87+
def ch(request: pytest.FixtureRequest) -> Callable[[], Union[int, str]]:
88+
"""Test with multiple types of ascii/latin1 characters."""
8689
return globals()["ch_%s" % request.param]() # type: ignore

tests/test_console.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
"""Tests for tcod.console."""
12
import pickle
23
from pathlib import Path
34

45
import numpy as np
56
import pytest
6-
from numpy import array
77

88
import tcod
99

10+
# ruff: noqa: D103
11+
1012

1113
@pytest.mark.filterwarnings("ignore:Directly access a consoles")
1214
@pytest.mark.filterwarnings("ignore:This function may be deprecated in the fu")
@@ -95,7 +97,8 @@ def test_console_pickle_fortran() -> None:
9597

9698

9799
def test_console_repr() -> None:
98-
array # Needed for eval.
100+
from numpy import array # noqa: F401 # Used for eval
101+
99102
eval(repr(tcod.console.Console(10, 2)))
100103

101104

tests/test_libtcodpy.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
"""Tests for the libtcodpy API."""
2+
from pathlib import Path
13
from typing import Any, Callable, Iterator, List, Optional, Tuple, Union
24

3-
import numpy
45
import numpy as np
56
import pytest
67
from numpy.typing import NDArray
78

89
import tcod
910
import tcod as libtcodpy
1011

12+
# ruff: noqa: D103
13+
1114
pytestmark = [
1215
pytest.mark.filterwarnings("ignore::DeprecationWarning"),
1316
pytest.mark.filterwarnings("ignore::PendingDeprecationWarning"),
@@ -29,7 +32,7 @@ def test_credits(console: tcod.console.Console) -> None:
2932
libtcodpy.console_credits_reset()
3033

3134

32-
def assert_char(
35+
def assert_char( # noqa: PLR0913
3336
console: tcod.console.Console,
3437
x: int,
3538
y: int,
@@ -143,20 +146,20 @@ def test_console_blit(console: tcod.console.Console, offscreen: tcod.console.Con
143146

144147

145148
@pytest.mark.filterwarnings("ignore")
146-
def test_console_asc_read_write(console: tcod.console.Console, offscreen: tcod.console.Console, tmpdir: Any) -> None:
149+
def test_console_asc_read_write(console: tcod.console.Console, offscreen: tcod.console.Console, tmp_path: Path) -> None:
147150
libtcodpy.console_print(console, 0, 0, "test")
148151

149-
asc_file = tmpdir.join("test.asc").strpath
152+
asc_file = tmp_path / "test.asc"
150153
assert libtcodpy.console_save_asc(console, asc_file)
151154
assert libtcodpy.console_load_asc(offscreen, asc_file)
152155
assertConsolesEqual(console, offscreen)
153156

154157

155158
@pytest.mark.filterwarnings("ignore")
156-
def test_console_apf_read_write(console: tcod.console.Console, offscreen: tcod.console.Console, tmpdir: Any) -> None:
159+
def test_console_apf_read_write(console: tcod.console.Console, offscreen: tcod.console.Console, tmp_path: Path) -> None:
157160
libtcodpy.console_print(console, 0, 0, "test")
158161

159-
apf_file = tmpdir.join("test.apf").strpath
162+
apf_file = tmp_path / "test.apf"
160163
assert libtcodpy.console_save_apf(console, apf_file)
161164
assert libtcodpy.console_load_apf(offscreen, apf_file)
162165
assertConsolesEqual(console, offscreen)
@@ -176,14 +179,14 @@ def test_console_rexpaint_load_test_file(console: tcod.console.Console) -> None:
176179
@pytest.mark.filterwarnings("ignore")
177180
def test_console_rexpaint_save_load(
178181
console: tcod.console.Console,
179-
tmpdir: Any,
182+
tmp_path: Path,
180183
ch: int,
181184
fg: Tuple[int, int, int],
182185
bg: Tuple[int, int, int],
183186
) -> None:
184187
libtcodpy.console_print(console, 0, 0, "test")
185188
libtcodpy.console_put_char_ex(console, 1, 1, ch, fg, bg)
186-
xp_file = tmpdir.join("test.xp").strpath
189+
xp_file = tmp_path / "test.xp"
187190
assert libtcodpy.console_save_xp(console, xp_file, 1)
188191
xp_console = libtcodpy.console_from_xp(xp_file)
189192
assert xp_console
@@ -193,12 +196,12 @@ def test_console_rexpaint_save_load(
193196

194197

195198
@pytest.mark.filterwarnings("ignore")
196-
def test_console_rexpaint_list_save_load(console: tcod.console.Console, tmpdir: Any) -> None:
199+
def test_console_rexpaint_list_save_load(console: tcod.console.Console, tmp_path: Path) -> None:
197200
con1 = libtcodpy.console_new(8, 2)
198201
con2 = libtcodpy.console_new(8, 2)
199202
libtcodpy.console_print(con1, 0, 0, "hello")
200203
libtcodpy.console_print(con2, 0, 0, "world")
201-
xp_file = tmpdir.join("test.xp").strpath
204+
xp_file = tmp_path / "test.xp"
202205
assert libtcodpy.console_list_save_xp([con1, con2], xp_file, 1)
203206
loaded_consoles = libtcodpy.console_list_load_xp(xp_file)
204207
assert loaded_consoles
@@ -252,7 +255,7 @@ def test_console_fill(console: tcod.console.Console) -> None:
252255
def test_console_fill_numpy(console: tcod.console.Console) -> None:
253256
width = libtcodpy.console_get_width(console)
254257
height = libtcodpy.console_get_height(console)
255-
fill: NDArray[np.intc] = numpy.zeros((height, width), dtype=np.intc)
258+
fill: NDArray[np.intc] = np.zeros((height, width), dtype=np.intc)
256259
for y in range(height):
257260
fill[y, :] = y % 256
258261

@@ -261,9 +264,9 @@ def test_console_fill_numpy(console: tcod.console.Console) -> None:
261264
libtcodpy.console_fill_char(console, fill) # type: ignore
262265

263266
# verify fill
264-
bg: NDArray[np.intc] = numpy.zeros((height, width), dtype=numpy.intc)
265-
fg: NDArray[np.intc] = numpy.zeros((height, width), dtype=numpy.intc)
266-
ch: NDArray[np.intc] = numpy.zeros((height, width), dtype=numpy.intc)
267+
bg: NDArray[np.intc] = np.zeros((height, width), dtype=np.intc)
268+
fg: NDArray[np.intc] = np.zeros((height, width), dtype=np.intc)
269+
ch: NDArray[np.intc] = np.zeros((height, width), dtype=np.intc)
267270
for y in range(height):
268271
for x in range(width):
269272
bg[y, x] = libtcodpy.console_get_char_background(console, x, y)[0]
@@ -291,7 +294,7 @@ def test_console_buffer(console: tcod.console.Console) -> None:
291294
@pytest.mark.filterwarnings("ignore:Console array attributes perform better")
292295
def test_console_buffer_error(console: tcod.console.Console) -> None:
293296
buffer = libtcodpy.ConsoleBuffer(0, 0)
294-
with pytest.raises(ValueError):
297+
with pytest.raises(ValueError, match=r".*Destination console has an incorrect size."):
295298
buffer.blit(console)
296299

297300

@@ -322,8 +325,8 @@ def test_sys_time(console: tcod.console.Console) -> None:
322325

323326

324327
@pytest.mark.filterwarnings("ignore")
325-
def test_sys_screenshot(console: tcod.console.Console, tmpdir: Any) -> None:
326-
libtcodpy.sys_save_screenshot(tmpdir.join("test.png").strpath)
328+
def test_sys_screenshot(console: tcod.console.Console, tmp_path: Path) -> None:
329+
libtcodpy.sys_save_screenshot(tmp_path / "test.png")
327330

328331

329332
@pytest.mark.filterwarnings("ignore")
@@ -333,7 +336,7 @@ def test_sys_custom_render(console: tcod.console.Console) -> None:
333336

334337
escape = []
335338

336-
def sdl_callback(sdl_surface: Any) -> None:
339+
def sdl_callback(sdl_surface: object) -> None:
337340
escape.append(True)
338341

339342
libtcodpy.sys_register_SDL_renderer(sdl_callback)
@@ -342,7 +345,7 @@ def sdl_callback(sdl_surface: Any) -> None:
342345

343346

344347
@pytest.mark.filterwarnings("ignore")
345-
def test_image(console: tcod.console.Console, tmpdir: Any) -> None:
348+
def test_image(console: tcod.console.Console, tmp_path: Path) -> None:
346349
img = libtcodpy.image_new(16, 16)
347350
libtcodpy.image_clear(img, (0, 0, 0))
348351
libtcodpy.image_invert(img)
@@ -360,7 +363,7 @@ def test_image(console: tcod.console.Console, tmpdir: Any) -> None:
360363
libtcodpy.image_blit(img, console, 0, 0, libtcodpy.BKGND_SET, 1, 1, 0)
361364
libtcodpy.image_blit_rect(img, console, 0, 0, 16, 16, libtcodpy.BKGND_SET)
362365
libtcodpy.image_blit_2x(img, console, 0, 0)
363-
libtcodpy.image_save(img, tmpdir.join("test.png").strpath)
366+
libtcodpy.image_save(img, tmp_path / "test.png")
364367
libtcodpy.image_delete(img)
365368

366369
img = libtcodpy.image_from_console(console)
@@ -385,14 +388,12 @@ def test_clipboard(console: tcod.console.Console, sample: str) -> None:
385388
# arguments to test with and the results expected from these arguments
386389
LINE_ARGS = (-5, 0, 5, 10)
387390
EXCLUSIVE_RESULTS = [(-4, 1), (-3, 2), (-2, 3), (-1, 4), (0, 5), (1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
388-
INCLUSIVE_RESULTS = [(-5, 0)] + EXCLUSIVE_RESULTS
391+
INCLUSIVE_RESULTS = [(-5, 0), *EXCLUSIVE_RESULTS]
389392

390393

391394
@pytest.mark.filterwarnings("ignore")
392395
def test_line_step() -> None:
393-
"""
394-
libtcodpy.line_init and libtcodpy.line_step
395-
"""
396+
"""libtcodpy.line_init and libtcodpy.line_step."""
396397
libtcodpy.line_init(*LINE_ARGS)
397398
for expected_xy in EXCLUSIVE_RESULTS:
398399
assert libtcodpy.line_step() == expected_xy
@@ -401,9 +402,7 @@ def test_line_step() -> None:
401402

402403
@pytest.mark.filterwarnings("ignore")
403404
def test_line() -> None:
404-
"""
405-
tests normal use, lazy evaluation, and error propagation
406-
"""
405+
"""Tests normal use, lazy evaluation, and error propagation."""
407406
# test normal results
408407
test_result: List[Tuple[int, int]] = []
409408

@@ -427,17 +426,13 @@ def return_false(x: int, y: int) -> bool:
427426

428427
@pytest.mark.filterwarnings("ignore")
429428
def test_line_iter() -> None:
430-
"""
431-
libtcodpy.line_iter
432-
"""
429+
"""libtcodpy.line_iter."""
433430
assert list(libtcodpy.line_iter(*LINE_ARGS)) == INCLUSIVE_RESULTS
434431

435432

436433
@pytest.mark.filterwarnings("ignore")
437434
def test_bsp() -> None:
438-
"""
439-
commented out statements work in libtcod-cffi
440-
"""
435+
"""Commented out statements work in libtcod-cffi."""
441436
bsp = libtcodpy.bsp_new_with_size(0, 0, 64, 64)
442437
repr(bsp) # test __repr__ on leaf
443438
libtcodpy.bsp_resize(bsp, 0, 0, 32, 32)
@@ -479,7 +474,7 @@ def test_bsp() -> None:
479474
libtcodpy.bsp_split_recursive(bsp, None, 4, 2, 2, 1.0, 1.0)
480475

481476
# cover bsp_traverse
482-
def traverse(node: tcod.bsp.BSP, user_data: Any) -> None:
477+
def traverse(node: tcod.bsp.BSP, user_data: object) -> None:
483478
return None
484479

485480
libtcodpy.bsp_traverse_pre_order(bsp, traverse)
@@ -498,9 +493,10 @@ def traverse(node: tcod.bsp.BSP, user_data: Any) -> None:
498493

499494
@pytest.mark.filterwarnings("ignore")
500495
def test_map() -> None:
501-
map = libtcodpy.map_new(16, 16)
502-
assert libtcodpy.map_get_width(map) == 16
503-
assert libtcodpy.map_get_height(map) == 16
496+
WIDTH, HEIGHT = 13, 17
497+
map = libtcodpy.map_new(WIDTH, HEIGHT)
498+
assert libtcodpy.map_get_width(map) == WIDTH
499+
assert libtcodpy.map_get_height(map) == HEIGHT
504500
libtcodpy.map_copy(map, map)
505501
libtcodpy.map_clear(map)
506502
libtcodpy.map_set_properties(map, 0, 0, True, True)

tests/test_noise.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Tests for the tcod.noise module."""
12
import copy
23
import pickle
34

@@ -6,6 +7,8 @@
67

78
import tcod
89

10+
# ruff: noqa: D103
11+
912

1013
@pytest.mark.parametrize("implementation", tcod.noise.Implementation)
1114
@pytest.mark.parametrize("algorithm", tcod.noise.Algorithm)
@@ -28,7 +31,7 @@ def test_noise_class(
2831
octaves=octaves,
2932
)
3033
# cover attributes
31-
assert noise.dimensions == 2
34+
assert noise.dimensions == 2 # noqa: PLR2004
3235
noise.algorithm = noise.algorithm
3336
noise.implementation = noise.implementation
3437
noise.octaves = noise.octaves
@@ -57,14 +60,14 @@ def test_noise_samples() -> None:
5760

5861

5962
def test_noise_errors() -> None:
60-
with pytest.raises(ValueError):
63+
with pytest.raises(ValueError, match=r"dimensions must be in range"):
6164
tcod.noise.Noise(0)
62-
with pytest.raises(ValueError):
65+
with pytest.raises(ValueError, match=r"-1 is not a valid implementation"):
6366
tcod.noise.Noise(1, implementation=-1)
6467
noise = tcod.noise.Noise(2)
65-
with pytest.raises(ValueError):
68+
with pytest.raises(ValueError, match=r"mgrid.shape\[0\] must equal self.dimensions"):
6669
noise.sample_mgrid(np.mgrid[:2, :2, :2])
67-
with pytest.raises(ValueError):
70+
with pytest.raises(ValueError, match=r"len\(ogrid\) must equal self.dimensions"):
6871
noise.sample_ogrid(np.ogrid[:2, :2, :2])
6972
with pytest.raises(IndexError):
7073
noise[0, 0, 0, 0, 0]

0 commit comments

Comments
 (0)