Skip to content

Commit d0d8c7b

Browse files
committed
Decorate already deprecated functions
Clean up deprecation warnings in tests
1 parent 250d0ad commit d0d8c7b

File tree

7 files changed

+63
-39
lines changed

7 files changed

+63
-39
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ addopts = [
9999
log_file_level = "DEBUG"
100100
faulthandler_timeout = 5
101101
filterwarnings = [
102-
"ignore::DeprecationWarning:tcod.libtcodpy",
103-
"ignore::PendingDeprecationWarning:tcod.libtcodpy",
102+
"ignore:This function may be deprecated in the future:PendingDeprecationWarning",
104103
"ignore:This class may perform poorly and is no longer needed.::tcod.map",
105104
"ignore:'import tcod as libtcodpy' is preferred.",
106105
]

tcod/console.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ def get_height_rect(self, x: int, y: int, width: int, height: int, string: str)
645645
string_ = string.encode("utf-8")
646646
return int(lib.TCOD_console_get_height_rect_n(self.console_c, x, y, width, height, len(string_), string_))
647647

648+
@deprecated("""Replace with 'console.draw_rect(x, y, width, height, ch=..., fg=..., bg=..., bg_blend=...)'""")
648649
def rect( # noqa: PLR0913
649650
self,
650651
x: int,
@@ -677,6 +678,9 @@ def rect( # noqa: PLR0913
677678
self.__deprecate_defaults("draw_rect", bg_blend, clear=bool(clear))
678679
lib.TCOD_console_rect(self.console_c, x, y, width, height, clear, bg_blend)
679680

681+
@deprecated(
682+
"""Replace with 'console.draw_rect(x, y, width=width, height=1, ch=ord("─"), fg=..., bg=..., bg_blend=...)'"""
683+
)
680684
def hline(
681685
self,
682686
x: int,
@@ -686,7 +690,7 @@ def hline(
686690
) -> None:
687691
"""Draw a horizontal line on the console.
688692
689-
This always uses ord('─'), the horizontal line character.
693+
This always uses ord("─"), the horizontal line character.
690694
691695
Args:
692696
x (int): The x coordinate from the left.
@@ -704,6 +708,9 @@ def hline(
704708
self.__deprecate_defaults("draw_rect", bg_blend)
705709
lib.TCOD_console_hline(self.console_c, x, y, width, bg_blend)
706710

711+
@deprecated(
712+
"""Replace with 'console.draw_rect(x, y, width=1, height=height, ch=ord("│"), fg=..., bg=..., bg_blend=...)'"""
713+
)
707714
def vline(
708715
self,
709716
x: int,
@@ -713,7 +720,7 @@ def vline(
713720
) -> None:
714721
"""Draw a vertical line on the console.
715722
716-
This always uses ord('│'), the vertical line character.
723+
This always uses ord("│"), the vertical line character.
717724
718725
Args:
719726
x (int): The x coordinate from the left.
@@ -1130,15 +1137,16 @@ def draw_frame( # noqa: PLR0913
11301137
11311138
Example::
11321139
1140+
>>> from tcod import libtcodpy
11331141
>>> console = tcod.console.Console(12, 6)
11341142
>>> console.draw_frame(x=0, y=0, width=3, height=3)
11351143
>>> console.draw_frame(x=3, y=0, width=3, height=3, decoration="╔═╗║ ║╚═╝")
11361144
>>> console.draw_frame(x=6, y=0, width=3, height=3, decoration="123456789")
11371145
>>> console.draw_frame(x=9, y=0, width=3, height=3, decoration="/-\\| |\\-/")
11381146
>>> console.draw_frame(x=0, y=3, width=12, height=3)
1139-
>>> console.print_box(x=0, y=3, width=12, height=1, string=" Title ", alignment=tcod.CENTER)
1147+
>>> console.print_box(x=0, y=3, width=12, height=1, string=" Title ", alignment=libtcodpy.CENTER)
11401148
1
1141-
>>> console.print_box(x=0, y=5, width=12, height=1, string="┤Lower├", alignment=tcod.CENTER)
1149+
>>> console.print_box(x=0, y=5, width=12, height=1, string="┤Lower├", alignment=libtcodpy.CENTER)
11421150
1
11431151
>>> print(console)
11441152
<┌─┐╔═╗123/-\

tcod/libtcodpy.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def __setattr__(self, attr: str, value: Any) -> None:
410410
def __repr__(self) -> str:
411411
"""Return a representation of this Key object."""
412412
params = []
413-
params.append(f"pressed={self.pressed!r}, vk=tcod.{_LOOKUP_VK[self.vk]}")
413+
params.append(f"pressed={self.pressed!r}, vk=libtcodpy.{_LOOKUP_VK[self.vk]}")
414414
if self.c:
415415
params.append(f"c=ord({chr(self.c)!r})")
416416
if self.text:
@@ -426,7 +426,7 @@ def __repr__(self) -> str:
426426
]:
427427
if getattr(self, attr):
428428
params.append(f"{attr}={getattr(self, attr)!r}")
429-
return "tcod.Key({})".format(", ".join(params))
429+
return "libtcodpy.Key({})".format(", ".join(params))
430430

431431
@property
432432
def key_p(self) -> Any:
@@ -504,7 +504,7 @@ def __repr__(self) -> str:
504504
]:
505505
if getattr(self, attr):
506506
params.append(f"{attr}={getattr(self, attr)!r}")
507-
return "tcod.Mouse({})".format(", ".join(params))
507+
return "libtcodpy.Mouse({})".format(", ".join(params))
508508

509509
@property
510510
def mouse_p(self) -> Any:
@@ -828,7 +828,8 @@ def color_gen_map(colors: Iterable[tuple[int, int, int]], indexes: Iterable[int]
828828
List[Color]: A list of Color instances.
829829
830830
Example:
831-
>>> tcod.color_gen_map([(0, 0, 0), (255, 128, 0)], [0, 5])
831+
>>> from tcod import libtcodpy
832+
>>> libtcodpy.color_gen_map([(0, 0, 0), (255, 128, 0)], [0, 5])
832833
[Color(0, 0, 0), Color(51, 25, 0), Color(102, 51, 0), \
833834
Color(153, 76, 0), Color(204, 102, 0), Color(255, 128, 0)]
834835
"""
@@ -2667,13 +2668,13 @@ def heightmap_kernel_transform(
26672668
26682669
Example:
26692670
>>> import numpy as np
2671+
>>> from tcod import libtcodpy
26702672
>>> heightmap = np.zeros((3, 3), dtype=np.float32)
26712673
>>> heightmap[:,1] = 1
26722674
>>> dx = [-1, 1, 0]
26732675
>>> dy = [0, 0, 0]
26742676
>>> weight = [0.33, 0.33, 0.33]
2675-
>>> tcod.heightmap_kernel_transform(heightmap, 3, dx, dy, weight,
2676-
... 0.0, 1.0)
2677+
>>> libtcodpy.heightmap_kernel_transform(heightmap, 3, dx, dy, weight, 0.0, 1.0)
26772678
"""
26782679
c_dx = ffi.new("int[]", dx)
26792680
c_dy = ffi.new("int[]", dy)

tests/conftest.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import random
44
import warnings
5-
from typing import Callable, Iterator, Union
5+
from typing import Any, Callable, Iterator, Union
66

77
import pytest
88

99
import tcod
10+
from tcod import libtcodpy
1011

1112
# ruff: noqa: D103
1213

@@ -15,6 +16,12 @@ def pytest_addoption(parser: pytest.Parser) -> None:
1516
parser.addoption("--no-window", action="store_true", help="Skip tests which need a rendering context.")
1617

1718

19+
@pytest.fixture(autouse=True)
20+
def add_tcod_to_namespace(doctest_namespace: dict[str, Any]) -> None:
21+
doctest_namespace["tcod"] = tcod
22+
doctest_namespace["libtcodpy"] = libtcodpy
23+
24+
1825
@pytest.fixture
1926
def uses_window(request: pytest.FixtureRequest) -> Iterator[None]:
2027
"""Marks tests which require a rendering context."""
@@ -33,23 +40,23 @@ def session_console(request: pytest.FixtureRequest) -> Iterator[tcod.console.Con
3340
HEIGHT = 10
3441
TITLE = "libtcod-cffi tests"
3542
FULLSCREEN = False
36-
RENDERER = getattr(tcod, "RENDERER_" + request.param)
43+
RENDERER = getattr(libtcodpy, "RENDERER_" + request.param)
3744

38-
tcod.console_set_custom_font(FONT_FILE)
39-
with tcod.console_init_root(WIDTH, HEIGHT, TITLE, FULLSCREEN, RENDERER, vsync=False) as con:
45+
libtcodpy.console_set_custom_font(FONT_FILE)
46+
with libtcodpy.console_init_root(WIDTH, HEIGHT, TITLE, FULLSCREEN, RENDERER, vsync=False) as con:
4047
yield con
4148

4249

4350
@pytest.fixture
4451
def console(session_console: tcod.console.Console) -> tcod.console.Console:
4552
console = session_console
46-
tcod.console_flush()
53+
libtcodpy.console_flush()
4754
with warnings.catch_warnings():
4855
warnings.simplefilter("ignore")
4956
console.default_fg = (255, 255, 255)
5057
console.default_bg = (0, 0, 0)
51-
console.default_bg_blend = tcod.BKGND_SET
52-
console.default_alignment = tcod.LEFT
58+
console.default_bg_blend = libtcodpy.BKGND_SET
59+
console.default_alignment = libtcodpy.LEFT
5360
console.clear()
5461
return console
5562

@@ -61,13 +68,13 @@ def offscreen(console: tcod.console.Console) -> tcod.console.Console:
6168

6269

6370
@pytest.fixture
64-
def fg() -> tcod.Color:
65-
return tcod.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
71+
def fg() -> libtcodpy.Color:
72+
return libtcodpy.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
6673

6774

6875
@pytest.fixture
69-
def bg() -> tcod.Color:
70-
return tcod.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
76+
def bg() -> libtcodpy.Color:
77+
return libtcodpy.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
7178

7279

7380
def ch_ascii_int() -> int:
@@ -96,4 +103,4 @@ def ch_latin1_str() -> str:
96103
)
97104
def ch(request: pytest.FixtureRequest) -> Callable[[], Union[int, str]]:
98105
"""Test with multiple types of ascii/latin1 characters."""
99-
return globals()["ch_%s" % request.param]() # type: ignore
106+
return globals()[f"ch_{request.param}"]() # type: ignore

tests/test_console.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ def test_console_methods() -> None:
7373
console.print_(0, 0, "Test")
7474
console.print_rect(0, 0, 2, 8, "a b c d e f")
7575
console.get_height_rect(0, 0, 2, 8, "a b c d e f")
76-
console.rect(0, 0, 2, 2, True)
77-
console.hline(0, 1, 10)
78-
console.vline(1, 0, 10)
76+
with pytest.deprecated_call():
77+
console.rect(0, 0, 2, 2, True)
78+
with pytest.deprecated_call():
79+
console.hline(0, 1, 10)
80+
with pytest.deprecated_call():
81+
console.vline(1, 0, 10)
7982
console.print_frame(0, 0, 8, 8, "Frame")
8083
console.blit(0, 0, 0, 0, console, 0, 0) # type: ignore
8184
console.blit(0, 0, 0, 0, console, 0, 0, key_color=(0, 0, 0)) # type: ignore
82-
console.set_key_color((254, 0, 254))
85+
with pytest.deprecated_call():
86+
console.set_key_color((254, 0, 254))
8387

8488

8589
def test_console_pickle() -> None:

tests/test_random.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pickle
55
from pathlib import Path
66

7+
import pytest
8+
79
import tcod.random
810

911
# ruff: noqa: D103
@@ -15,8 +17,10 @@ def test_tcod_random() -> None:
1517
rand = tcod.random.Random(tcod.random.COMPLEMENTARY_MULTIPLY_WITH_CARRY)
1618
assert 0 <= rand.randint(0, 100) <= 100 # noqa: PLR2004
1719
assert 0 <= rand.uniform(0, 100) <= 100 # noqa: PLR2004
18-
rand.guass(0, 1)
19-
rand.inverse_guass(0, 1)
20+
with pytest.warns(FutureWarning, match=r"typo"):
21+
rand.guass(0, 1)
22+
with pytest.warns(FutureWarning, match=r"typo"):
23+
rand.inverse_guass(0, 1)
2024

2125

2226
def test_tcod_random_copy() -> None:

tests/test_tcod.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import tcod
1212
import tcod.console
13+
from tcod import libtcodpy
1314

1415
# ruff: noqa: D103
1516

@@ -35,7 +36,7 @@ def test_tcod_bsp() -> None:
3536
assert not bsp.children
3637

3738
with pytest.raises(RuntimeError):
38-
tcod.bsp_traverse_pre_order(bsp, raise_Exception)
39+
libtcodpy.bsp_traverse_pre_order(bsp, raise_Exception)
3940

4041
bsp.split_recursive(3, 4, 4, 1, 1)
4142
for node in bsp.walk():
@@ -66,11 +67,11 @@ def test_tcod_map_set_bits() -> None:
6667
assert not map_.fov[:].any()
6768

6869
map_.transparent[1, 0] = True
69-
assert tcod.map_is_transparent(map_, 0, 1)
70+
assert libtcodpy.map_is_transparent(map_, 0, 1)
7071
map_.walkable[1, 0] = True
71-
assert tcod.map_is_walkable(map_, 0, 1)
72+
assert libtcodpy.map_is_walkable(map_, 0, 1)
7273
map_.fov[1, 0] = True
73-
assert tcod.map_is_in_fov(map_, 0, 1)
74+
assert libtcodpy.map_is_in_fov(map_, 0, 1)
7475

7576

7677
@pytest.mark.filterwarnings("ignore:This class may perform poorly")
@@ -115,7 +116,7 @@ def test_color_class() -> None:
115116
assert tcod.white - tcod.white == tcod.black
116117
assert tcod.black + (2, 2, 2) - (1, 1, 1) == (1, 1, 1) # noqa: RUF005
117118

118-
color = tcod.Color()
119+
color = libtcodpy.Color()
119120
color.r = 1
120121
color.g = 2
121122
color.b = 3
@@ -156,7 +157,7 @@ def test_path_callback() -> None:
156157

157158

158159
def test_key_repr() -> None:
159-
Key = tcod.Key
160+
Key = libtcodpy.Key
160161
key = Key(vk=1, c=2, shift=True)
161162
assert key.vk == 1
162163
assert key.c == 2 # noqa: PLR2004
@@ -168,7 +169,7 @@ def test_key_repr() -> None:
168169

169170

170171
def test_mouse_repr() -> None:
171-
Mouse = tcod.Mouse
172+
Mouse = libtcodpy.Mouse
172173
mouse = Mouse(x=1, lbutton=True)
173174
mouse_copy = eval(repr(mouse)) # noqa: S307
174175
assert mouse.x == mouse_copy.x
@@ -188,10 +189,10 @@ def test_recommended_size(console: tcod.console.Console) -> None:
188189

189190
@pytest.mark.filterwarnings("ignore")
190191
def test_context(uses_window: None) -> None:
191-
with tcod.context.new_window(32, 32, renderer=tcod.RENDERER_SDL2):
192+
with tcod.context.new_window(32, 32, renderer=libtcodpy.RENDERER_SDL2):
192193
pass
193194
WIDTH, HEIGHT = 16, 4
194-
with tcod.context.new_terminal(columns=WIDTH, rows=HEIGHT, renderer=tcod.RENDERER_SDL2) as context:
195+
with tcod.context.new_terminal(columns=WIDTH, rows=HEIGHT, renderer=libtcodpy.RENDERER_SDL2) as context:
195196
console = tcod.console.Console(*context.recommended_console_size())
196197
context.present(console)
197198
assert context.sdl_window_p is not None

0 commit comments

Comments
 (0)