Skip to content

Commit 27a3278

Browse files
committed
Deprecate support for Python 3.5.
Use typing_extensions to better support types on older versions of Python. This version actually supports 3.5 better than previous versions, which makes it a better point to drop support from.
1 parent fe6352b commit 27a3278

File tree

8 files changed

+49
-25
lines changed

8 files changed

+49
-25
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Unreleased
1111
Deprecated
1212
- `tcod.console_load_xp` has been deprecated, `tcod.console_from_xp` can load
1313
these files without modifying an existing console.
14+
- Support for Python 3.5 will be dropped soon.
1415

1516
Fixed
1617
- `tcod.console_from_xp` now has better error handling (instead of crashing.)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def check_sdl_version():
135135
install_requires=[
136136
"cffi~=1.13", # Also required by pyproject.toml.
137137
"numpy~=1.10" if not is_pypy else "",
138+
"typing_extensions",
138139
],
139140
cffi_modules=["build_libtcod.py:ffi"],
140141
setup_requires=pytest_runner,

tcod/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
Bring any issues or requests to GitHub:
1616
https://github.com/HexDecimal/libtcod-cffi
1717
"""
18+
import sys
19+
import warnings
20+
1821
from tcod import (
1922
bsp,
2023
color,
@@ -39,6 +42,13 @@
3942
except ImportError: # Gets imported without version.py by ReadTheDocs
4043
__version__ = ""
4144

45+
if sys.version_info < (3, 6):
46+
warnings.warn(
47+
"Support for Python 3.5 is being dropped from python-tcod.",
48+
DeprecationWarning,
49+
stacklevel=2,
50+
)
51+
4252
__all__ = [ # noqa: F405
4353
"__version__",
4454
"lib",

tcod/_internal.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"""
33
import functools
44
import warnings
5-
from typing import Any, AnyStr, Callable, NoReturn, TypeVar, cast
5+
from typing import Any, AnyStr, Callable, TypeVar, Union, cast
66

77
import numpy as np
8+
from typing_extensions import Literal, NoReturn
89

910
from tcod.loader import ffi, lib
1011

@@ -42,21 +43,15 @@ def pending_deprecate(
4243
return deprecate(message, category, stacklevel)
4344

4445

45-
def verify_order(order: str) -> str:
46-
order = order.upper()
46+
def verify_order(
47+
order: Union[Literal["C"], Literal["F"]]
48+
) -> Union[Literal["C"], Literal["F"]]:
49+
order = order.upper() # type: ignore
4750
if order not in ("C", "F"):
4851
raise TypeError("order must be 'C' or 'F', not %r" % (order,))
4952
return order
5053

5154

52-
def handle_order(shape: Any, order: str) -> Any:
53-
order = verify_order(order)
54-
if order == "C":
55-
return shape
56-
else:
57-
return tuple(reversed(shape))
58-
59-
6055
def _raise_tcod_error() -> NoReturn:
6156
"""Raise an error from libtcod, this function assumes an error exists."""
6257
raise RuntimeError(ffi.string(lib.TCOD_get_error()).decode("utf-8"))

tcod/console.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"""
66

77
import warnings
8-
from typing import Any, Optional, Tuple # noqa: F401
8+
from typing import Any, Optional, Tuple, Union # noqa: F401
99

1010
import numpy as np
11+
from typing_extensions import Literal
1112

1213
import tcod._internal
1314
import tcod.constants
@@ -92,7 +93,7 @@ def __init__(
9293
self,
9394
width: int,
9495
height: int,
95-
order: str = "C",
96+
order: Union[Literal["C"], Literal["F"]] = "C",
9697
buffer: Optional[np.ndarray] = None,
9798
):
9899
self._key_color = None # type: Optional[Tuple[int, int, int]]
@@ -131,7 +132,9 @@ def __init__(
131132
self.clear()
132133

133134
@classmethod
134-
def _from_cdata(cls, cdata: Any, order: str = "C") -> "Console":
135+
def _from_cdata(
136+
cls, cdata: Any, order: Union[Literal["C"], Literal["F"]] = "C"
137+
) -> "Console":
135138
"""Return a Console instance which wraps this `TCOD_Console*` object."""
136139
if isinstance(cdata, cls):
137140
return cdata
@@ -141,7 +144,9 @@ def _from_cdata(cls, cdata: Any, order: str = "C") -> "Console":
141144
return self
142145

143146
@classmethod
144-
def _get_root(cls, order: Optional[str] = None) -> "Console":
147+
def _get_root(
148+
cls, order: Optional[Union[Literal["C"], Literal["F"]]] = None
149+
) -> "Console":
145150
"""Return a root console singleton with valid buffers.
146151
147152
This function will also update an already active root console.
@@ -156,7 +161,9 @@ def _get_root(cls, order: Optional[str] = None) -> "Console":
156161
self._init_setup_console_data(self._order)
157162
return self
158163

159-
def _init_setup_console_data(self, order: str = "C") -> None:
164+
def _init_setup_console_data(
165+
self, order: Union[Literal["C"], Literal["F"]] = "C"
166+
) -> None:
160167
"""Setup numpy arrays over libtcod data buffers."""
161168
global _root_console
162169
self._key_color = None
@@ -195,7 +202,7 @@ def bg(self) -> np.ndarray:
195202
``console.bg[x, y, channel] # order='F'``.
196203
197204
"""
198-
bg = self._tiles["bg"][..., :3]
205+
bg = self._tiles["bg"][..., :3] # type: np.ndarray
199206
if self._order == "F":
200207
bg = bg.transpose(1, 0, 2)
201208
return bg
@@ -209,7 +216,7 @@ def fg(self) -> np.ndarray:
209216
Index this array with ``console.fg[i, j, channel] # order='C'`` or
210217
``console.fg[x, y, channel] # order='F'``.
211218
"""
212-
fg = self._tiles["fg"][..., :3]
219+
fg = self._tiles["fg"][..., :3] # type: np.ndarray
213220
if self._order == "F":
214221
fg = fg.transpose(1, 0, 2)
215222
return fg

tcod/libtcodpy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121

2222
import numpy as np
23+
from typing_extensions import Literal
2324

2425
import tcod.bsp
2526
import tcod.console
@@ -879,7 +880,7 @@ def console_init_root(
879880
title: Optional[str] = None,
880881
fullscreen: bool = False,
881882
renderer: Optional[int] = None,
882-
order: str = "C",
883+
order: Union[Literal["C"], Literal["F"]] = "C",
883884
vsync: Optional[bool] = None,
884885
) -> tcod.console.Console:
885886
"""Set up the primary display and return the root console.

tcod/map.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
44
"""
55
import warnings
6-
from typing import Any, Tuple
6+
from typing import Any, Tuple, Union
77

88
import numpy as np
9+
from typing_extensions import Literal
910

1011
import tcod._internal
1112
import tcod.constants
@@ -68,7 +69,12 @@ class Map(object):
6869
See :any:`tcod.map.compute_fov` and :any:`tcod.path`.
6970
"""
7071

71-
def __init__(self, width: int, height: int, order: str = "C"):
72+
def __init__(
73+
self,
74+
width: int,
75+
height: int,
76+
order: Union[Literal["C"], Literal["F"]] = "C",
77+
):
7278
warnings.warn(
7379
"This class may perform poorly and is no longer needed.",
7480
DeprecationWarning,
@@ -94,17 +100,17 @@ def __as_cdata(self) -> Any:
94100

95101
@property
96102
def transparent(self) -> np.ndarray:
97-
buffer = self.__buffer[:, :, 0]
103+
buffer = self.__buffer[:, :, 0] # type: np.ndarray
98104
return buffer.T if self._order == "F" else buffer
99105

100106
@property
101107
def walkable(self) -> np.ndarray:
102-
buffer = self.__buffer[:, :, 1]
108+
buffer = self.__buffer[:, :, 1] # type: np.ndarray
103109
return buffer.T if self._order == "F" else buffer
104110

105111
@property
106112
def fov(self) -> np.ndarray:
107-
buffer = self.__buffer[:, :, 2]
113+
buffer = self.__buffer[:, :, 2] # type: np.ndarray
108114
return buffer.T if self._order == "F" else buffer
109115

110116
def compute_fov(

tcod/path.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
2121

2222
import numpy as np
23+
from typing_extensions import Literal
2324

2425
import tcod.map # noqa: F401
2526
from tcod._internal import _check
@@ -301,7 +302,9 @@ def get_path(self, x: int, y: int) -> List[Tuple[int, int]]:
301302

302303

303304
def maxarray(
304-
shape: Tuple[int, ...], dtype: Any = np.int32, order: str = "C"
305+
shape: Tuple[int, ...],
306+
dtype: Any = np.int32,
307+
order: Union[Literal["C"], Literal["F"]] = "C",
305308
) -> np.ndarray:
306309
"""Return a new array filled with the maximum finite value for `dtype`.
307310

0 commit comments

Comments
 (0)