Skip to content

Commit 018e090

Browse files
committed
Clean up type errors, use standard deprecation functions
Fix minor typos Improve deprecation docs Set lower bound for typing_extensions
1 parent b033bf2 commit 018e090

File tree

7 files changed

+137
-102
lines changed

7 files changed

+137
-102
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ license = { text = "Simplified BSD License" }
2323
dependencies = [
2424
"cffi>=1.15",
2525
'numpy>=1.21.4; implementation_name != "pypy"',
26-
"typing_extensions",
26+
"typing_extensions>=4.12.2",
2727
]
2828
keywords = [
2929
"roguelike",

tcod/_internal.py

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

33
from __future__ import annotations
44

5-
import functools
65
import locale
76
import sys
87
import warnings
98
from pathlib import Path
109
from types import TracebackType
11-
from typing import TYPE_CHECKING, Any, AnyStr, Callable, NoReturn, SupportsInt, TypeVar, cast
10+
from typing import TYPE_CHECKING, Any, AnyStr, Callable, NoReturn, SupportsInt, TypeVar
1211

1312
import numpy as np
1413
from numpy.typing import ArrayLike, NDArray
15-
from typing_extensions import Literal
14+
from typing_extensions import Literal, LiteralString, deprecated
1615

1716
from tcod.cffi import ffi, lib
1817

@@ -24,31 +23,28 @@
2423
T = TypeVar("T")
2524

2625

27-
def deprecate(message: str, category: type[Warning] = DeprecationWarning, stacklevel: int = 0) -> Callable[[F], F]:
28-
"""Return a decorator which adds a warning to functions."""
26+
def _deprecate_passthrough(
27+
message: str, /, *, category: type[Warning] = DeprecationWarning, stacklevel: int = 0
28+
) -> Callable[[F], F]:
29+
"""Return a decorator which skips wrapping a warning onto functions. This is used for non-debug runs."""
2930

3031
def decorator(func: F) -> F:
31-
if not __debug__:
32-
return func
32+
return func
3333

34-
@functools.wraps(func)
35-
def wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
36-
warnings.warn(message, category, stacklevel=stacklevel + 2)
37-
return func(*args, **kwargs)
34+
return decorator
3835

39-
return cast(F, wrapper)
4036

41-
return decorator
37+
deprecate = deprecated if __debug__ or TYPE_CHECKING else _deprecate_passthrough
4238

4339

4440
def pending_deprecate(
45-
message: str = "This function may be deprecated in the future."
41+
message: LiteralString = "This function may be deprecated in the future."
4642
" Consider raising an issue on GitHub if you need this feature.",
4743
category: type[Warning] = PendingDeprecationWarning,
4844
stacklevel: int = 0,
4945
) -> Callable[[F], F]:
5046
"""Like deprecate, but the default parameters are filled out for a generic pending deprecation warning."""
51-
return deprecate(message, category, stacklevel)
47+
return deprecate(message, category=category, stacklevel=stacklevel)
5248

5349

5450
def verify_order(order: Literal["C", "F"]) -> Literal["C", "F"]:
@@ -119,7 +115,7 @@ def _unicode(string: AnyStr, stacklevel: int = 2) -> str:
119115
stacklevel=stacklevel + 1,
120116
)
121117
return string.decode("latin-1")
122-
return string
118+
return str(string)
123119

124120

125121
def _fmt(string: str, stacklevel: int = 2) -> bytes:

tcod/cffi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_sdl_version() -> str:
5959

6060

6161
verify_dependencies()
62-
from tcod._libtcod import ffi, lib # noqa
62+
from tcod._libtcod import ffi, lib # noqa: E402
6363

6464
__sdl_version__ = get_sdl_version()
6565

tcod/color.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def r(self) -> int:
3131
return int(self[0])
3232

3333
@r.setter
34-
@deprecate("Setting color attributes has been deprecated.", FutureWarning)
34+
@deprecate("Setting color attributes has been deprecated.", category=FutureWarning)
3535
def r(self, value: int) -> None:
3636
self[0] = value & 0xFF
3737

@@ -45,7 +45,7 @@ def g(self) -> int:
4545
return int(self[1])
4646

4747
@g.setter
48-
@deprecate("Setting color attributes has been deprecated.", FutureWarning)
48+
@deprecate("Setting color attributes has been deprecated.", category=FutureWarning)
4949
def g(self, value: int) -> None:
5050
self[1] = value & 0xFF
5151

@@ -59,7 +59,7 @@ def b(self) -> int:
5959
return int(self[2])
6060

6161
@b.setter
62-
@deprecate("Setting color attributes has been deprecated.", FutureWarning)
62+
@deprecate("Setting color attributes has been deprecated.", category=FutureWarning)
6363
def b(self, value: int) -> None:
6464
self[2] = value & 0xFF
6565

@@ -82,7 +82,7 @@ def __getitem__(self, index: Any) -> Any: # noqa: ANN401
8282
return super().__getitem__("rgb".index(index))
8383
return super().__getitem__(index)
8484

85-
@deprecate("This class will not be mutable in the future.", FutureWarning)
85+
@deprecate("This class will not be mutable in the future.", category=FutureWarning)
8686
def __setitem__(self, index: Any, value: Any) -> None: # noqa: ANN401, D105
8787
if isinstance(index, str):
8888
super().__setitem__("rgb".index(index), value)
@@ -99,7 +99,7 @@ def __eq__(self, other: object) -> bool:
9999
except TypeError:
100100
return False
101101

102-
@deprecate("Use NumPy instead for color math operations.", FutureWarning)
102+
@deprecate("Use NumPy instead for color math operations.", category=FutureWarning)
103103
def __add__(self, other: object) -> Color: # type: ignore[override]
104104
"""Add two colors together.
105105
@@ -108,7 +108,7 @@ def __add__(self, other: object) -> Color: # type: ignore[override]
108108
"""
109109
return Color._new_from_cdata(lib.TCOD_color_add(self, other))
110110

111-
@deprecate("Use NumPy instead for color math operations.", FutureWarning)
111+
@deprecate("Use NumPy instead for color math operations.", category=FutureWarning)
112112
def __sub__(self, other: object) -> Color:
113113
"""Subtract one color from another.
114114
@@ -117,7 +117,7 @@ def __sub__(self, other: object) -> Color:
117117
"""
118118
return Color._new_from_cdata(lib.TCOD_color_subtract(self, other))
119119

120-
@deprecate("Use NumPy instead for color math operations.", FutureWarning)
120+
@deprecate("Use NumPy instead for color math operations.", category=FutureWarning)
121121
def __mul__(self, other: object) -> Color:
122122
"""Multiply with a scaler or another color.
123123

tcod/console.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def __init__(
122122
) -> None:
123123
"""Initialize the console."""
124124
self._key_color: tuple[int, int, int] | None = None
125-
self._order = tcod._internal.verify_order(order)
125+
self._order: Literal["C", "F"] = tcod._internal.verify_order(order)
126126
if buffer is not None:
127127
if self._order == "F":
128128
buffer = buffer.transpose()
@@ -345,53 +345,90 @@ def rgb(self) -> NDArray[Any]:
345345
"""
346346
return self.rgba.view(self._DTYPE_RGB)
347347

348+
_DEPRECATE_CONSOLE_DEFAULTS_MSG = """Console defaults have been deprecated.
349+
Consider one of the following:
350+
351+
# Set parameters once then pass them as kwargs
352+
DEFAULT_COLOR = {"bg": (0, 0, 127), "fg": (127, 127, 255)}
353+
console.print(x, y, string, **DEFAULT_COLOR)
354+
355+
# Clear the console to a color and then skip setting colors on printing/drawing
356+
console.clear(fg=(127, 127, 255), bg=(0, 0, 127))
357+
console.print(x, y, string, fg=None)
358+
"""
359+
348360
@property
361+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
349362
def default_bg(self) -> tuple[int, int, int]:
350-
"""Tuple[int, int, int]: The default background color."""
363+
"""Tuple[int, int, int]: The default background color.
364+
365+
.. deprecated:: 8.5
366+
These should not be used. Prefer passing defaults as kwargs.
367+
368+
.. code-block::
369+
370+
DEFAULT_COLOR = {"bg": (0, 0, 127), "fg": (127, 127, 255)}
371+
console.print(x, y, string, **DEFAULT_COLOR)
372+
"""
351373
color = self._console_data.back
352374
return color.r, color.g, color.b
353375

354376
@default_bg.setter
355-
@deprecated("Console defaults have been deprecated.", category=FutureWarning)
377+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
356378
def default_bg(self, color: tuple[int, int, int]) -> None:
357379
self._console_data.back = color
358380

359381
@property
382+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
360383
def default_fg(self) -> tuple[int, int, int]:
361-
"""Tuple[int, int, int]: The default foreground color."""
384+
"""Tuple[int, int, int]: The default foreground color.
385+
386+
.. deprecated:: 8.5
387+
These should not be used. Prefer passing defaults as kwargs.
388+
"""
362389
color = self._console_data.fore
363390
return color.r, color.g, color.b
364391

365392
@default_fg.setter
366-
@deprecated("Console defaults have been deprecated.", category=FutureWarning)
393+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
367394
def default_fg(self, color: tuple[int, int, int]) -> None:
368395
self._console_data.fore = color
369396

370397
@property
398+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
371399
def default_bg_blend(self) -> int:
372-
"""int: The default blending mode."""
400+
"""int: The default blending mode.
401+
402+
.. deprecated:: 8.5
403+
These should not be used. Prefer passing defaults as kwargs.
404+
"""
373405
return self._console_data.bkgnd_flag # type: ignore
374406

375407
@default_bg_blend.setter
376-
@deprecated("Console defaults have been deprecated.", category=FutureWarning)
408+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
377409
def default_bg_blend(self, value: int) -> None:
378410
self._console_data.bkgnd_flag = value
379411

380412
@property
413+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
381414
def default_alignment(self) -> int:
382-
"""int: The default text alignment."""
415+
"""int: The default text alignment.
416+
417+
.. deprecated:: 8.5
418+
These should not be used. Prefer passing defaults as kwargs.
419+
"""
383420
return self._console_data.alignment # type: ignore
384421

385422
@default_alignment.setter
386-
@deprecated("Console defaults have been deprecated.", category=FutureWarning)
423+
@deprecated(_DEPRECATE_CONSOLE_DEFAULTS_MSG, category=FutureWarning)
387424
def default_alignment(self, value: int) -> None:
388425
self._console_data.alignment = value
389426

390427
def __clear_warning(self, name: str, value: tuple[int, int, int]) -> None:
391428
"""Raise a warning for bad default values during calls to clear."""
392429
warnings.warn(
393430
f"Clearing with the console default values is deprecated.\nAdd {name}={value!r} to this call.",
394-
DeprecationWarning,
431+
FutureWarning,
395432
stacklevel=3,
396433
)
397434

@@ -737,8 +774,8 @@ def print_frame( # noqa: PLR0913
737774
explicit.
738775
"""
739776
self.__deprecate_defaults("draw_frame", bg_blend)
740-
string = _fmt(string) if string else ffi.NULL
741-
_check(lib.TCOD_console_printf_frame(self.console_c, x, y, width, height, clear, bg_blend, string))
777+
string_: Any = _fmt(string) if string else ffi.NULL
778+
_check(lib.TCOD_console_printf_frame(self.console_c, x, y, width, height, clear, bg_blend, string_))
742779

743780
def blit( # noqa: PLR0913
744781
self,

0 commit comments

Comments
 (0)