Skip to content

Commit eb8ebf2

Browse files
committed
Fix typing.
1 parent 59b1812 commit eb8ebf2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

sdl3/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,12 @@ def SDL_DOWNLOAD_BINARIES(path: str, system: str = SDL_SYSTEM, arch: str = SDL_A
181181
binaryData["files"].remove(path)
182182
break
183183

184-
def SDL_ARRAY(*args: list[typing.Any], **kwargs: dict[str, typing.Any]) -> tuple[ctypes.Array[typing.Any], int]:
185-
"""Create a ctypes array."""
186-
return ((kwargs.get("type") or args[0].__class__) * len(args))(*args), len(args)
184+
BaseType = typing.TypeVar("BaseType")
185+
TargetType = typing.TypeVar("TargetType")
186+
187+
def SDL_ARRAY(*args: BaseType, **kwargs: TargetType) -> tuple[ctypes.Array[BaseType | TargetType], int]:
188+
"""Create a ctypes array from the given arguments."""
189+
return ((kwargs.get("type", None) or args[0].__class__) * len(args))(*args), len(args)
187190

188191
def SDL_DEREFERENCE(value: typing.Any) -> typing.Any:
189192
"""Dereference a ctypes pointer or object."""
@@ -195,7 +198,7 @@ def SDL_CACHE_FUNC(func: abc.Callable[..., typing.Any]) -> abc.Callable[..., typ
195198
"""Simple function cache decorator."""
196199
cache = {}
197200

198-
def __inner__(*args: list[typing.Any], **kwargs: dict[str, typing.Any]) -> typing.Any:
201+
def __inner__(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
199202
_hash = hash((args, tuple(frozenset(sorted(kwargs.items())))))
200203
if _hash not in cache: cache.update({_hash: func(*args, **kwargs)})
201204
return cache.get(_hash, None)
@@ -241,7 +244,7 @@ def __class_getitem__(cls, key: tuple[str, type, list[type], str]) -> typing.Any
241244
func.restype, func.binary = key[1], binary
242245

243246
if ... in key[2]:
244-
def __inner__(*args: list[typing.Any], **kwargs: dict[str, typing.Any]) -> typing.Any:
247+
def __inner__(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
245248
for arg in args[len(__inner__.func.argtypes):]:
246249
if isinstance(arg, int): __inner__.func.argtypes += [ctypes.c_int]
247250
elif isinstance(arg, float): __inner__.func.argtypes += [ctypes.c_float]
@@ -490,7 +493,7 @@ def SDL_PYTHONIZE_TYPE(type: type, name: str | None = None) -> str:
490493
arguments = [f"{'_' if i in keyword.kwlist else ''}{i.replace('[', '').replace(']', '')}" for i in arguments]
491494

492495
assert _return is None or SDL_PYTHONIZE_TYPE(_return) == SDL_GET_NAME(restype), f"return type mismatch for 'https://wiki.libsdl.org/{module}/{func}' (expected: {SDL_PYTHONIZE_TYPE(_return)}, got: {SDL_GET_NAME(restype)})."
493-
definitions += f"def {func}({', '.join([f'{arg}: {SDL_GET_NAME(type)}' for arg, type in zip(arguments, argtypes)] + (['*args: list[typing.Any]'] if vararg else []))}) -> {SDL_GET_NAME(restype)}:\n"
496+
definitions += f"def {func}({', '.join([f'{arg}: {SDL_GET_NAME(type)}' for arg, type in zip(arguments, argtypes)] + (['*args: typing.Any'] if vararg else []))}) -> {SDL_GET_NAME(restype)}:\n"
494497
if not rst or description is not None: definitions += f"{' ' * 4}\"\"\"\n"
495498
if description is not None: definitions += f" {description}\n"
496499
if not rst: definitions += f"\n{' ' * 4}https://wiki.libsdl.org/{module}/{func}\n"

0 commit comments

Comments
 (0)