Skip to content

Commit 1aaaf7c

Browse files
committed
Ensure deprecated colors still work.
1 parent 5a1d70c commit 1aaaf7c

5 files changed

Lines changed: 22 additions & 4 deletions

File tree

libtcodpy.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
"""This module just an alias for tcod"""
1+
"""Module alias for tcod."""
22
import warnings
33

4+
from tcod import * # noqa: F4
5+
from tcod.libtcodpy import __getattr__ # noqa: F401
6+
47
warnings.warn(
58
"'import tcod as libtcodpy' is preferred.",
69
DeprecationWarning,
710
stacklevel=2,
811
)
9-
from tcod import * # noqa: F4

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ filterwarnings = [
9999
"ignore::DeprecationWarning:tcod.libtcodpy",
100100
"ignore::PendingDeprecationWarning:tcod.libtcodpy",
101101
"ignore:This class may perform poorly and is no longer needed.::tcod.map",
102+
"ignore:'import tcod as libtcodpy' is preferred.",
102103
]
103104

104105
[tool.mypy]

tcod/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
__version__ = ""
2626

2727

28-
def __getattr__(name: str) -> color.Color:
28+
def __getattr__(name: str, stacklevel: int = 1) -> color.Color:
2929
"""Mark access to color constants as deprecated."""
3030
value: color.Color | None = getattr(constants, name, None)
3131
if value is None:
@@ -34,7 +34,7 @@ def __getattr__(name: str) -> color.Color:
3434
warnings.warn(
3535
f"Color constants will be removed from future releases.\nReplace `tcod.{name}` with `{tuple(value)}`.",
3636
FutureWarning,
37-
stacklevel=2,
37+
stacklevel=stacklevel + 1,
3838
)
3939
return value
4040

tcod/libtcodpy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,6 +4201,15 @@ def _atexit_verify() -> None:
42014201
lib.TCOD_console_delete(ffi.NULL)
42024202

42034203

4204+
def __getattr__(name: str) -> Color:
4205+
"""Mark access to color constants as deprecated."""
4206+
try:
4207+
return tcod.__getattr__(name, stacklevel=2) # type: ignore[call-arg]
4208+
except AttributeError:
4209+
msg = f"module {__name__!r} has no attribute {name!r}"
4210+
raise AttributeError(msg) from None
4211+
4212+
42044213
__all__ = [ # noqa: F405
42054214
"Color",
42064215
"Bsp",

tests/test_deprecated.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import pytest
55

6+
import libtcodpy
67
import tcod
78
import tcod.event
9+
import tcod.libtcodpy
810

911
# ruff: noqa: D103
1012

@@ -13,6 +15,10 @@
1315
def test_deprecate_color() -> None:
1416
with pytest.raises(FutureWarning, match=r".*\(0, 0, 0\)"):
1517
_ = tcod.black
18+
with pytest.raises(FutureWarning, match=r".*\(0, 0, 0\)"):
19+
_ = tcod.libtcodpy.black
20+
with pytest.raises(FutureWarning, match=r".*\(0, 0, 0\)"):
21+
_ = libtcodpy.black
1622

1723

1824
@pytest.mark.filterwarnings("error")

0 commit comments

Comments
 (0)