Skip to content

Commit dd3df52

Browse files
authored
Merge pull request #3150 from pygame-community/ankith26-typing-test
Add basic pygame.typing unit test, remove problematic TypeAlias usage
2 parents 3ce1d23 + 8414fed commit dd3df52

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

buildconfig/stubs/pygame/typing.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ __all__ = [
1414

1515
import sys
1616
from abc import abstractmethod
17-
from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias
17+
from typing import IO, Callable, Tuple, Union, TypeVar, Protocol
1818

1919
if sys.version_info >= (3, 9):
2020
from os import PathLike as _PathProtocol
@@ -27,9 +27,9 @@ else:
2727

2828

2929
# For functions that take a file name
30-
_PathLike: TypeAlias = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]]
30+
_PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]]
3131
# Most pygame functions that take a file argument should be able to handle a FileLike type
32-
FileLike: TypeAlias = Union[_PathLike, IO[bytes], IO[str]]
32+
FileLike = Union[_PathLike, IO[bytes], IO[str]]
3333

3434
_T_co = TypeVar("_T_co", covariant=True)
3535

@@ -49,11 +49,11 @@ class SequenceLike(Protocol[_T_co]):
4949

5050
# Pygame handles float without errors in most cases where a point is expected,
5151
# usually rounding to int. Also, 'Union[int, float] == float'
52-
Point: TypeAlias = SequenceLike[float]
52+
Point = SequenceLike[float]
5353
# This is used where ints are strictly required
54-
IntPoint: TypeAlias = SequenceLike[int]
54+
IntPoint = SequenceLike[int]
5555

56-
ColorLike: TypeAlias = Union[int, str, SequenceLike[int]]
56+
ColorLike = Union[int, str, SequenceLike[int]]
5757

5858

5959
class _HasRectAttribute(Protocol):
@@ -63,8 +63,8 @@ class _HasRectAttribute(Protocol):
6363
def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ...
6464

6565

66-
RectLike: TypeAlias = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute]
66+
RectLike = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute]
6767

6868

6969
# cleanup namespace
70-
del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias
70+
del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol

src_py/typing.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import sys
1616
from abc import abstractmethod
17-
from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias
17+
from typing import IO, Callable, Tuple, Union, TypeVar, Protocol
1818

1919
if sys.version_info >= (3, 9):
2020
from os import PathLike as _PathProtocol
@@ -27,9 +27,9 @@ def __fspath__(self) -> _AnyStr_co: ...
2727

2828

2929
# For functions that take a file name
30-
_PathLike: TypeAlias = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]]
30+
_PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]]
3131
# Most pygame functions that take a file argument should be able to handle a FileLike type
32-
FileLike: TypeAlias = Union[_PathLike, IO[bytes], IO[str]]
32+
FileLike = Union[_PathLike, IO[bytes], IO[str]]
3333

3434
_T_co = TypeVar("_T_co", covariant=True)
3535

@@ -49,11 +49,11 @@ def __len__(self) -> int: ...
4949

5050
# Pygame handles float without errors in most cases where a point is expected,
5151
# usually rounding to int. Also, 'Union[int, float] == float'
52-
Point: TypeAlias = SequenceLike[float]
52+
Point = SequenceLike[float]
5353
# This is used where ints are strictly required
54-
IntPoint: TypeAlias = SequenceLike[int]
54+
IntPoint = SequenceLike[int]
5555

56-
ColorLike: TypeAlias = Union[int, str, SequenceLike[int]]
56+
ColorLike = Union[int, str, SequenceLike[int]]
5757

5858

5959
class _HasRectAttribute(Protocol):
@@ -63,8 +63,8 @@ class _HasRectAttribute(Protocol):
6363
def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ...
6464

6565

66-
RectLike: TypeAlias = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute]
66+
RectLike = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute]
6767

6868

6969
# cleanup namespace
70-
del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol, TypeAlias
70+
del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol

test/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ test_files = files(
5050
'time_test.py',
5151
'touch_test.py',
5252
'transform_test.py',
53+
'typing_test.py',
5354
'version_test.py',
5455
'video_test.py',
5556
'window_test.py',

test/typing_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
3+
# A basic unit test to test that pygame.typing can import in python code, and
4+
# the documented attributes are accessible.
5+
# More rigorous testing needs mypy and is therefore implemented in the stubs
6+
# directory.
7+
TYPING_PUBLIC_ATTRS = [
8+
"RectLike",
9+
"SequenceLike",
10+
"FileLike",
11+
"ColorLike",
12+
"Point",
13+
"IntPoint",
14+
]
15+
16+
17+
class TypingTest(unittest.TestCase):
18+
def test_typing_has_attrs(self):
19+
import pygame.typing
20+
21+
for i in TYPING_PUBLIC_ATTRS:
22+
self.assertTrue(hasattr(pygame.typing, i))
23+
24+
25+
if __name__ == "__main__":
26+
unittest.main()

0 commit comments

Comments
 (0)