Skip to content

Commit cc1d859

Browse files
authored
Merge pull request #3301 from pygame-community/ankith26-new-typing-2
Freetype typing fixes, enhancements to dict/tuple/ellipsis typing
2 parents de6a74d + b1f00ac commit cc1d859

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
from typing import Union, Optional
1+
from typing import TypedDict, Optional
2+
3+
# dict at runtime, this only exist for benefit of the typechecker
4+
class _FingerDict(TypedDict):
5+
id: int
6+
x: float
7+
y: float
8+
pressure: float
29

310
def get_num_devices() -> int: ...
411
def get_device(index: int, /) -> int: ...
512
def get_num_fingers(device_id: int, /) -> int: ...
6-
def get_finger(touchid: int, index: int) -> Optional[dict[str, Union[int, float]]]: ...
13+
def get_finger(touchid: int, index: int) -> Optional[_FingerDict]: ...

buildconfig/stubs/pygame/color.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class Color(Collection[int]):
4141
def __index__(self) -> int: ...
4242
def __invert__(self) -> Color: ...
4343
def __contains__(self, other: int) -> bool: ... # type: ignore[override]
44-
def __getattribute__(self, attr: str) -> Union[Color, tuple]: ...
45-
def __setattr__(self, attr: str, value: Union[Color, tuple]) -> None: ...
44+
def __getattribute__(self, attr: str) -> Union[Color, tuple[int, ...]]: ...
45+
def __setattr__(self, attr: str, value: Union[Color, tuple[int, ...]]) -> None: ...
4646
@overload
4747
@classmethod
4848
def from_cmy(cls, object: tuple[float, float, float], /) -> Color: ...

buildconfig/stubs/pygame/freetype.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get_init() -> bool: ...
1717
def was_init() -> bool: ...
1818
def get_cache_size() -> int: ...
1919
def get_default_resolution() -> int: ...
20-
def set_default_resolution(resolution: int, /) -> None: ...
20+
def set_default_resolution(resolution: int = 0, /) -> None: ...
2121
def SysFont(
2222
name: Union[str, bytes, Iterable[Union[str, bytes]], None],
2323
size: int,
@@ -143,7 +143,7 @@ class Font:
143143
) -> None: ...
144144
def get_rect(
145145
self,
146-
text: str,
146+
text: Optional[str],
147147
style: int = STYLE_DEFAULT,
148148
rotation: int = 0,
149149
size: float = 0,
@@ -158,7 +158,7 @@ class Font:
158158
def get_sizes(self) -> list[tuple[int, int, int, float, float]]: ...
159159
def render(
160160
self,
161-
text: str,
161+
text: Optional[str],
162162
fgcolor: Optional[ColorLike] = None,
163163
bgcolor: Optional[ColorLike] = None,
164164
style: int = STYLE_DEFAULT,
@@ -169,7 +169,7 @@ class Font:
169169
self,
170170
surf: Surface,
171171
dest: RectLike,
172-
text: str,
172+
text: Optional[str],
173173
fgcolor: Optional[ColorLike] = None,
174174
bgcolor: Optional[ColorLike] = None,
175175
style: int = STYLE_DEFAULT,
@@ -178,7 +178,7 @@ class Font:
178178
) -> Rect: ...
179179
def render_raw(
180180
self,
181-
text: str,
181+
text: Optional[str],
182182
style: int = STYLE_DEFAULT,
183183
rotation: int = 0,
184184
size: float = 0,
@@ -187,7 +187,7 @@ class Font:
187187
def render_raw_to(
188188
self,
189189
array: Any,
190-
text: str,
190+
text: Optional[str],
191191
dest: Optional[RectLike] = None,
192192
style: int = STYLE_DEFAULT,
193193
rotation: int = 0,

buildconfig/stubs/pygame/mixer_music.pyi

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
from typing import Optional
1+
from typing import Optional, TypedDict
22

33
from pygame.typing import FileLike
44

5+
class _MusicMetadataDict(TypedDict):
6+
title: str
7+
album: str
8+
artist: str
9+
copyright: str
10+
511
def load(filename: FileLike, namehint: Optional[str] = "") -> None: ...
612
def unload() -> None: ...
713
def play(loops: int = 0, start: float = 0.0, fade_ms: int = 0) -> None: ...
@@ -18,4 +24,6 @@ def get_pos() -> int: ...
1824
def queue(filename: FileLike, namehint: str = "", loops: int = 0) -> None: ...
1925
def set_endevent(event_type: int, /) -> None: ...
2026
def get_endevent() -> int: ...
21-
def get_metadata(filename: Optional[FileLike] = None, namehint: str = "") -> dict[str, str]: ...
27+
def get_metadata(
28+
filename: Optional[FileLike] = None, namehint: str = ""
29+
) -> _MusicMetadataDict: ...

buildconfig/stubs/pygame/pixelarray.pyi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
import sys
12
from typing import Any, Union, overload
23

34
from pygame.surface import Surface
45
from pygame.color import Color
56
from pygame.typing import SequenceLike
67

8+
# 'ellipsis' existed in typeshed pre 3.10, now we use EllipsisType which is
9+
# the modern standard library equivalent.
10+
if sys.version_info >= (3, 10):
11+
from types import EllipsisType
12+
else:
13+
EllipsisType = ellipsis
14+
715
_ColorLike = int | Color | tuple[int, int, int] | tuple[int, int, int, int]
816

917
class PixelArray:
@@ -32,7 +40,7 @@ class PixelArray:
3240
def __getitem__(self, indices: tuple[int, int]) -> int: ...
3341
# returns self
3442
@overload
35-
def __getitem__(self, ell: ellipsis) -> PixelArray: ...
43+
def __getitem__(self, ell: EllipsisType) -> PixelArray: ...
3644
def make_surface(self) -> Surface: ...
3745
def replace(
3846
self,

buildconfig/stubs/pygame/rect.pyi

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ if sys.version_info >= (3, 11):
1616
else:
1717
from typing_extensions import Self
1818

19+
# 'ellipsis' existed in typeshed pre 3.10, now we use EllipsisType which is
20+
# the modern standard library equivalent.
21+
if sys.version_info >= (3, 10):
22+
from types import EllipsisType
23+
else:
24+
EllipsisType = ellipsis
25+
1926
_N = TypeVar("_N", int, float)
2027
_K = TypeVar("_K")
2128
_V = TypeVar("_V")
@@ -129,11 +136,13 @@ class _GenericRect(Collection[_N]):
129136
@overload
130137
def __getitem__(self, i: SupportsIndex) -> _N: ...
131138
@overload
132-
def __getitem__(self, s: slice) -> list[_N]: ...
139+
def __getitem__(self, s: Union[slice, EllipsisType]) -> list[_N]: ...
133140
@overload
134141
def __setitem__(self, key: int, value: float) -> None: ...
135142
@overload
136-
def __setitem__(self, key: slice, value: Union[float, RectLike]) -> None: ...
143+
def __setitem__(
144+
self, key: Union[slice, EllipsisType], value: Union[float, RectLike]
145+
) -> None: ...
137146
def __copy__(self) -> Self: ...
138147
def copy(self) -> Self: ...
139148
@overload

0 commit comments

Comments
 (0)