Skip to content

Commit 065a7c7

Browse files
authored
Merge pull request #3398 from aatle/buffer-stubs
Fix stubs for buffer types
2 parents 4d94b75 + 4c291ab commit 065a7c7

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

buildconfig/stubs/pygame/bufferproxy.pyi

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

34
class BufferProxy:
@@ -13,5 +14,12 @@ class BufferProxy:
1314
def __array_interface__(self) -> dict[str, Any]: ...
1415
@property
1516
def __array_struct__(self) -> Any: ...
17+
if sys.version_info >= (3, 12):
18+
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
19+
def __release_buffer__(self, view: memoryview[int], /) -> None: ...
1620
def __init__(self, parent: Any) -> None: ... # TODO: parent: TypedDict | Protocol
17-
def write(self, buffer: bytes, offset: int = 0) -> None: ...
21+
def write(
22+
self,
23+
buffer: str | bytes, # Any "read-only bytes-like-object" is valid
24+
offset: int = 0,
25+
) -> None: ...

buildconfig/stubs/pygame/color.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from collections.abc import Collection, Iterator
23
from typing import Any, ClassVar, SupportsIndex, Union, overload
34

@@ -22,6 +23,8 @@ class Color(Collection[int]):
2223
__hash__: ClassVar[None] # type: ignore[assignment]
2324
@property
2425
def __array_struct__(self) -> Any: ...
26+
if sys.version_info >= (3, 12):
27+
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
2528
@overload
2629
def __init__(self, r: int, g: int, b: int, a: int = 255) -> None: ...
2730
@overload

buildconfig/stubs/pygame/image.pyi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ from typing import Literal, Optional, Union
6767
from pygame.bufferproxy import BufferProxy
6868
from pygame.surface import Surface
6969
from pygame.typing import FileLike, IntPoint, Point
70-
from typing_extensions import deprecated # added in 3.13
70+
from typing_extensions import (
71+
Buffer, # collections.abc 3.12
72+
deprecated, # added in 3.13
73+
)
7174

72-
_BufferLike = Union[BufferProxy, bytes, bytearray, memoryview]
7375
_from_buffer_format = Literal["P", "RGB", "BGR", "BGRA", "RGBX", "RGBA", "ARGB"]
7476
_to_bytes_format = Literal[
7577
"P", "RGB", "RGBX", "RGBA", "ARGB", "BGRA", "ABGR", "RGBA_PREMULT", "ARGB_PREMULT"
@@ -320,7 +322,7 @@ def frombytes(
320322
"""
321323

322324
def frombuffer(
323-
buffer: _BufferLike,
325+
buffer: Buffer,
324326
size: IntPoint,
325327
format: _from_buffer_format,
326328
pitch: int = -1,

buildconfig/stubs/pygame/mask.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from typing import Any, Optional, Union
23

34
from pygame.rect import Rect
@@ -53,6 +54,9 @@ class Mask:
5354
unsetcolor: Optional[ColorLike] = (0, 0, 0, 255),
5455
dest: Union[RectLike, Point] = (0, 0),
5556
) -> Surface: ...
57+
if sys.version_info >= (3, 12):
58+
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
59+
def __release_buffer__(self, view: memoryview[int], /) -> None: ...
5660

5761
@deprecated("Use `Mask` instead (MaskType is an old alias)")
5862
class MaskType(Mask): ...

buildconfig/stubs/pygame/mixer.pyi

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

3-
import numpy
44
from pygame.event import Event
55
from pygame.typing import FileLike
6-
from typing_extensions import deprecated # added in 3.13
6+
from typing_extensions import (
7+
Buffer, # collections.abc 3.12
8+
deprecated, # added in 3.13
9+
)
710

811
from . import mixer_music
912

@@ -46,13 +49,7 @@ class Sound:
4649
@overload
4750
def __init__(self, file: FileLike) -> None: ...
4851
@overload
49-
def __init__(
50-
self, buffer: Any
51-
) -> None: ... # Buffer protocol is still not implemented in typing
52-
@overload
53-
def __init__(
54-
self, array: numpy.ndarray
55-
) -> None: ... # Buffer protocol is still not implemented in typing
52+
def __init__(self, buffer: Buffer) -> None: ...
5653
def play(
5754
self,
5855
loops: int = 0,
@@ -65,6 +62,9 @@ class Sound:
6562
def __array_interface__(self) -> dict[str, Any]: ...
6663
@property
6764
def __array_struct__(self) -> Any: ...
65+
if sys.version_info >= (3, 12):
66+
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
67+
def __release_buffer__(self, view: memoryview[int], /) -> None: ...
6868
def stop(self) -> None: ...
6969
def fadeout(self, time: int, /) -> None: ...
7070
def set_volume(self, value: float, /) -> None: ...

buildconfig/stubs/pygame/pixelarray.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class PixelArray:
3131
def __array_interface__(self) -> dict[str, Any]: ...
3232
@property
3333
def __array_struct__(self) -> Any: ...
34+
if sys.version_info >= (3, 12):
35+
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
3436
def __init__(self, surface: Surface) -> None: ...
3537
def __enter__(self) -> PixelArray: ...
3638
def __exit__(self, *args, **kwargs) -> None: ...

0 commit comments

Comments
 (0)