1111import re
1212import subprocess
1313import sys
14- from collections .abc import Iterable , Iterator
1514from pathlib import Path
16- from typing import Any , ClassVar
15+ from typing import TYPE_CHECKING , Any , ClassVar , Final
1716
1817import attrs
1918import pycparser # type: ignore[import-untyped]
2726
2827import build_sdl
2928
29+ if TYPE_CHECKING :
30+ from collections .abc import Iterable , Iterator
31+
3032Py_LIMITED_API = 0x03100000
3133
3234HEADER_PARSE_PATHS = ("tcod/" , "libtcod/src/libtcod/" )
@@ -269,7 +271,7 @@ def find_sdl_attrs(prefix: str) -> Iterator[tuple[str, int | str | Any]]:
269271
270272 `prefix` is used to filter out which names to copy.
271273 """
272- from tcod ._libtcod import lib
274+ from tcod ._libtcod import lib # noqa: PLC0415
273275
274276 if prefix .startswith ("SDL_" ):
275277 name_starts_at = 4
@@ -320,12 +322,14 @@ def parse_sdl_attrs(prefix: str, all_names: list[str] | None) -> tuple[str, str]
320322]
321323
322324
325+ RE_CONSTANTS_ALL : Final = re .compile (
326+ r"(.*# --- From constants.py ---).*(# --- End constants.py ---.*)" ,
327+ re .DOTALL ,
328+ )
329+
330+
323331def update_module_all (filename : Path , new_all : str ) -> None :
324332 """Update the __all__ of a file with the constants from new_all."""
325- RE_CONSTANTS_ALL = re .compile (
326- r"(.*# --- From constants.py ---).*(# --- End constants.py ---.*)" ,
327- re .DOTALL ,
328- )
329333 match = RE_CONSTANTS_ALL .match (filename .read_text (encoding = "utf-8" ))
330334 assert match , f"Can't determine __all__ subsection in { filename } !"
331335 header , footer = match .groups ()
@@ -346,8 +350,8 @@ def generate_enums(prefix: str) -> Iterator[str]:
346350
347351def write_library_constants () -> None :
348352 """Write libtcod constants into the tcod.constants module."""
349- import tcod .color
350- from tcod ._libtcod import ffi , lib
353+ import tcod .color # noqa: PLC0415
354+ from tcod ._libtcod import ffi , lib # noqa: PLC0415
351355
352356 with Path ("tcod/constants.py" ).open ("w" , encoding = "utf-8" ) as f :
353357 all_names = []
@@ -441,6 +445,8 @@ def _fix_reserved_name(name: str) -> str:
441445
442446@attrs .define (frozen = True )
443447class ConvertedParam :
448+ """Converted type parameter from C types into Python type-hints."""
449+
444450 name : str = attrs .field (converter = _fix_reserved_name )
445451 hint : str
446452 original : str
@@ -460,7 +466,8 @@ def _type_from_names(names: list[str]) -> str:
460466 return "Any"
461467
462468
463- def _param_as_hint (node : pycparser .c_ast .Node , default_name : str ) -> ConvertedParam :
469+ def _param_as_hint (node : pycparser .c_ast .Node , default_name : str ) -> ConvertedParam : # noqa: PLR0911
470+ """Return a Python type-hint from a C AST node."""
464471 original = pycparser .c_generator .CGenerator ().visit (node )
465472 name : str
466473 names : list [str ]
0 commit comments