33To render a console you need a tileset and a window to render to.
44See :ref:`getting-started` for info on how to set those up.
55"""
6+ from __future__ import annotations
7+
68import os
79import warnings
810from pathlib import Path
911from typing import Any , Iterable , Optional , Sequence , Tuple , Union
1012
1113import numpy as np
14+ from numpy .typing import NDArray
1215from typing_extensions import Literal
1316
1417import tcod ._internal
@@ -114,9 +117,9 @@ def __init__(
114117 width : int ,
115118 height : int ,
116119 order : Literal ["C" , "F" ] = "C" ,
117- buffer : " Optional[np.ndarray [Any, Any]]" = None ,
120+ buffer : Optional [NDArray [Any ]] = None ,
118121 ):
119- self ._key_color = None # type : Optional[Tuple[int, int, int]]
122+ self ._key_color : Optional [Tuple [int , int , int ]] = None
120123 self ._order = tcod ._internal .verify_order (order )
121124 if buffer is not None :
122125 if self ._order == "F" :
@@ -150,25 +153,25 @@ def __init__(
150153 self .clear ()
151154
152155 @classmethod
153- def _from_cdata (cls , cdata : Any , order : Literal ["C" , "F" ] = "C" ) -> " Console" :
156+ def _from_cdata (cls , cdata : Any , order : Literal ["C" , "F" ] = "C" ) -> Console :
154157 """Return a Console instance which wraps this `TCOD_Console*` object."""
155158 if isinstance (cdata , cls ):
156159 return cdata
157- self = object .__new__ (cls ) # type: Console
160+ self : Console = object .__new__ (cls )
158161 self .console_c = cdata
159162 self ._init_setup_console_data (order )
160163 return self
161164
162165 @classmethod
163- def _get_root (cls , order : Optional [Literal ["C" , "F" ]] = None ) -> " Console" :
166+ def _get_root (cls , order : Optional [Literal ["C" , "F" ]] = None ) -> Console :
164167 """Return a root console singleton with valid buffers.
165168
166169 This function will also update an already active root console.
167170 """
168171 global _root_console
169172 if _root_console is None :
170173 _root_console = object .__new__ (cls )
171- self = _root_console # type: Console
174+ self : Console = _root_console
172175 if order is not None :
173176 self ._order = order
174177 self .console_c = ffi .NULL
@@ -185,7 +188,7 @@ def _init_setup_console_data(self, order: Literal["C", "F"] = "C") -> None:
185188 else :
186189 self ._console_data = ffi .cast ("struct TCOD_Console*" , self .console_c )
187190
188- self ._tiles = np .frombuffer ( # type: ignore
191+ self ._tiles : NDArray [ Any ] = np .frombuffer ( # type: ignore
189192 ffi .buffer (self ._console_data .tiles [0 : self .width * self .height ]),
190193 dtype = self .DTYPE ,
191194 ).reshape ((self .height , self .width ))
@@ -203,7 +206,7 @@ def height(self) -> int:
203206 return lib .TCOD_console_get_height (self .console_c ) # type: ignore
204207
205208 @property
206- def bg (self ) -> "np.ndarray[Any, np.dtype[np. uint8]]" :
209+ def bg (self ) -> NDArray [ np .uint8 ]:
207210 """A uint8 array with the shape (height, width, 3).
208211
209212 You can change the consoles background colors by using this array.
@@ -218,7 +221,7 @@ def bg(self) -> "np.ndarray[Any, np.dtype[np.uint8]]":
218221 return bg
219222
220223 @property
221- def fg (self ) -> "np.ndarray[Any, np.dtype[np. uint8]]" :
224+ def fg (self ) -> NDArray [ np .uint8 ]:
222225 """A uint8 array with the shape (height, width, 3).
223226
224227 You can change the consoles foreground colors by using this array.
@@ -232,7 +235,7 @@ def fg(self) -> "np.ndarray[Any, np.dtype[np.uint8]]":
232235 return fg
233236
234237 @property
235- def ch (self ) -> "np.ndarray[Any, np.dtype[np. intc]]" :
238+ def ch (self ) -> NDArray [ np .intc ]:
236239 """An integer array with the shape (height, width).
237240
238241 You can change the consoles character codes by using this array.
@@ -244,7 +247,7 @@ def ch(self) -> "np.ndarray[Any, np.dtype[np.intc]]":
244247
245248 @property # type: ignore
246249 @deprecate ("This attribute has been renamed to `rgba`." )
247- def tiles (self ) -> "np.ndarray [Any, Any]" :
250+ def tiles (self ) -> NDArray [Any ] :
248251 """An array of this consoles raw tile data.
249252
250253 This acts as a combination of the `ch`, `fg`, and `bg` attributes.
@@ -260,7 +263,7 @@ def tiles(self) -> "np.ndarray[Any, Any]":
260263
261264 @property # type: ignore
262265 @deprecate ("This attribute has been renamed to `rgba`." )
263- def buffer (self ) -> "np.ndarray [Any, Any]" :
266+ def buffer (self ) -> NDArray [Any ] :
264267 """An array of this consoles raw tile data.
265268
266269 .. versionadded:: 11.4
@@ -272,7 +275,7 @@ def buffer(self) -> "np.ndarray[Any, Any]":
272275
273276 @property # type: ignore
274277 @deprecate ("This attribute has been renamed to `rgb`." )
275- def tiles_rgb (self ) -> "np.ndarray [Any, Any]" :
278+ def tiles_rgb (self ) -> NDArray [Any ] :
276279 """An array of this consoles data without the alpha channel.
277280
278281 .. versionadded:: 11.8
@@ -284,7 +287,7 @@ def tiles_rgb(self) -> "np.ndarray[Any, Any]":
284287
285288 @property # type: ignore
286289 @deprecate ("This attribute has been renamed to `rgb`." )
287- def tiles2 (self ) -> "np.ndarray [Any, Any]" :
290+ def tiles2 (self ) -> NDArray [Any ] :
288291 """This name is deprecated in favour of :any:`rgb`.
289292
290293 .. versionadded:: 11.3
@@ -295,7 +298,7 @@ def tiles2(self) -> "np.ndarray[Any, Any]":
295298 return self .rgb
296299
297300 @property
298- def rgba (self ) -> "np.ndarray [Any, Any]" :
301+ def rgba (self ) -> NDArray [Any ] :
299302 """An array of this consoles raw tile data.
300303
301304 The axes of this array is affected by the `order` parameter given to
@@ -316,7 +319,7 @@ def rgba(self) -> "np.ndarray[Any, Any]":
316319 return self ._tiles .T if self ._order == "F" else self ._tiles
317320
318321 @property
319- def rgb (self ) -> "np.ndarray [Any, Any]" :
322+ def rgb (self ) -> NDArray [Any ] :
320323 """An array of this consoles data without the alpha channel.
321324
322325 The axes of this array is affected by the `order` parameter given to
@@ -468,8 +471,8 @@ def __deprecate_defaults(
468471 if not __debug__ :
469472 return
470473
471- fg = self .default_fg # type: Any
472- bg = self .default_bg # type: Any
474+ fg : Optional [ Tuple [ int , int , int ]] = self .default_fg
475+ bg : Optional [ Tuple [ int , int , int ]] = self .default_bg
473476 if bg_blend == tcod .constants .BKGND_NONE :
474477 bg = None
475478 if bg_blend == tcod .constants .BKGND_DEFAULT :
@@ -737,7 +740,7 @@ def print_frame(
737740
738741 def blit (
739742 self ,
740- dest : " Console" ,
743+ dest : Console ,
741744 dest_x : int = 0 ,
742745 dest_y : int = 0 ,
743746 src_x : int = 0 ,
@@ -837,7 +840,7 @@ def set_key_color(self, color: Optional[Tuple[int, int, int]]) -> None:
837840 """
838841 self ._key_color = color
839842
840- def __enter__ (self ) -> " Console" :
843+ def __enter__ (self ) -> Console :
841844 """Returns this console in a managed context.
842845
843846 When the root console is used as a context, the graphical window will
@@ -898,7 +901,7 @@ def __getstate__(self) -> Any:
898901 def __setstate__ (self , state : Any ) -> None :
899902 self ._key_color = None
900903 if "_tiles" not in state :
901- tiles : np . ndarray [ Any , Any ] = np .ndarray ((self .height , self .width ), dtype = self .DTYPE )
904+ tiles : NDArray [ Any ] = np .ndarray ((self .height , self .width ), dtype = self .DTYPE )
902905 tiles ["ch" ] = state ["_ch" ]
903906 tiles ["fg" ][..., :3 ] = state ["_fg" ]
904907 tiles ["fg" ][..., 3 ] = 255
@@ -962,7 +965,7 @@ def print(
962965 .. versionchanged:: 13.0
963966 `x` and `y` are now always used as an absolute position for negative values.
964967 """
965- string_ = string .encode ("utf-8" ) # type: bytes
968+ string_ = string .encode ("utf-8" )
966969 lib .TCOD_console_printn (
967970 self .console_c ,
968971 x ,
@@ -1017,7 +1020,7 @@ def print_box(
10171020 .. versionchanged:: 13.0
10181021 `x` and `y` are now always used as an absolute position for negative values.
10191022 """
1020- string_ = string .encode ("utf-8" ) # type: bytes
1023+ string_ = string .encode ("utf-8" )
10211024 return int (
10221025 lib .TCOD_console_printn_rect (
10231026 self .console_c ,
@@ -1118,7 +1121,7 @@ def draw_frame(
11181121 PendingDeprecationWarning ,
11191122 stacklevel = 2 ,
11201123 )
1121- title_ = title .encode ("utf-8" ) # type: bytes
1124+ title_ = title .encode ("utf-8" )
11221125 lib .TCOD_console_printn_frame (
11231126 self .console_c ,
11241127 x ,
@@ -1227,7 +1230,7 @@ def get_height_rect(width: int, string: str) -> int:
12271230
12281231 .. versionadded:: 9.2
12291232 """
1230- string_ = string .encode ("utf-8" ) # type: bytes
1233+ string_ = string .encode ("utf-8" )
12311234 return int (lib .TCOD_console_get_height_rect_wn (width , len (string_ ), string_ ))
12321235
12331236
0 commit comments