From d374db203df0cc4b7c3b336a7f829cbbb24486ec Mon Sep 17 00:00:00 2001 From: Ankith Date: Mon, 7 Oct 2024 01:51:58 +0530 Subject: [PATCH 1/2] Add basic pygame.typing unit test --- test/meson.build | 1 + test/typing_test.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/typing_test.py diff --git a/test/meson.build b/test/meson.build index 2ef78899bf..d789ad01e0 100644 --- a/test/meson.build +++ b/test/meson.build @@ -50,6 +50,7 @@ test_files = files( 'time_test.py', 'touch_test.py', 'transform_test.py', + 'typing_test.py', 'version_test.py', 'video_test.py', 'window_test.py', diff --git a/test/typing_test.py b/test/typing_test.py new file mode 100644 index 0000000000..46fd4d29c3 --- /dev/null +++ b/test/typing_test.py @@ -0,0 +1,26 @@ +import unittest + +# A basic unit test to test that pygame.typing can import in python code, and +# the documented attributes are accessible. +# More rigorous testing needs mypy and is therefore implemented in the stubs +# directory. +TYPING_PUBLIC_ATTRS = [ + "RectLike", + "SequenceLike", + "FileLike", + "ColorLike", + "Point", + "IntPoint", +] + + +class TypingTest(unittest.TestCase): + def test_typing_has_attrs(self): + import pygame.typing + + for i in TYPING_PUBLIC_ATTRS: + self.assertTrue(hasattr(pygame.typing, i)) + + +if __name__ == "__main__": + unittest.main() From 8414fed7d04d7d3eae1ac7d3e593af345d4e2ca6 Mon Sep 17 00:00:00 2001 From: Ankith Date: Thu, 10 Oct 2024 16:51:55 +0530 Subject: [PATCH 2/2] Revert TypeAlias usage in typing --- buildconfig/stubs/pygame/typing.pyi | 16 ++++++++-------- src_py/typing.py | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/buildconfig/stubs/pygame/typing.pyi b/buildconfig/stubs/pygame/typing.pyi index 531a037fb4..45d793a4ac 100644 --- a/buildconfig/stubs/pygame/typing.pyi +++ b/buildconfig/stubs/pygame/typing.pyi @@ -14,7 +14,7 @@ __all__ = [ import sys from abc import abstractmethod -from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias +from typing import IO, Callable, Tuple, Union, TypeVar, Protocol if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol @@ -27,9 +27,9 @@ else: # For functions that take a file name -_PathLike: TypeAlias = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] +_PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] # Most pygame functions that take a file argument should be able to handle a FileLike type -FileLike: TypeAlias = Union[_PathLike, IO[bytes], IO[str]] +FileLike = Union[_PathLike, IO[bytes], IO[str]] _T_co = TypeVar("_T_co", covariant=True) @@ -49,11 +49,11 @@ class SequenceLike(Protocol[_T_co]): # Pygame handles float without errors in most cases where a point is expected, # usually rounding to int. Also, 'Union[int, float] == float' -Point: TypeAlias = SequenceLike[float] +Point = SequenceLike[float] # This is used where ints are strictly required -IntPoint: TypeAlias = SequenceLike[int] +IntPoint = SequenceLike[int] -ColorLike: TypeAlias = Union[int, str, SequenceLike[int]] +ColorLike = Union[int, str, SequenceLike[int]] class _HasRectAttribute(Protocol): @@ -63,8 +63,8 @@ class _HasRectAttribute(Protocol): def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... -RectLike: TypeAlias = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute] +RectLike = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute] # cleanup namespace -del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias +del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol diff --git a/src_py/typing.py b/src_py/typing.py index 531a037fb4..45d793a4ac 100644 --- a/src_py/typing.py +++ b/src_py/typing.py @@ -14,7 +14,7 @@ import sys from abc import abstractmethod -from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias +from typing import IO, Callable, Tuple, Union, TypeVar, Protocol if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol @@ -27,9 +27,9 @@ def __fspath__(self) -> _AnyStr_co: ... # For functions that take a file name -_PathLike: TypeAlias = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] +_PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] # Most pygame functions that take a file argument should be able to handle a FileLike type -FileLike: TypeAlias = Union[_PathLike, IO[bytes], IO[str]] +FileLike = Union[_PathLike, IO[bytes], IO[str]] _T_co = TypeVar("_T_co", covariant=True) @@ -49,11 +49,11 @@ def __len__(self) -> int: ... # Pygame handles float without errors in most cases where a point is expected, # usually rounding to int. Also, 'Union[int, float] == float' -Point: TypeAlias = SequenceLike[float] +Point = SequenceLike[float] # This is used where ints are strictly required -IntPoint: TypeAlias = SequenceLike[int] +IntPoint = SequenceLike[int] -ColorLike: TypeAlias = Union[int, str, SequenceLike[int]] +ColorLike = Union[int, str, SequenceLike[int]] class _HasRectAttribute(Protocol): @@ -63,8 +63,8 @@ class _HasRectAttribute(Protocol): def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... -RectLike: TypeAlias = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute] +RectLike = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute] # cleanup namespace -del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias +del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol