Skip to content

Fix stubs for buffer types #3398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion buildconfig/stubs/pygame/bufferproxy.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Any

class BufferProxy:
Expand All @@ -13,5 +14,12 @@ class BufferProxy:
def __array_interface__(self) -> dict[str, Any]: ...
@property
def __array_struct__(self) -> Any: ...
if sys.version_info >= (3, 12):
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
def __release_buffer__(self, view: memoryview[int], /) -> None: ...
def __init__(self, parent: Any) -> None: ... # TODO: parent: TypedDict | Protocol
def write(self, buffer: bytes, offset: int = 0) -> None: ...
def write(
self,
buffer: str | bytes, # See https://docs.python.org/3/c-api/arg.html at s#
offset: int = 0,
) -> None: ...
3 changes: 3 additions & 0 deletions buildconfig/stubs/pygame/color.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from collections.abc import Collection, Iterator
from typing import Any, ClassVar, SupportsIndex, Union, overload

Expand All @@ -21,6 +22,8 @@ class Color(Collection[int]):
__hash__: ClassVar[None] # type: ignore[assignment]
@property
def __array_struct__(self) -> Any: ...
if sys.version_info >= (3, 12):
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
@overload
def __init__(self, r: int, g: int, b: int, a: int = 255) -> None: ...
@overload
Expand Down
8 changes: 5 additions & 3 deletions buildconfig/stubs/pygame/image.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ from typing import Literal, Optional, Union
from pygame.bufferproxy import BufferProxy
from pygame.surface import Surface
from pygame.typing import FileLike, IntPoint, Point
from typing_extensions import deprecated # added in 3.13
from typing_extensions import (
Buffer, # collections.abc 3.12
deprecated, # added in 3.13
)

_BufferLike = Union[BufferProxy, bytes, bytearray, memoryview]
_from_buffer_format = Literal["P", "RGB", "BGR", "BGRA", "RGBX", "RGBA", "ARGB"]
_to_bytes_format = Literal[
"P", "RGB", "RGBX", "RGBA", "ARGB", "BGRA", "ABGR", "RGBA_PREMULT", "ARGB_PREMULT"
Expand Down Expand Up @@ -47,7 +49,7 @@ def frombytes(
pitch: int = -1,
) -> Surface: ...
def frombuffer(
buffer: _BufferLike,
buffer: Buffer,
size: IntPoint,
format: _from_buffer_format,
pitch: int = -1,
Expand Down
4 changes: 4 additions & 0 deletions buildconfig/stubs/pygame/mask.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Any, Optional, Union

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

@deprecated("Use `Mask` instead (MaskType is an old alias)")
class MaskType(Mask): ...
18 changes: 9 additions & 9 deletions buildconfig/stubs/pygame/mixer.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sys
from typing import Any, Optional, Union, overload

import numpy
from pygame.event import Event
from pygame.typing import FileLike
from typing_extensions import deprecated # added in 3.13
from typing_extensions import (
Buffer, # collections.abc 3.12
deprecated, # added in 3.13
)

from . import mixer_music

Expand Down Expand Up @@ -46,13 +49,7 @@ class Sound:
@overload
def __init__(self, file: FileLike) -> None: ...
@overload
def __init__(
self, buffer: Any
) -> None: ... # Buffer protocol is still not implemented in typing
@overload
def __init__(
self, array: numpy.ndarray
) -> None: ... # Buffer protocol is still not implemented in typing
def __init__(self, buffer: Buffer) -> None: ...
def play(
self,
loops: int = 0,
Expand All @@ -65,6 +62,9 @@ class Sound:
def __array_interface__(self) -> dict[str, Any]: ...
@property
def __array_struct__(self) -> Any: ...
if sys.version_info >= (3, 12):
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
def __release_buffer__(self, view: memoryview[int], /) -> None: ...
def stop(self) -> None: ...
def fadeout(self, time: int, /) -> None: ...
def set_volume(self, value: float, /) -> None: ...
Expand Down
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/pixelarray.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class PixelArray:
def __array_interface__(self) -> dict[str, Any]: ...
@property
def __array_struct__(self) -> Any: ...
if sys.version_info >= (3, 12):
def __buffer__(self, flags: int, /) -> memoryview[int]: ...
def __init__(self, surface: Surface) -> None: ...
def __enter__(self) -> PixelArray: ...
def __exit__(self, *args, **kwargs) -> None: ...
Expand Down
Loading