From 1fba232c5daf7f7c4dd110906d8aaec44d63ffa5 Mon Sep 17 00:00:00 2001 From: Ankith Date: Fri, 17 Oct 2025 12:30:12 +0530 Subject: [PATCH 1/2] Drop python 3.9 --- .github/workflows/build-sdl3.yml | 3 +++ .github/workflows/run-ubuntu-checks.yml | 5 ++--- README.rst | 2 +- docs/readmes/README.es.rst | 2 +- docs/readmes/README.fa.rst | 2 +- docs/readmes/README.fr.rst | 2 +- docs/readmes/README.gr.rst | 2 +- docs/readmes/README.it.rst | 2 +- docs/readmes/README.ja.rst | 2 +- docs/readmes/README.pt-br.rst | 2 +- docs/readmes/README.ru.rst | 2 +- docs/readmes/README.zh-cn.rst | 2 +- docs/readmes/README.zh-tw.rst | 2 +- pyproject.toml | 5 ++--- setup.cfg | 2 +- setup.py | 9 ++++----- 16 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.github/workflows/build-sdl3.yml b/.github/workflows/build-sdl3.yml index 0ee78442b3..fc7a5dbbb7 100644 --- a/.github/workflows/build-sdl3.yml +++ b/.github/workflows/build-sdl3.yml @@ -53,6 +53,9 @@ jobs: steps: - uses: actions/checkout@v5.0.0 + - uses: actions/setup-python@v6 + with: + python-version: '3.14' - name: Install pygame deps (linux) if: matrix.os == 'ubuntu-24.04' diff --git a/.github/workflows/run-ubuntu-checks.yml b/.github/workflows/run-ubuntu-checks.yml index cf8fc50850..b8eebbcd0b 100644 --- a/.github/workflows/run-ubuntu-checks.yml +++ b/.github/workflows/run-ubuntu-checks.yml @@ -53,9 +53,8 @@ jobs: os: [ ubuntu-24.04 ] # check our min python (minor) version and our max python (minor) version python: [ - 3.9.23, - 3.13.5, - 3.14.0rc1 + 3.10.17, + 3.14.0, ] env: diff --git a/README.rst b/README.rst index 00235150a0..c4c18663fb 100644 --- a/README.rst +++ b/README.rst @@ -139,7 +139,7 @@ Dependency versions: +----------+------------------------+ -| CPython | >= 3.9 (Or use PyPy3) | +| CPython | >= 3.10 (Or use PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.es.rst b/docs/readmes/README.es.rst index ccefa726b7..185430608c 100644 --- a/docs/readmes/README.es.rst +++ b/docs/readmes/README.es.rst @@ -117,7 +117,7 @@ El módulo surfarray necesita el paquete python numpy, para sus matrices numéri Versiones de dependencia: +----------+-----------------------------+ -| CPython | >= 3.9 (Ou utiliser PyPy3) | +| CPython | >= 3.10 (Ou utiliser PyPy3) | +----------+-----------------------------+ | SDL | >= 2.0.14 | +----------+-----------------------------+ diff --git a/docs/readmes/README.fa.rst b/docs/readmes/README.fa.rst index 3a8663dc17..2e98c0abee 100644 --- a/docs/readmes/README.fa.rst +++ b/docs/readmes/README.fa.rst @@ -189,7 +189,7 @@ Dependencies (وابستگی ها) مشاهده کنید. +----------+------------------------+ -| CPython | >= 3.9 (Or use PyPy3) | +| CPython | >= 3.10 (Or use PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.fr.rst b/docs/readmes/README.fr.rst index 7adae3adfe..31cb8c7c1c 100644 --- a/docs/readmes/README.fr.rst +++ b/docs/readmes/README.fr.rst @@ -146,7 +146,7 @@ multidimensionnels. Versions des dépendances: +----------+-----------------------------+ -| CPython | >= 3.9 (Ou utiliser PyPy3) | +| CPython | >= 3.10 (Ou utiliser PyPy3) | +----------+-----------------------------+ | SDL | >= 2.0.14 | +----------+-----------------------------+ diff --git a/docs/readmes/README.gr.rst b/docs/readmes/README.gr.rst index 0fa1974079..ffa9e36a98 100644 --- a/docs/readmes/README.gr.rst +++ b/docs/readmes/README.gr.rst @@ -140,7 +140,7 @@ pygame.org ιστοσελίδα ώστε άλλοι να μάθουν και ν +----------+------------------------+ -| CPython | >= 3.9 (Or use PyPy3) | +| CPython | >= 3.10 (Or use PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.it.rst b/docs/readmes/README.it.rst index e1c5fa3fbb..38baec6341 100644 --- a/docs/readmes/README.it.rst +++ b/docs/readmes/README.it.rst @@ -140,7 +140,7 @@ Le versioni dei requisiti: +----------+------------------------+ -| CPython | >= 3.9 (Or use PyPy3) | +| CPython | >= 3.10 (Or use PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.ja.rst b/docs/readmes/README.ja.rst index 8c9afda883..0bef8992b9 100644 --- a/docs/readmes/README.ja.rst +++ b/docs/readmes/README.ja.rst @@ -151,7 +151,7 @@ gfxdraw には SDL_gfx の埋め込みバージョンが含まれています。 依存バージョン: +----------+------------------------+ -| CPython | >= 3.9 (Or use PyPy3) | +| CPython | >= 3.10 (Or use PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.pt-br.rst b/docs/readmes/README.pt-br.rst index 9c1251acc8..28ee640af0 100644 --- a/docs/readmes/README.pt-br.rst +++ b/docs/readmes/README.pt-br.rst @@ -135,7 +135,7 @@ e gfxdraw contém uma versão embutida do SDL_gfx. Versões de dependências: +----------+------------------------+ -| CPython | >= 3.9 (Ou use PyPy3) | +| CPython | >= 3.10 (Ou use PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.ru.rst b/docs/readmes/README.ru.rst index 754e7cb4bc..3298792763 100644 --- a/docs/readmes/README.ru.rst +++ b/docs/readmes/README.ru.rst @@ -139,7 +139,7 @@ SDL_rotozoom, а gfxdraw имеет встроенную версию SDL_gfx. +----------+------------------------+ -| CPython | >= 3.9 (Or use PyPy3) | +| CPython | >= 3.10 (Or use PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.zh-cn.rst b/docs/readmes/README.zh-cn.rst index 91a812d764..1fa7d35e78 100644 --- a/docs/readmes/README.zh-cn.rst +++ b/docs/readmes/README.zh-cn.rst @@ -104,7 +104,7 @@ pygame显然依赖于SDL和Python。此外pygame还嵌入了几个较小的库 +----------+------------------------+ -| CPython | >= 3.9 (或 PyPy3) | +| CPython | >= 3.10 (或 PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/docs/readmes/README.zh-tw.rst b/docs/readmes/README.zh-tw.rst index 517c70699d..4f4881d0e4 100644 --- a/docs/readmes/README.zh-tw.rst +++ b/docs/readmes/README.zh-tw.rst @@ -109,7 +109,7 @@ pygame顯然依賴SDL和Python。此外pygame也嵌入了幾個較小的函式 +----------+------------------------+ -| CPython | >= 3.9 (或 PyPy3) | +| CPython | >= 3.10 (或 PyPy3) | +----------+------------------------+ | SDL | >= 2.0.14 | +----------+------------------------+ diff --git a/pyproject.toml b/pyproject.toml index 5faca6ec3d..95e5ba0664 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "pygame-ce" version = "2.5.7.dev1" description = "Python Game Development" readme = "README.rst" # for long description -requires-python = ">=3.9" +requires-python = ">=3.10" license = {text = "LGPL v2.1"} authors = [{name = "A community project"}] classifiers = [ @@ -14,7 +14,6 @@ classifiers = [ "Programming Language :: Cython", "Programming Language :: Python", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", @@ -77,7 +76,7 @@ install = ['--tags=runtime,python-runtime,pg-tag'] # dependencies. Here is where uv comes into the picture. It is an "installer" like pip, # but faster. It has been observed to save a couple of minutes of CI time. build-frontend = "build[uv]" -build = "cp3{9,10,11,12,13,14}-* pp3{10,11}-*" +build = "cp3{10,11,12,13,14}-* pp311-*" skip = ["*-musllinux_*", "cp31?t-*"] # build[uv] is verbose by default, so below flag is not needed here # build-verbosity = 3 diff --git a/setup.cfg b/setup.cfg index c3fdd9f9d5..bfb8b9d23a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [tox:tox] -envlist = py{39,310,311,312,313} +envlist = py{310,311,312,313,314} skip_missing_interpreters = True skipsdist = True diff --git a/setup.py b/setup.py index 1bf4831ece..388d230066 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,6 @@ "Programming Language :: Cython", "Programming Language :: Python", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", @@ -63,7 +62,7 @@ "Operating System :: MacOS", "Typing :: Typed" ], - "python_requires": '>=3.9', + "python_requires": '>=3.10', } import re @@ -156,11 +155,11 @@ def compilation_help(): print('---\n') -if not hasattr(sys, 'version_info') or sys.version_info < (3, 9): +if not hasattr(sys, 'version_info') or sys.version_info < (3, 10): compilation_help() - raise SystemExit("Pygame-ce requires Python3 version 3.9 or above.") + raise SystemExit("Pygame-ce requires Python3 version 3.10 or above.") if IS_PYPY and sys.pypy_version_info < (7,): - raise SystemExit("Pygame-ce requires PyPy version 7.0.0 above, compatible with CPython >= 3.9") + raise SystemExit("Pygame-ce requires PyPy version 7.0.0 above, compatible with CPython >= 3.10") def consume_arg(name): From 1f001e9a10809f171091586a1323e208b74062b1 Mon Sep 17 00:00:00 2001 From: Ankith Date: Fri, 17 Oct 2025 12:37:15 +0530 Subject: [PATCH 2/2] Modernise code to use python 3.10 features --- buildconfig/stubs/pygame/_debug.pyi | 9 +- buildconfig/stubs/pygame/_render.pyi | 26 ++--- .../stubs/pygame/_sdl2/controller_old.pyi | 3 +- buildconfig/stubs/pygame/_sdl2/sdl2.pyi | 4 +- buildconfig/stubs/pygame/_sdl2/touch.pyi | 4 +- buildconfig/stubs/pygame/_sdl2/video.pyi | 36 +++--- buildconfig/stubs/pygame/camera.pyi | 10 +- buildconfig/stubs/pygame/color.pyi | 6 +- buildconfig/stubs/pygame/cursors.pyi | 28 ++--- buildconfig/stubs/pygame/display.pyi | 16 +-- buildconfig/stubs/pygame/draw.pyi | 6 +- buildconfig/stubs/pygame/event.pyi | 16 +-- buildconfig/stubs/pygame/font.pyi | 20 ++-- buildconfig/stubs/pygame/freetype.pyi | 34 +++--- buildconfig/stubs/pygame/geometry.pyi | 20 ++-- buildconfig/stubs/pygame/image.pyi | 25 ++-- buildconfig/stubs/pygame/mask.pyi | 22 ++-- buildconfig/stubs/pygame/math.pyi | 110 ++++++++---------- buildconfig/stubs/pygame/midi.pyi | 11 +- buildconfig/stubs/pygame/mixer.pyi | 12 +- buildconfig/stubs/pygame/pixelarray.pyi | 16 +-- buildconfig/stubs/pygame/pixelcopy.pyi | 6 +- buildconfig/stubs/pygame/rect.pyi | 33 +++--- buildconfig/stubs/pygame/rwobject.pyi | 14 +-- buildconfig/stubs/pygame/scrap.pyi | 4 +- buildconfig/stubs/pygame/sprite.pyi | 61 +++++----- buildconfig/stubs/pygame/surface.pyi | 34 +++--- buildconfig/stubs/pygame/system.pyi | 6 +- buildconfig/stubs/pygame/time.pyi | 4 +- buildconfig/stubs/pygame/transform.pyi | 46 ++++---- buildconfig/stubs/pygame/typing.pyi | 23 ++-- buildconfig/stubs/pygame/window.pyi | 12 +- dev.py | 4 +- src_c/doc/display_doc.h | 2 +- src_c/doc/image_doc.h | 2 +- src_c/doc/surface_doc.h | 6 +- src_c/doc/system_doc.h | 2 +- src_c/doc/window_doc.h | 2 +- src_py/_data_classes.py | 5 +- src_py/_debug.py | 4 - src_py/sprite.py | 9 +- src_py/typing.py | 23 ++-- 42 files changed, 346 insertions(+), 390 deletions(-) diff --git a/buildconfig/stubs/pygame/_debug.pyi b/buildconfig/stubs/pygame/_debug.pyi index 6707acb2c8..61aecb1213 100644 --- a/buildconfig/stubs/pygame/_debug.pyi +++ b/buildconfig/stubs/pygame/_debug.pyi @@ -1,10 +1,7 @@ from collections.abc import Callable -from typing import Optional, Union -ImportResult = tuple[str, bool, Optional[Callable]] - -def str_from_tuple(version_tuple: Union[tuple[int, int, int], None]) -> str: ... +def str_from_tuple(version_tuple: tuple[int, int, int] | None) -> str: ... def attempt_import( module: str, function_name: str, output_str: str = "" -) -> ImportResult: ... -def print_debug_info(filename: Optional[str] = None) -> None: ... +) -> tuple[str, bool, Callable | None]: ... +def print_debug_info(filename: str | None = None) -> None: ... diff --git a/buildconfig/stubs/pygame/_render.pyi b/buildconfig/stubs/pygame/_render.pyi index 155e0786de..907bda2601 100644 --- a/buildconfig/stubs/pygame/_render.pyi +++ b/buildconfig/stubs/pygame/_render.pyi @@ -1,5 +1,5 @@ from collections.abc import Iterable -from typing import Any, Optional, Protocol, Union, final +from typing import Any, Protocol, final from pygame.color import Color from pygame.rect import Rect @@ -9,9 +9,7 @@ from pygame.window import Window class _DrawableClass(Protocol): # Object that has the draw method that accepts area and dest arguments - def draw( - self, area: Optional[RectLike] = None, dest: Optional[RectLike] = None - ): ... + def draw(self, area: RectLike | None = None, dest: RectLike | None = None): ... @final class Renderer: @@ -25,9 +23,9 @@ class Renderer: ) -> None: ... def blit( self, - source: Union["Texture", "Image", _DrawableClass], - dest: Optional[RectLike] = None, - area: Optional[RectLike] = None, + source: "Texture" | "Image" | _DrawableClass, + dest: RectLike | None = None, + area: RectLike | None = None, special_flags: int = 0, ) -> Rect: ... def clear(self) -> None: ... @@ -43,9 +41,9 @@ class Renderer: def coordinates_to_window(self, point: Point) -> tuple[float, float]: ... def coordinates_from_window(self, point: Point) -> tuple[float, float]: ... def present(self) -> None: ... - def set_viewport(self, area: Optional[RectLike]) -> None: ... + def set_viewport(self, area: RectLike | None) -> None: ... def to_surface( - self, surface: Optional[Surface] = None, area: Optional[RectLike] = None + self, surface: Surface | None = None, area: RectLike | None = None ) -> Surface: ... @property def draw_blend_mode(self) -> int: ... @@ -84,7 +82,7 @@ class Texture: static: bool = False, streaming: bool = False, target: bool = False, - scale_quality: Optional[int] = None, + scale_quality: int | None = None, ) -> None: ... @property def alpha(self) -> int: ... @@ -108,10 +106,10 @@ class Texture: def from_surface(cls, renderer: Renderer, surface: Surface) -> Texture: ... def draw( self, - srcrect: Optional[RectLike] = None, - dstrect: Optional[RectLike] = None, + srcrect: RectLike | None = None, + dstrect: RectLike | None = None, angle: float = 0.0, - origin: Optional[Iterable[int]] = None, + origin: Iterable[int] | None = None, flip_x: bool = False, flip_y: bool = False, ) -> None: ... @@ -143,7 +141,7 @@ class Texture: p4_mod: ColorLike = (255, 255, 255, 255), ) -> None: ... def get_rect(self, **kwargs: Any) -> Rect: ... - def update(self, surface: Surface, area: Optional[RectLike] = None) -> None: ... + def update(self, surface: Surface, area: RectLike | None = None) -> None: ... @final class Image: diff --git a/buildconfig/stubs/pygame/_sdl2/controller_old.pyi b/buildconfig/stubs/pygame/_sdl2/controller_old.pyi index e1ae7a54ea..23375556ca 100644 --- a/buildconfig/stubs/pygame/_sdl2/controller_old.pyi +++ b/buildconfig/stubs/pygame/_sdl2/controller_old.pyi @@ -1,5 +1,4 @@ from collections.abc import Mapping -from typing import Optional from pygame.joystick import JoystickType @@ -11,7 +10,7 @@ def get_eventstate() -> bool: ... def update() -> None: ... def get_count() -> int: ... def is_controller(index: int) -> bool: ... -def name_forindex(index: int) -> Optional[str]: ... +def name_forindex(index: int) -> str | None: ... class Controller: def __init__(self, index: int) -> None: ... diff --git a/buildconfig/stubs/pygame/_sdl2/sdl2.pyi b/buildconfig/stubs/pygame/_sdl2/sdl2.pyi index 8a2b622d67..aab925d2ba 100644 --- a/buildconfig/stubs/pygame/_sdl2/sdl2.pyi +++ b/buildconfig/stubs/pygame/_sdl2/sdl2.pyi @@ -1,5 +1,3 @@ -from typing import Optional - INIT_TIMER: int INIT_AUDIO: int INIT_VIDEO: int @@ -11,6 +9,6 @@ INIT_NOPARACHUTE: int INIT_EVERYTHING: int class error(RuntimeError): - def __init__(self, message: Optional[str] = None) -> None: ... + def __init__(self, message: str | None = None) -> None: ... def init_subsystem(flags: int) -> None: ... diff --git a/buildconfig/stubs/pygame/_sdl2/touch.pyi b/buildconfig/stubs/pygame/_sdl2/touch.pyi index b91c964bab..1f33b2f0c3 100644 --- a/buildconfig/stubs/pygame/_sdl2/touch.pyi +++ b/buildconfig/stubs/pygame/_sdl2/touch.pyi @@ -1,4 +1,4 @@ -from typing import Optional, TypedDict +from typing import TypedDict class _FingerDict(TypedDict): id: int @@ -9,4 +9,4 @@ class _FingerDict(TypedDict): def get_num_devices() -> int: ... def get_device(index: int, /) -> int: ... def get_num_fingers(device_id: int, /) -> int: ... -def get_finger(touchid: int, index: int) -> Optional[_FingerDict]: ... +def get_finger(touchid: int, index: int) -> _FingerDict | None: ... diff --git a/buildconfig/stubs/pygame/_sdl2/video.pyi b/buildconfig/stubs/pygame/_sdl2/video.pyi index 540f139ddc..22861ffe3e 100644 --- a/buildconfig/stubs/pygame/_sdl2/video.pyi +++ b/buildconfig/stubs/pygame/_sdl2/video.pyi @@ -1,5 +1,5 @@ from collections.abc import Generator, Iterable -from typing import Any, Optional, Union +from typing import Any from pygame.color import Color from pygame.rect import Rect @@ -26,11 +26,11 @@ class RendererDriverInfo: max_texture_height: int def get_drivers() -> Generator[RendererDriverInfo, None, None]: ... -def get_grabbed_window() -> Optional[Window]: ... +def get_grabbed_window() -> Window | None: ... def messagebox( title: str, message: str, - window: Optional[Window] = None, + window: Window | None = None, info: bool = False, warn: bool = False, error: bool = False, @@ -48,7 +48,7 @@ class Texture: static: bool = False, streaming: bool = False, target: bool = False, - scale_quality: Optional[int] = None, + scale_quality: int | None = None, ) -> None: ... @staticmethod def from_surface(renderer: Renderer, surface: Surface) -> Texture: ... @@ -65,10 +65,10 @@ class Texture: def get_rect(self, **kwargs: Any) -> Rect: ... def draw( self, - srcrect: Optional[RectLike] = None, - dstrect: Optional[RectLike] = None, + srcrect: RectLike | None = None, + dstrect: RectLike | None = None, angle: float = 0.0, - origin: Optional[Iterable[int]] = None, + origin: Iterable[int] | None = None, flip_x: bool = False, flip_y: bool = False, ) -> None: ... @@ -99,20 +99,20 @@ class Texture: p3_mod: Iterable[int] = (255, 255, 255, 255), p4_mod: Iterable[int] = (255, 255, 255, 255), ) -> None: ... - def update(self, surface: Surface, area: Optional[RectLike] = None) -> None: ... + def update(self, surface: Surface, area: RectLike | None = None) -> None: ... class Image: def __init__( self, - texture_or_image: Union[Texture, Image], - srcrect: Optional[RectLike] = None, + texture_or_image: Texture | Image, + srcrect: RectLike | None = None, ) -> None: ... def get_rect(self) -> Rect: ... def draw( - self, srcrect: Optional[RectLike] = None, dstrect: Optional[RectLike] = None + self, srcrect: RectLike | None = None, dstrect: RectLike | None = None ) -> None: ... angle: float - origin: Optional[Iterable[float]] + origin: Iterable[float] | None flip_x: bool flip_y: bool alpha: float @@ -144,17 +144,17 @@ class Renderer: def clear(self) -> None: ... def present(self) -> None: ... def get_viewport(self) -> Rect: ... - def set_viewport(self, area: Optional[RectLike]) -> None: ... + def set_viewport(self, area: RectLike | None) -> None: ... logical_size: Iterable[int] def coordinates_to_window(self, point: Point) -> tuple[float, float]: ... def coordinates_from_window(self, point: Point) -> tuple[float, float]: ... scale: Iterable[float] - target: Optional[Texture] + target: Texture | None def blit( self, - source: Union[Texture, Image], - dest: Optional[RectLike] = None, - area: Optional[RectLike] = None, + source: Texture | Image, + dest: RectLike | None = None, + area: RectLike | None = None, special_flags: int = 0, ) -> Rect: ... def draw_line(self, p1: Point, p2: Point) -> None: ... @@ -166,7 +166,7 @@ class Renderer: def draw_quad(self, p1: Point, p2: Point, p3: Point, p4: Point) -> None: ... def fill_quad(self, p1: Point, p2: Point, p3: Point, p4: Point) -> None: ... def to_surface( - self, surface: Optional[Surface] = None, area: Optional[RectLike] = None + self, surface: Surface | None = None, area: RectLike | None = None ) -> Surface: ... @staticmethod def compose_custom_blend_mode( diff --git a/buildconfig/stubs/pygame/camera.pyi b/buildconfig/stubs/pygame/camera.pyi index 713fda223e..7c828894cb 100644 --- a/buildconfig/stubs/pygame/camera.pyi +++ b/buildconfig/stubs/pygame/camera.pyi @@ -1,11 +1,11 @@ from abc import ABC, abstractmethod -from typing import Literal, Optional, Union +from typing import Literal from pygame.surface import Surface from pygame.typing import IntPoint def get_backends() -> list[str]: ... -def init(backend: Optional[str] = None) -> None: ... +def init(backend: str | None = None) -> None: ... def quit() -> None: ... def list_cameras() -> list[str]: ... def colorspace( @@ -24,7 +24,7 @@ class AbstractCamera(ABC): @abstractmethod def query_image(self) -> bool: ... @abstractmethod - def get_image(self, dest_surf: Optional[Surface] = None) -> Surface: ... + def get_image(self, dest_surf: Surface | None = None) -> Surface: ... @abstractmethod def get_raw(self) -> bytes: ... # set_controls and get_controls are not a part of the AbstractCamera ABC, @@ -34,7 +34,7 @@ class AbstractCamera(ABC): class Camera(AbstractCamera): def __init__( self, - device: Union[str, int] = 0, + device: str | int = 0, size: IntPoint = (640, 480), format: str = "RGB", ) -> None: ... @@ -49,5 +49,5 @@ class Camera(AbstractCamera): ) -> tuple[bool, bool, int]: ... def get_size(self) -> tuple[int, int]: ... def query_image(self) -> bool: ... - def get_image(self, surface: Optional[Surface] = None) -> Surface: ... + def get_image(self, surface: Surface | None = None) -> Surface: ... def get_raw(self) -> bytes: ... diff --git a/buildconfig/stubs/pygame/color.pyi b/buildconfig/stubs/pygame/color.pyi index fc2fb489fb..436f2f6630 100644 --- a/buildconfig/stubs/pygame/color.pyi +++ b/buildconfig/stubs/pygame/color.pyi @@ -1,6 +1,6 @@ import sys from collections.abc import Collection, Iterator -from typing import Any, ClassVar, SupportsIndex, Union, overload +from typing import Any, ClassVar, SupportsIndex, overload from pygame.typing import ColorLike from typing_extensions import deprecated # added in 3.13 @@ -47,8 +47,8 @@ class Color(Collection[int]): def __index__(self) -> int: ... def __invert__(self) -> Color: ... def __contains__(self, other: int) -> bool: ... # type: ignore[override] - def __getattribute__(self, name: str) -> Union[Color, tuple[int, ...]]: ... - def __setattr__(self, name: str, value: Union[Color, tuple[int, ...]]) -> None: ... + def __getattribute__(self, name: str) -> Color | tuple[int, ...]: ... + def __setattr__(self, name: str, value: Color | tuple[int, ...]) -> None: ... @overload @classmethod def from_cmy(cls, object: tuple[float, float, float], /) -> Color: ... diff --git a/buildconfig/stubs/pygame/cursors.pyi b/buildconfig/stubs/pygame/cursors.pyi index d6005b9778..66e338c9e4 100644 --- a/buildconfig/stubs/pygame/cursors.pyi +++ b/buildconfig/stubs/pygame/cursors.pyi @@ -1,13 +1,13 @@ from collections.abc import Iterator -from typing import Any, Literal, Union, overload +from typing import Any, Literal, TypeAlias, overload from pygame.surface import Surface from pygame.typing import FileLike, IntPoint, SequenceLike -_Small_string = tuple[ +_SmallString: TypeAlias = tuple[ str, str, str, str, str, str, str, str, str, str, str, str, str, str, str, str ] -_Big_string = tuple[ +_BigString: TypeAlias = tuple[ str, str, str, @@ -40,11 +40,11 @@ broken_x: Cursor tri_left: Cursor tri_right: Cursor ball: Cursor -thickarrow_strings: _Big_string -sizer_x_strings: _Small_string -sizer_y_strings: _Big_string -sizer_xy_strings: _Small_string -textmarker_strings: _Small_string +thickarrow_strings: _BigString +sizer_x_strings: _SmallString +sizer_y_strings: _BigString +sizer_xy_strings: _SmallString +textmarker_strings: _SmallString def compile( strings: SequenceLike[str], @@ -79,11 +79,11 @@ class Cursor: def __len__(self) -> int: ... def __copy__(self) -> Cursor: ... def __hash__(self) -> int: ... - def __getitem__(self, index: int) -> Union[int, IntPoint, Surface]: ... + def __getitem__(self, index: int) -> int | IntPoint | Surface: ... def copy(self) -> Cursor: ... type: Literal["system", "color", "bitmap"] - data: Union[ - tuple[int], - tuple[tuple[int, int], tuple[int, int], tuple[int, ...], tuple[int, ...]], - tuple[IntPoint, Surface], - ] + data: ( + tuple[int] + | tuple[tuple[int, int], tuple[int, int], tuple[int, ...], tuple[int, ...]] + | tuple[IntPoint, Surface] + ) diff --git a/buildconfig/stubs/pygame/display.pyi b/buildconfig/stubs/pygame/display.pyi index 077c3e0c49..42429bae49 100644 --- a/buildconfig/stubs/pygame/display.pyi +++ b/buildconfig/stubs/pygame/display.pyi @@ -52,7 +52,7 @@ required). """ from collections.abc import Iterable -from typing import Literal, Optional, Union, overload +from typing import Literal, overload from pygame._sdl2 import Window from pygame.constants import FULLSCREEN @@ -285,7 +285,7 @@ def set_mode( on the major platforms this function was tested with. """ -def get_surface() -> Optional[Surface]: +def get_surface() -> Surface | None: """Get a reference to the currently set display surface. Return a reference to the currently set display Surface. If no display mode @@ -304,9 +304,9 @@ def flip() -> None: @overload def update() -> None: ... @overload -def update(rectangle: Optional[RectLike], /) -> None: ... +def update(rectangle: RectLike | None, /) -> None: ... @overload -def update(rectangles: Iterable[Optional[RectLike]], /) -> None: ... +def update(rectangles: Iterable[RectLike | None], /) -> None: ... @overload def update(x: float, y: float, w: float, h: float, /) -> None: ... @overload @@ -678,7 +678,7 @@ def set_icon(surface: Surface, /) -> None: the icon before the display mode is set. """ -def set_caption(title: str, icontitle: Optional[str] = None, /) -> None: +def set_caption(title: str, icontitle: str | None = None, /) -> None: """Set the current window caption. If the display has a window title, this function will change the name on the @@ -836,12 +836,12 @@ def get_desktop_refresh_rates() -> list[int]: def message_box( title: str, - message: Optional[str] = None, + message: str | None = None, message_type: Literal["info", "warn", "error"] = "info", - parent_window: Optional[Window] = None, + parent_window: Window | None = None, buttons: SequenceLike[str] = ("OK",), return_button: int = 0, - escape_button: Optional[int] = None, + escape_button: int | None = None, ) -> int: """Create a native GUI message box. diff --git a/buildconfig/stubs/pygame/draw.pyi b/buildconfig/stubs/pygame/draw.pyi index 439a52724f..eaa0ad52e7 100644 --- a/buildconfig/stubs/pygame/draw.pyi +++ b/buildconfig/stubs/pygame/draw.pyi @@ -28,7 +28,7 @@ object around the draw calls (see :func:`pygame.Surface.lock` and See the :mod:`pygame.gfxdraw` module for alternative draw methods. """ -from typing import Union, overload +from typing import overload from pygame.rect import Rect from pygame.surface import Surface @@ -569,9 +569,7 @@ def aalines( always raise a deprecation exception when used """ -def flood_fill( - surface: Surface, color: Union[ColorLike, Surface], start_pos: Point -) -> Rect: +def flood_fill(surface: Surface, color: ColorLike | Surface, start_pos: Point) -> Rect: """Fill an enclosed, same color area, on a surface. Replace the color of a cluster of connected same-color pixels, beginning diff --git a/buildconfig/stubs/pygame/event.pyi b/buildconfig/stubs/pygame/event.pyi index 4c0674731b..e72942c8b0 100644 --- a/buildconfig/stubs/pygame/event.pyi +++ b/buildconfig/stubs/pygame/event.pyi @@ -1,4 +1,4 @@ -from typing import Any, ClassVar, Optional, Union, final +from typing import Any, ClassVar, TypeAlias, final from pygame.typing import SequenceLike from typing_extensions import deprecated # added in 3.13 @@ -33,21 +33,21 @@ class Event(_GenericEvent): class EventType(_GenericEvent): pass -_EventTypes = Union[int, SequenceLike[int]] +_EventTypes: TypeAlias = int | SequenceLike[int] def pump() -> None: ... def get( - eventtype: Optional[_EventTypes] = None, + eventtype: _EventTypes | None = None, pump: bool = True, - exclude: Optional[_EventTypes] = None, + exclude: _EventTypes | None = None, ) -> list[Event]: ... def poll() -> Event: ... def wait(timeout: int = 0) -> Event: ... -def peek(eventtype: Optional[_EventTypes] = None, pump: bool = True) -> bool: ... -def clear(eventtype: Optional[_EventTypes] = None, pump: bool = True) -> None: ... +def peek(eventtype: _EventTypes | None = None, pump: bool = True) -> bool: ... +def clear(eventtype: _EventTypes | None = None, pump: bool = True) -> None: ... def event_name(type: int, /) -> str: ... -def set_blocked(type: Optional[_EventTypes], /) -> None: ... -def set_allowed(type: Optional[_EventTypes], /) -> None: ... +def set_blocked(type: _EventTypes | None, /) -> None: ... +def set_allowed(type: _EventTypes | None, /) -> None: ... def get_blocked(type: _EventTypes, /) -> bool: ... def set_grab(grab: bool, /) -> None: ... def get_grab() -> bool: ... diff --git a/buildconfig/stubs/pygame/font.pyi b/buildconfig/stubs/pygame/font.pyi index 99587d9b76..df5a89bb55 100644 --- a/buildconfig/stubs/pygame/font.pyi +++ b/buildconfig/stubs/pygame/font.pyi @@ -1,5 +1,5 @@ from collections.abc import Callable, Hashable, Iterable -from typing import Literal, Optional, Union +from typing import Literal from pygame.surface import Surface from pygame.typing import ColorLike, FileLike @@ -16,16 +16,16 @@ def get_sdl_ttf_version(linked: bool = True) -> tuple[int, int, int]: ... def get_default_font() -> str: ... def get_fonts() -> list[str]: ... def match_font( - name: Union[str, bytes, Iterable[Union[str, bytes]]], + name: str | bytes | Iterable[str | bytes], bold: Hashable = False, italic: Hashable = False, ) -> str: ... def SysFont( - name: Union[str, bytes, Iterable[Union[str, bytes]], None], + name: str | bytes | Iterable[str | bytes] | None, size: int, bold: Hashable = False, italic: Hashable = False, - constructor: Optional[Callable[[Optional[str], int, bool, bool], Font]] = None, + constructor: Callable[[str | None, int, bool, bool], Font] | None = None, ) -> Font: ... class Font: @@ -57,16 +57,16 @@ class Font: def point_size(self) -> int: ... @point_size.setter def point_size(self, value: int) -> None: ... - def __init__(self, filename: Optional[FileLike] = None, size: int = 20) -> None: ... + def __init__(self, filename: FileLike | None = None, size: int = 20) -> None: ... def render( self, - text: Union[str, bytes, None], + text: str | bytes | None, antialias: bool, color: ColorLike, - bgcolor: Optional[ColorLike] = None, + bgcolor: ColorLike | None = None, wraplength: int = 0, ) -> Surface: ... - def size(self, text: Union[str, bytes], /) -> tuple[int, int]: ... + def size(self, text: str | bytes, /) -> tuple[int, int]: ... def set_underline(self, value: bool, /) -> None: ... def get_underline(self) -> bool: ... def set_strikethrough(self, value: bool, /) -> None: ... @@ -74,9 +74,7 @@ class Font: def set_bold(self, value: bool, /) -> None: ... def get_bold(self) -> bool: ... def set_italic(self, value: bool, /) -> None: ... - def metrics( - self, text: Union[str, bytes], / - ) -> list[tuple[int, int, int, int, int]]: ... + def metrics(self, text: str | bytes, /) -> list[tuple[int, int, int, int, int]]: ... def get_italic(self) -> bool: ... def get_linesize(self) -> int: ... def set_linesize(self, linesize: int, /) -> None: ... diff --git a/buildconfig/stubs/pygame/freetype.pyi b/buildconfig/stubs/pygame/freetype.pyi index 3dd9faa4ff..63bf25487a 100644 --- a/buildconfig/stubs/pygame/freetype.pyi +++ b/buildconfig/stubs/pygame/freetype.pyi @@ -1,5 +1,5 @@ from collections.abc import Callable, Iterable -from typing import Any, Optional, Union +from typing import Any from pygame.color import Color from pygame.rect import Rect @@ -18,16 +18,16 @@ def get_cache_size() -> int: ... def get_default_resolution() -> int: ... def set_default_resolution(resolution: int = 0, /) -> None: ... def SysFont( - name: Union[str, bytes, Iterable[Union[str, bytes]], None], + name: str | bytes | Iterable[str | bytes] | None, size: int, bold: int = False, italic: int = False, - constructor: Optional[Callable[[Optional[str], int, bool, bool], Font]] = None, + constructor: Callable[[str | None, int, bool, bool], Font] | None = None, ) -> Font: ... def get_default_font() -> str: ... def get_fonts() -> list[str]: ... def match_font( - name: Union[str, bytes, Iterable[Union[str, bytes]]], + name: str | bytes | Iterable[str | bytes], bold: Any = False, italic: Any = False, ) -> str: ... @@ -41,9 +41,9 @@ STYLE_DEFAULT: int class Font: @property - def size(self) -> Union[float, tuple[float, float]]: ... + def size(self) -> float | tuple[float, float]: ... @size.setter - def size(self, value: Union[float, tuple[float, float]]) -> None: ... + def size(self, value: float | tuple[float, float]) -> None: ... @property def style(self) -> int: ... @style.setter @@ -134,7 +134,7 @@ class Font: def origin(self, value: bool) -> None: ... def __init__( self, - file: Optional[FileLike], + file: FileLike | None, size: float = 0, font_index: int = 0, resolution: int = 0, @@ -142,7 +142,7 @@ class Font: ) -> None: ... def get_rect( self, - text: Optional[str], + text: str | None, style: int = STYLE_DEFAULT, rotation: int = 0, size: float = 0, @@ -157,9 +157,9 @@ class Font: def get_sizes(self) -> list[tuple[int, int, int, float, float]]: ... def render( self, - text: Optional[str], - fgcolor: Optional[ColorLike] = None, - bgcolor: Optional[ColorLike] = None, + text: str | None, + fgcolor: ColorLike | None = None, + bgcolor: ColorLike | None = None, style: int = STYLE_DEFAULT, rotation: int = 0, size: float = 0, @@ -168,16 +168,16 @@ class Font: self, surf: Surface, dest: RectLike, - text: Optional[str], - fgcolor: Optional[ColorLike] = None, - bgcolor: Optional[ColorLike] = None, + text: str | None, + fgcolor: ColorLike | None = None, + bgcolor: ColorLike | None = None, style: int = STYLE_DEFAULT, rotation: int = 0, size: float = 0, ) -> Rect: ... def render_raw( self, - text: Optional[str], + text: str | None, style: int = STYLE_DEFAULT, rotation: int = 0, size: float = 0, @@ -186,8 +186,8 @@ class Font: def render_raw_to( self, array: Any, - text: Optional[str], - dest: Optional[RectLike] = None, + text: str | None, + dest: RectLike | None = None, style: int = STYLE_DEFAULT, rotation: int = 0, size: float = 0, diff --git a/buildconfig/stubs/pygame/geometry.pyi b/buildconfig/stubs/pygame/geometry.pyi index a9995cd795..04c8f7ef9d 100644 --- a/buildconfig/stubs/pygame/geometry.pyi +++ b/buildconfig/stubs/pygame/geometry.pyi @@ -1,5 +1,5 @@ from collections.abc import Callable -from typing import Protocol, Union, overload +from typing import Protocol, TypeAlias, overload from pygame import FRect, Rect from pygame.typing import Point, RectLike, SequenceLike @@ -8,22 +8,24 @@ class _HasCircleAttribute(Protocol): # An object that has a circle attribute that is either a circle, or a function # that returns a circle @property - def circle(self) -> Union[_CircleLike, Callable[[], _CircleLike]]: ... + def circle(self) -> _CircleLike | Callable[[], _CircleLike]: ... -_CircleLike = Union[ - Circle, tuple[Point, float], SequenceLike[float], _HasCircleAttribute -] +_CircleLike: TypeAlias = ( + Circle | tuple[Point, float] | SequenceLike[float] | _HasCircleAttribute +) class _HasLineAttribute(Protocol): # An object that has a line attribute that is either a line, or a function # that returns a line @property - def line(self) -> Union[_LineLike, Callable[[], _LineLike]]: ... + def line(self) -> _LineLike | Callable[[], _LineLike]: ... -_LineLike = Union[Line, SequenceLike[float], SequenceLike[Point], _HasLineAttribute] +_LineLike: TypeAlias = ( + Line | SequenceLike[float] | SequenceLike[Point] | _HasLineAttribute +) -_CanBeCollided = Union[Circle, Rect, FRect, Point] -_CanBeIntersected = Union[Circle] +_CanBeCollided: TypeAlias = Circle | Rect | FRect | Point +_CanBeIntersected: TypeAlias = Circle class Circle: @property diff --git a/buildconfig/stubs/pygame/image.pyi b/buildconfig/stubs/pygame/image.pyi index 01591a823f..a1a6f3a9bb 100644 --- a/buildconfig/stubs/pygame/image.pyi +++ b/buildconfig/stubs/pygame/image.pyi @@ -62,9 +62,8 @@ following formats. .. versionaddedold:: 1.8 Saving PNG and JPEG files. """ -from typing import Literal, Optional, Union +from typing import Literal, TypeAlias -from pygame.bufferproxy import BufferProxy from pygame.surface import Surface from pygame.typing import FileLike, IntPoint, Point from typing_extensions import ( @@ -72,11 +71,15 @@ from typing_extensions import ( deprecated, # added in 3.13 ) -_from_buffer_format = Literal["P", "RGB", "BGR", "BGRA", "RGBX", "RGBA", "ARGB"] -_to_bytes_format = Literal[ +_FromBufferFormat: TypeAlias = Literal[ + "P", "RGB", "BGR", "BGRA", "RGBX", "RGBA", "ARGB" +] +_ToBytesFormat: TypeAlias = Literal[ "P", "RGB", "RGBX", "RGBA", "ARGB", "BGRA", "ABGR", "RGBA_PREMULT", "ARGB_PREMULT" ] -_from_bytes_format = Literal["P", "RGB", "RGBX", "RGBA", "ARGB", "BGRA", "ABGR"] +_FromBytesFormat: TypeAlias = Literal[ + "P", "RGB", "RGBX", "RGBA", "ARGB", "BGRA", "ABGR" +] def load(file: FileLike, namehint: str = "") -> Surface: """Load new image from a file (or file-like object). @@ -177,7 +180,7 @@ def save(surface: Surface, file: FileLike, namehint: str = "") -> None: .. versionchanged:: 2.2.0 Now supports keyword arguments. """ -def get_sdl_image_version(linked: bool = True) -> Optional[tuple[int, int, int]]: +def get_sdl_image_version(linked: bool = True) -> tuple[int, int, int] | None: """Get version number of the SDL_Image library being used. If pygame is built with extended image formats, then this function will @@ -206,7 +209,7 @@ def get_extended() -> bool: @deprecated("since 2.3.0. Use `pygame.image.tobytes` instead") def tostring( surface: Surface, - format: _to_bytes_format, + format: _ToBytesFormat, flipped: bool = False, pitch: int = -1, ) -> bytes: @@ -220,7 +223,7 @@ def tostring( def tobytes( surface: Surface, - format: _to_bytes_format, + format: _ToBytesFormat, flipped: bool = False, pitch: int = -1, ) -> bytes: @@ -275,7 +278,7 @@ def tobytes( def fromstring( bytes: bytes, size: IntPoint, - format: _from_bytes_format, + format: _FromBytesFormat, flipped: bool = False, pitch: int = -1, ) -> Surface: @@ -290,7 +293,7 @@ def fromstring( def frombytes( bytes: bytes, size: IntPoint, - format: _from_bytes_format, + format: _FromBytesFormat, flipped: bool = False, pitch: int = -1, ) -> Surface: @@ -324,7 +327,7 @@ def frombytes( def frombuffer( buffer: Buffer, size: IntPoint, - format: _from_buffer_format, + format: _FromBufferFormat, pitch: int = -1, ) -> Surface: """Create a new Surface that shares data inside a bytes buffer. diff --git a/buildconfig/stubs/pygame/mask.pyi b/buildconfig/stubs/pygame/mask.pyi index 1f4ab58329..cf6a011274 100644 --- a/buildconfig/stubs/pygame/mask.pyi +++ b/buildconfig/stubs/pygame/mask.pyi @@ -1,5 +1,5 @@ import sys -from typing import Any, Optional, Union +from typing import Any from pygame.rect import Rect from pygame.surface import Surface @@ -11,7 +11,7 @@ def from_threshold( surface: Surface, color: ColorLike, threshold: ColorLike = (0, 0, 0, 255), - othersurface: Optional[Surface] = None, + othersurface: Surface | None = None, palette_colors: int = 1, ) -> Mask: ... @@ -23,7 +23,7 @@ class Mask: def get_rect(self, **kwargs: Any) -> Rect: ... # Dict type needs to be completed def get_at(self, pos: Point) -> int: ... def set_at(self, pos: Point, value: int = 1) -> None: ... - def overlap(self, other: Mask, offset: Point) -> Optional[tuple[int, int]]: ... + def overlap(self, other: Mask, offset: Point) -> tuple[int, int] | None: ... def overlap_area(self, other: Mask, offset: Point) -> int: ... def overlap_mask(self, other: Mask, offset: Point) -> Mask: ... def fill(self) -> None: ... @@ -39,7 +39,7 @@ class Mask: def convolve( self, other: Mask, - output: Optional[Mask] = None, + output: Mask | None = None, offset: Point = (0, 0), ) -> Mask: ... def connected_component(self, pos: Point = ...) -> Mask: ... @@ -47,13 +47,13 @@ class Mask: def get_bounding_rects(self) -> list[Rect]: ... def to_surface( self, - surface: Optional[Surface] = None, - setsurface: Optional[Surface] = None, - unsetsurface: Optional[Surface] = None, - setcolor: Optional[ColorLike] = (255, 255, 255, 255), - unsetcolor: Optional[ColorLike] = (0, 0, 0, 255), - dest: Union[RectLike, Point] = (0, 0), - area: Optional[RectLike] = None, + surface: Surface | None = None, + setsurface: Surface | None = None, + unsetsurface: Surface | None = None, + setcolor: ColorLike | None = (255, 255, 255, 255), + unsetcolor: ColorLike | None = (0, 0, 0, 255), + dest: RectLike | Point = (0, 0), + area: RectLike | None = None, ) -> Surface: ... if sys.version_info >= (3, 12): def __buffer__(self, flags: int, /) -> memoryview[int]: ... diff --git a/buildconfig/stubs/pygame/math.pyi b/buildconfig/stubs/pygame/math.pyi index 98b84ec340..6b37044853 100644 --- a/buildconfig/stubs/pygame/math.pyi +++ b/buildconfig/stubs/pygame/math.pyi @@ -4,10 +4,8 @@ from typing import ( ClassVar, Generic, Literal, - Optional, SupportsIndex, TypeVar, - Union, final, overload, ) @@ -29,20 +27,18 @@ class _GenericVector(Collection[float]): @overload def __setitem__(self, key: int, value: float) -> None: ... @overload - def __setitem__( - self, key: slice, value: Union[SequenceLike[float], _TVec] - ) -> None: ... + def __setitem__(self, key: slice, value: SequenceLike[float] | _TVec) -> None: ... @overload def __getitem__(self, i: SupportsIndex) -> float: ... @overload def __getitem__(self, s: slice) -> list[float]: ... def __iter__(self) -> VectorIterator: ... - def __add__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> _TVec: ... - def __radd__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> _TVec: ... - def __sub__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> _TVec: ... - def __rsub__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> _TVec: ... + def __add__(self: _TVec, other: SequenceLike[float] | _TVec) -> _TVec: ... + def __radd__(self: _TVec, other: SequenceLike[float] | _TVec) -> _TVec: ... + def __sub__(self: _TVec, other: SequenceLike[float] | _TVec) -> _TVec: ... + def __rsub__(self: _TVec, other: SequenceLike[float] | _TVec) -> _TVec: ... @overload - def __mul__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> float: ... + def __mul__(self: _TVec, other: SequenceLike[float] | _TVec) -> float: ... @overload def __mul__(self: _TVec, other: float) -> _TVec: ... def __rmul__(self: _TVec, other: float) -> _TVec: ... @@ -52,17 +48,17 @@ class _GenericVector(Collection[float]): def __neg__(self: _TVec) -> _TVec: ... def __pos__(self: _TVec) -> _TVec: ... def __bool__(self) -> bool: ... - def __iadd__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> _TVec: ... - def __isub__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> _TVec: ... + def __iadd__(self: _TVec, other: SequenceLike[float] | _TVec) -> _TVec: ... + def __isub__(self: _TVec, other: SequenceLike[float] | _TVec) -> _TVec: ... @overload - def __imul__(self: _TVec, other: Union[SequenceLike[float], _TVec]) -> float: ... + def __imul__(self: _TVec, other: SequenceLike[float] | _TVec) -> float: ... @overload def __imul__(self: _TVec, other: float) -> _TVec: ... def __copy__(self: _TVec) -> _TVec: ... def copy(self: _TVec) -> _TVec: ... def __safe_for_unpickling__(self) -> Literal[True]: ... def __contains__(self, other: float) -> bool: ... # type: ignore[override] - def dot(self: _TVec, other: Union[SequenceLike[float], _TVec], /) -> float: ... + def dot(self: _TVec, other: SequenceLike[float] | _TVec, /) -> float: ... def magnitude(self) -> float: ... def magnitude_squared(self) -> float: ... def length(self) -> float: ... @@ -71,32 +67,28 @@ class _GenericVector(Collection[float]): def normalize_ip(self) -> None: ... def is_normalized(self) -> bool: ... def scale_to_length(self, value: float, /) -> None: ... - def reflect(self: _TVec, other: Union[SequenceLike[float], _TVec], /) -> _TVec: ... - def reflect_ip( - self: _TVec, other: Union[SequenceLike[float], _TVec], / - ) -> None: ... - def distance_to( - self: _TVec, other: Union[SequenceLike[float], _TVec], / - ) -> float: ... + def reflect(self: _TVec, other: SequenceLike[float] | _TVec, /) -> _TVec: ... + def reflect_ip(self: _TVec, other: SequenceLike[float] | _TVec, /) -> None: ... + def distance_to(self: _TVec, other: SequenceLike[float] | _TVec, /) -> float: ... def distance_squared_to( - self: _TVec, other: Union[SequenceLike[float], _TVec], / + self: _TVec, other: SequenceLike[float] | _TVec, / ) -> float: ... def lerp( - self: _TVec, other: Union[SequenceLike[float], _TVec], value: float, / + self: _TVec, other: SequenceLike[float] | _TVec, value: float, / ) -> _TVec: ... def slerp( - self: _TVec, other: Union[SequenceLike[float], _TVec], value: float, / + self: _TVec, other: SequenceLike[float] | _TVec, value: float, / ) -> _TVec: ... def smoothstep( - self: _TVec, other: Union[SequenceLike[float], _TVec], value: float, / + self: _TVec, other: SequenceLike[float] | _TVec, value: float, / ) -> _TVec: ... def elementwise(self: _TVec) -> VectorElementwiseProxy[_TVec]: ... - def angle_to(self: _TVec, other: Union[SequenceLike[float], _TVec], /) -> float: ... + def angle_to(self: _TVec, other: SequenceLike[float] | _TVec, /) -> float: ... def move_towards( - self: _TVec, target: Union[SequenceLike[float], _TVec], max_distance: float, / + self: _TVec, target: SequenceLike[float] | _TVec, max_distance: float, / ) -> _TVec: ... def move_towards_ip( - self: _TVec, target: Union[SequenceLike[float], _TVec], max_distance: float, / + self: _TVec, target: SequenceLike[float] | _TVec, max_distance: float, / ) -> None: ... @overload def clamp_magnitude(self: _TVec, max_length: float, /) -> _TVec: ... @@ -108,8 +100,8 @@ class _GenericVector(Collection[float]): def clamp_magnitude_ip(self, max_length: float, /) -> None: ... @overload def clamp_magnitude_ip(self, min_length: float, max_length: float, /) -> None: ... - def project(self: _TVec, other: Union[SequenceLike[float], _TVec], /) -> _TVec: ... - def __round__(self: _TVec, ndigits: Optional[int] = None, /) -> _TVec: ... + def project(self: _TVec, other: SequenceLike[float] | _TVec, /) -> _TVec: ... + def __round__(self: _TVec, ndigits: int | None = None, /) -> _TVec: ... # VectorElementwiseProxy is a generic, it can be an elementwiseproxy object for # Vector2, Vector3 and vector subclass objects @@ -117,79 +109,79 @@ class _GenericVector(Collection[float]): class VectorElementwiseProxy(Generic[_TVec]): def __add__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __radd__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __sub__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __rsub__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __mul__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __rmul__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __truediv__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __rtruediv__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __floordiv__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __rfloordiv__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __mod__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __rmod__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> _TVec: ... def __pow__( self, - power: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + power: float | _TVec | VectorElementwiseProxy[_TVec], mod: None = None, ) -> _TVec: ... def __rpow__( self, - power: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + power: float | _TVec | VectorElementwiseProxy[_TVec], mod: None = None, ) -> _TVec: ... def __eq__(self, other: Any) -> bool: ... def __ne__(self, other: Any) -> bool: ... def __gt__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> bool: ... def __lt__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> bool: ... def __ge__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> bool: ... def __le__( self, - other: Union[float, _TVec, VectorElementwiseProxy[_TVec]], + other: float | _TVec | VectorElementwiseProxy[_TVec], ) -> bool: ... def __abs__(self) -> _TVec: ... def __neg__(self) -> _TVec: ... @@ -216,7 +208,7 @@ class Vector2(_GenericVector): @overload def __init__( self: _TVec, - x: Union[str, float, SequenceLike[float], _TVec] = 0, + x: str | float | SequenceLike[float] | _TVec = 0, ) -> None: ... @overload def __init__(self, x: float, y: float) -> None: ... @@ -227,13 +219,13 @@ class Vector2(_GenericVector): def rotate_rad_ip(self, angle: float, /) -> None: ... @deprecated("since 2.1.1. Use `pygame.Vector2.rotate_rad_ip` instead") def rotate_ip_rad(self, angle: float, /) -> None: ... - def cross(self: _TVec, other: Union[SequenceLike[float], _TVec], /) -> float: ... + def cross(self: _TVec, other: SequenceLike[float] | _TVec, /) -> float: ... def as_polar(self) -> tuple[float, float]: ... def from_polar(self, polar_value: SequenceLike[float], /) -> None: ... @overload def update( self: _TVec, - x: Union[str, float, SequenceLike[float], _TVec] = 0, + x: str | float | SequenceLike[float] | _TVec = 0, ) -> None: ... @overload def update(self, x: float = 0, y: float = 0) -> None: ... @@ -281,27 +273,27 @@ class Vector3(_GenericVector): @overload def __init__( self: _TVec, - x: Union[str, float, SequenceLike[float], _TVec] = 0, + x: str | float | SequenceLike[float] | _TVec = 0, ) -> None: ... @overload def __init__(self, x: float, y: float, z: float) -> None: ... def __reduce__(self: _TVec) -> tuple[type[_TVec], tuple[float, float, float]]: ... - def cross(self: _TVec, other: Union[SequenceLike[float], _TVec], /) -> _TVec: ... + def cross(self: _TVec, other: SequenceLike[float] | _TVec, /) -> _TVec: ... def rotate( - self: _TVec, angle: float, axis: Union[SequenceLike[float], _TVec], / + self: _TVec, angle: float, axis: SequenceLike[float] | _TVec, / ) -> _TVec: ... def rotate_rad( - self: _TVec, angle: float, axis: Union[SequenceLike[float], _TVec], / + self: _TVec, angle: float, axis: SequenceLike[float] | _TVec, / ) -> _TVec: ... def rotate_ip( - self: _TVec, angle: float, axis: Union[SequenceLike[float], _TVec], / + self: _TVec, angle: float, axis: SequenceLike[float] | _TVec, / ) -> None: ... def rotate_rad_ip( - self: _TVec, angle: float, axis: Union[SequenceLike[float], _TVec], / + self: _TVec, angle: float, axis: SequenceLike[float] | _TVec, / ) -> None: ... @deprecated("since 2.1.1. Use `pygame.Vector3.rotate_rad_ip` instead") def rotate_ip_rad( - self: _TVec, angle: float, axis: Union[SequenceLike[float], _TVec], / + self: _TVec, angle: float, axis: SequenceLike[float] | _TVec, / ) -> None: ... def rotate_x(self: _TVec, angle: float, /) -> _TVec: ... def rotate_x_rad(self: _TVec, angle: float, /) -> _TVec: ... @@ -326,7 +318,7 @@ class Vector3(_GenericVector): @overload def update( self: _TVec, - x: Union[str, float, SequenceLike[float], _TVec] = 0, + x: str | float | SequenceLike[float] | _TVec = 0, ) -> None: ... @overload def update(self, x: int, y: int, z: int) -> None: ... diff --git a/buildconfig/stubs/pygame/midi.pyi b/buildconfig/stubs/pygame/midi.pyi index 7d2fb62a45..b70433d8a6 100644 --- a/buildconfig/stubs/pygame/midi.pyi +++ b/buildconfig/stubs/pygame/midi.pyi @@ -1,5 +1,4 @@ from collections.abc import Iterable -from typing import Union from pygame.event import Event from pygame.typing import SequenceLike @@ -18,7 +17,7 @@ def get_default_input_id() -> int: ... def get_default_output_id() -> int: ... def get_device_info(an_id: int) -> tuple[str, str, int, int, int]: ... def midis2events( - midis: Iterable[SequenceLike[Union[SequenceLike[int], int]]], device_id: int + midis: Iterable[SequenceLike[SequenceLike[int] | int]], device_id: int ) -> list[Event]: ... def time() -> int: ... def frequency_to_midi(frequency: float) -> int: ... @@ -30,7 +29,7 @@ class Input: def __init__(self, device_id: int, buffer_size: int = 4096) -> None: ... def close(self) -> None: ... def poll(self) -> bool: ... - def read(self, num_events: int) -> list[list[Union[list[int], int]]]: ... + def read(self, num_events: int) -> list[list[list[int] | int]]: ... class Output: device_id: int @@ -46,11 +45,9 @@ class Output: def note_on(self, note: int, velocity: int, channel: int = 0) -> None: ... def set_instrument(self, instrument_id: int, channel: int = 0) -> None: ... def pitch_bend(self, value: int = 0, channel: int = 0) -> None: ... - def write( - self, data: Iterable[SequenceLike[Union[SequenceLike[int], int]]] - ) -> None: ... + def write(self, data: Iterable[SequenceLike[SequenceLike[int] | int]]) -> None: ... def write_short(self, status: int, data1: int = 0, data2: int = 0) -> None: ... - def write_sys_ex(self, when: int, msg: Union[list[int], str]) -> None: ... + def write_sys_ex(self, when: int, msg: list[int] | str) -> None: ... # keep in sync with midi.py __all__ = [ diff --git a/buildconfig/stubs/pygame/mixer.pyi b/buildconfig/stubs/pygame/mixer.pyi index 309ce5e1a2..d354a656d8 100644 --- a/buildconfig/stubs/pygame/mixer.pyi +++ b/buildconfig/stubs/pygame/mixer.pyi @@ -1,5 +1,5 @@ import sys -from typing import Any, Optional, Union, overload +from typing import Any, overload from pygame.event import Event from pygame.typing import FileLike @@ -19,7 +19,7 @@ def init( size: int = -16, channels: int = 2, buffer: int = 512, - devicename: Optional[str] = None, + devicename: str | None = None, allowedchanges: int = 5, ) -> None: ... def pre_init( @@ -27,7 +27,7 @@ def pre_init( size: int = -16, channels: int = 2, buffer: int = 512, - devicename: Optional[str] = None, + devicename: str | None = None, allowedchanges: int = 5, ) -> None: ... def quit() -> None: ... @@ -41,8 +41,8 @@ def set_num_channels(count: int, /) -> None: ... def get_num_channels() -> int: ... def set_reserved(count: int, /) -> int: ... def find_channel(force: bool = False) -> Channel: ... -def set_soundfont(paths: Optional[str] = None, /) -> None: ... -def get_soundfont() -> Optional[str]: ... +def set_soundfont(paths: str | None = None, /) -> None: ... +def get_soundfont() -> str | None: ... def get_busy() -> bool: ... def get_sdl_mixer_version(linked: bool = True) -> tuple[int, int, int]: ... @@ -101,7 +101,7 @@ class Channel: def get_busy(self) -> bool: ... def get_sound(self) -> Sound: ... def get_queue(self) -> Sound: ... - def set_endevent(self, type: Union[int, Event] = 0, /) -> None: ... + def set_endevent(self, type: int | Event = 0, /) -> None: ... def get_endevent(self) -> int: ... @deprecated("Use `Sound` instead (SoundType is an old alias)") diff --git a/buildconfig/stubs/pygame/pixelarray.pyi b/buildconfig/stubs/pygame/pixelarray.pyi index 21b7dc41fe..f889d589f6 100644 --- a/buildconfig/stubs/pygame/pixelarray.pyi +++ b/buildconfig/stubs/pygame/pixelarray.pyi @@ -1,18 +1,12 @@ import sys -from typing import Any, Union, overload +from types import EllipsisType +from typing import Any, TypeAlias, overload from pygame.color import Color from pygame.surface import Surface from pygame.typing import SequenceLike -# 'ellipsis' existed in typeshed pre 3.10, now we use EllipsisType which is -# the modern standard library equivalent. -if sys.version_info >= (3, 10): - from types import EllipsisType -else: - EllipsisType = ellipsis - -_PixelColor = Union[int, Color, tuple[int, int, int], tuple[int, int, int, int]] +_PixelColor: TypeAlias = int | Color | tuple[int, int, int] | tuple[int, int, int, int] class PixelArray: @property @@ -39,11 +33,11 @@ class PixelArray: # if indexing into a 2D PixelArray, a 1D PixelArray will be returned # if indexing into a 1D PixelArray, an int will be returned @overload - def __getitem__(self, index: int) -> Union[PixelArray, int]: ... + def __getitem__(self, index: int) -> PixelArray | int: ... # complicated, but I'm pretty sure this is guaranteed to return a PixelArray or None # will only return None if the slice start and end are the same @overload - def __getitem__(self, index_range: slice) -> Union[PixelArray, None]: ... + def __getitem__(self, index_range: slice) -> PixelArray | None: ... # only valid for a 2D PixelArray @overload def __getitem__(self, indices: tuple[int, int]) -> int: ... diff --git a/buildconfig/stubs/pygame/pixelcopy.pyi b/buildconfig/stubs/pygame/pixelcopy.pyi index f28a208586..39f8def013 100644 --- a/buildconfig/stubs/pygame/pixelcopy.pyi +++ b/buildconfig/stubs/pygame/pixelcopy.pyi @@ -1,14 +1,14 @@ -from typing import Literal +from typing import Literal, TypeAlias import numpy from pygame.surface import Surface -_kind = Literal["P", "p", "R", "r", "G", "g", "B", "b", "A", "a", "C", "c"] +_Kind: TypeAlias = Literal["P", "p", "R", "r", "G", "g", "B", "b", "A", "a", "C", "c"] def surface_to_array( array: numpy.ndarray, surface: Surface, - kind: _kind = "P", + kind: _Kind = "P", opaque: int = 255, clear: int = 0, ) -> None: ... diff --git a/buildconfig/stubs/pygame/rect.pyi b/buildconfig/stubs/pygame/rect.pyi index 9edab76300..611e4f645b 100644 --- a/buildconfig/stubs/pygame/rect.pyi +++ b/buildconfig/stubs/pygame/rect.pyi @@ -3,10 +3,8 @@ from collections.abc import Callable, Collection, Iterator from typing import ( ClassVar, Literal, - Optional, SupportsIndex, TypeVar, - Union, overload, ) @@ -18,12 +16,7 @@ if sys.version_info >= (3, 11): else: from typing_extensions import Self -# 'ellipsis' existed in typeshed pre 3.10, now we use EllipsisType which is -# the modern standard library equivalent. -if sys.version_info >= (3, 10): - from types import EllipsisType -else: - EllipsisType = ellipsis +from types import EllipsisType _N = TypeVar("_N", int, float) _K = TypeVar("_K") @@ -145,12 +138,12 @@ class _GenericRect(Collection[_N]): @overload def __getitem__(self, i: SupportsIndex) -> _N: ... @overload - def __getitem__(self, s: Union[slice, EllipsisType]) -> list[_N]: ... + def __getitem__(self, s: slice | EllipsisType) -> list[_N]: ... @overload def __setitem__(self, key: int, value: float) -> None: ... @overload def __setitem__( - self, key: Union[slice, EllipsisType], value: Union[float, RectLike] + self, key: slice | EllipsisType, value: float | RectLike ) -> None: ... def __copy__(self) -> Self: ... def copy(self) -> Self: ... @@ -162,7 +155,7 @@ class _GenericRect(Collection[_N]): def move_ip(self, x: float, y: float, /) -> None: ... @overload def move_ip(self, move_by: Point, /) -> None: ... - def move_to(self, **kwargs: Union[float, Point]) -> Self: ... + def move_to(self, **kwargs: float | Point) -> Self: ... @overload def inflate(self, x: float, y: float, /) -> Self: ... @overload @@ -212,15 +205,15 @@ class _GenericRect(Collection[_N]): @overload def clipline( self, x1: float, x2: float, x3: float, x4: float, / - ) -> Union[tuple[tuple[_N, _N], tuple[_N, _N]], tuple[()]]: ... + ) -> tuple[tuple[_N, _N], tuple[_N, _N]] | tuple[()]: ... @overload def clipline( self, first_point: Point, second_point: Point, / - ) -> Union[tuple[tuple[_N, _N], tuple[_N, _N]], tuple[()]]: ... + ) -> tuple[tuple[_N, _N], tuple[_N, _N]] | tuple[()]: ... @overload def clipline( self, rect_arg: RectLike, / - ) -> Union[tuple[tuple[_N, _N], tuple[_N, _N]], tuple[()]]: ... + ) -> tuple[tuple[_N, _N], tuple[_N, _N]] | tuple[()]: ... @overload def union(self, rect: RectLike, /) -> Self: ... @overload @@ -248,7 +241,7 @@ class _GenericRect(Collection[_N]): @overload def fit(self, left: float, top: float, width: float, height: float, /) -> Self: ... def normalize(self) -> None: ... - def __contains__(self, rect: Union[RectLike, _N], /) -> bool: ... # type: ignore[override] + def __contains__(self, rect: RectLike | _N, /) -> bool: ... # type: ignore[override] @overload def contains(self, rect: RectLike, /) -> bool: ... @overload @@ -276,21 +269,21 @@ class _GenericRect(Collection[_N]): self, rect_list: SequenceLike[_RectTypeCompatible_co], / ) -> list[int]: ... def collideobjectsall( - self, objects: SequenceLike[_T], key: Optional[Callable[[_T], RectLike]] = None + self, objects: SequenceLike[_T], key: Callable[[_T], RectLike] | None = None ) -> list[_T]: ... def collideobjects( - self, objects: SequenceLike[_T], key: Optional[Callable[[_T], RectLike]] = None - ) -> Optional[_T]: ... + self, objects: SequenceLike[_T], key: Callable[[_T], RectLike] | None = None + ) -> _T | None: ... @overload def collidedict( self, rect_dict: dict[_RectTypeCompatible_co, _V], values: Literal[False] = False, - ) -> Optional[tuple[_RectTypeCompatible_co, _V]]: ... + ) -> tuple[_RectTypeCompatible_co, _V] | None: ... @overload def collidedict( self, rect_dict: dict[_K, _RectTypeCompatible_co], values: Literal[True] - ) -> Optional[tuple[_K, _RectTypeCompatible_co]]: ... + ) -> tuple[_K, _RectTypeCompatible_co] | None: ... @overload def collidedictall( self, diff --git a/buildconfig/stubs/pygame/rwobject.pyi b/buildconfig/stubs/pygame/rwobject.pyi index 11eb417a5b..f686414e1d 100644 --- a/buildconfig/stubs/pygame/rwobject.pyi +++ b/buildconfig/stubs/pygame/rwobject.pyi @@ -1,18 +1,18 @@ -from typing import Any, Optional, overload +from typing import Any, overload from pygame.typing import _PathLike def encode_string( - obj: Optional[_PathLike], - encoding: Optional[str] = "unicode_escape", - errors: Optional[str] = "backslashreplace", - etype: Optional[type[Exception]] = UnicodeEncodeError, + obj: _PathLike | None, + encoding: str | None = "unicode_escape", + errors: str | None = "backslashreplace", + etype: type[Exception] | None = UnicodeEncodeError, ) -> bytes: ... @overload def encode_file_path( - obj: Optional[_PathLike], etype: Optional[type[Exception]] = UnicodeEncodeError + obj: _PathLike | None, etype: type[Exception] | None = UnicodeEncodeError ) -> bytes: ... @overload def encode_file_path( - obj: Any, etype: Optional[type[Exception]] = UnicodeEncodeError + obj: Any, etype: type[Exception] | None = UnicodeEncodeError ) -> bytes: ... diff --git a/buildconfig/stubs/pygame/scrap.pyi b/buildconfig/stubs/pygame/scrap.pyi index b75098e3f2..b49a0b82ca 100644 --- a/buildconfig/stubs/pygame/scrap.pyi +++ b/buildconfig/stubs/pygame/scrap.pyi @@ -1,5 +1,3 @@ -from typing import Optional - from typing_extensions import ( Buffer, # added in 3.12, deprecated, # added in 3.13 @@ -10,7 +8,7 @@ def init() -> None: ... @deprecated("since 2.2.0. Use the new API instead, which doesn't require scrap init") def get_init() -> bool: ... @deprecated("since 2.2.0. Use the new API instead: `pygame.scrap.get_text`") -def get(data_type: str, /) -> Optional[bytes]: ... +def get(data_type: str, /) -> bytes | None: ... @deprecated("since 2.2.0. Use the new API instead, which only supports strings") def get_types() -> list[str]: ... @deprecated("since 2.2.0. Use the new API instead: `pygame.scrap.put_text`") diff --git a/buildconfig/stubs/pygame/sprite.pyi b/buildconfig/stubs/pygame/sprite.pyi index 148c54dea2..e78292f2d7 100644 --- a/buildconfig/stubs/pygame/sprite.pyi +++ b/buildconfig/stubs/pygame/sprite.pyi @@ -4,11 +4,10 @@ from collections.abc import Callable, Iterable, Iterator from typing import ( Any, Generic, - Optional, Protocol, SupportsFloat, + TypeAlias, TypeVar, - Union, ) # use typing_extensions for compatibility with older Python versions @@ -30,12 +29,12 @@ from pygame.typing import Point, RectLike # Some sprite functions only need objects with certain attributes, not always a sprite class _HasRect(Protocol): @property - def rect(self) -> Optional[Union[FRect, Rect]]: ... + def rect(self) -> FRect | Rect | None: ... # image in addition to rect class _HasImageAndRect(_HasRect, Protocol): @property - def image(self) -> Optional[Surface]: ... + def image(self) -> Surface | None: ... # mask in addition to rect class _HasMaskAndRect(_HasRect, Protocol): @@ -44,13 +43,13 @@ class _HasMaskAndRect(_HasRect, Protocol): class Sprite(_HasImageAndRect): @property - def image(self) -> Optional[Surface]: ... + def image(self) -> Surface | None: ... @image.setter - def image(self, value: Optional[Surface]) -> None: ... + def image(self, value: Surface | None) -> None: ... @property - def rect(self) -> Optional[Union[FRect, Rect]]: ... + def rect(self) -> FRect | Rect | None: ... @rect.setter - def rect(self, value: Optional[Union[FRect, Rect]]) -> None: ... + def rect(self, value: FRect | Rect | None) -> None: ... @property def layer(self) -> int: ... @layer.setter @@ -68,7 +67,7 @@ class Sprite(_HasImageAndRect): class DirtySprite(Sprite): dirty: int blendmode: int - source_rect: Union[FRect, Rect] + source_rect: FRect | Rect visible: int _layer: int @@ -76,19 +75,19 @@ _SpriteT = TypeVar("_SpriteT", bound=Sprite) _SpriteT2 = TypeVar("_SpriteT2", bound=Sprite) _DirtySpriteT = TypeVar("_DirtySpriteT", bound=DirtySprite) -_GroupOrGroups = Union[AbstractGroup[_SpriteT], Iterable[_GroupOrGroups[_SpriteT]]] -_SpriteOrSprites = Union[_SpriteT, Iterable[_SpriteOrSprites[_SpriteT]]] +_GroupOrGroups: TypeAlias = AbstractGroup[_SpriteT] | Iterable[_GroupOrGroups[_SpriteT]] +_SpriteOrSprites: TypeAlias = _SpriteT | Iterable[_SpriteOrSprites[_SpriteT]] class AbstractGroup(Generic[_SpriteT]): - spritedict: dict[_SpriteT, Optional[Union[FRect, Rect]]] - lostsprites: list[Union[FRect, Rect]] + spritedict: dict[_SpriteT, FRect | Rect | None] + lostsprites: list[FRect | Rect] def __class_getitem__(cls, item: Any, /) -> types.GenericAlias: ... def __init__(self) -> None: ... def __len__(self) -> int: ... def __iter__(self) -> Iterator[_SpriteT]: ... def __bool__(self) -> bool: ... def __contains__(self, item: Any) -> bool: ... - def add_internal(self, sprite: _SpriteT, layer: Optional[int] = None) -> None: ... + def add_internal(self, sprite: _SpriteT, layer: int | None = None) -> None: ... def remove_internal(self, sprite: _SpriteT) -> None: ... def has_internal(self, sprite: _SpriteT) -> bool: ... def copy(self) -> Self: ... @@ -98,12 +97,12 @@ class AbstractGroup(Generic[_SpriteT]): def has(self, *sprites: _SpriteOrSprites[_SpriteT]) -> bool: ... def update(self, *args: Any, **kwargs: Any) -> None: ... def draw( - self, surface: Surface, bgd: Optional[Surface] = None, special_flags: int = 0 - ) -> list[Union[FRect, Rect]]: ... + self, surface: Surface, bgd: Surface | None = None, special_flags: int = 0 + ) -> list[FRect | Rect]: ... def clear( self, surface: Surface, - bgd: Union[Surface, Callable[[Surface, Union[FRect, Rect]], Any]], + bgd: Surface | Callable[[Surface, FRect | Rect], Any], ) -> None: ... def empty(self) -> None: ... @@ -143,14 +142,14 @@ class LayeredDirty(LayeredUpdates[_DirtySpriteT]): def draw( self, surface: Surface, - bgd: Optional[Surface] = None, - special_flags: Optional[int] = None, - ) -> list[Union[FRect, Rect]]: ... + bgd: Surface | None = None, + special_flags: int | None = None, + ) -> list[FRect | Rect]: ... # clear breaks Liskov substitution principle in code def clear(self, surface: Surface, bgd: Surface) -> None: ... # type: ignore[override] def repaint_rect(self, screen_rect: RectLike) -> None: ... - def set_clip(self, screen_rect: Optional[RectLike] = None) -> None: ... - def get_clip(self) -> Union[FRect, Rect]: ... + def set_clip(self, screen_rect: RectLike | None = None) -> None: ... + def get_clip(self) -> FRect | Rect: ... def set_timing_threshold(self, time_ms: SupportsFloat) -> None: ... @deprecated( "since 2.1.1. Use `pygame.sprite.LayeredDirty.set_timing_threshold` instead" @@ -158,8 +157,8 @@ class LayeredDirty(LayeredUpdates[_DirtySpriteT]): def set_timing_treshold(self, time_ms: SupportsFloat) -> None: ... class GroupSingle(AbstractGroup[_SpriteT]): - sprite: Optional[_SpriteT] - def __init__(self, sprite: Optional[_SpriteT] = None) -> None: ... + sprite: _SpriteT | None + def __init__(self, sprite: _SpriteT | None = None) -> None: ... def collide_rect(left: _HasRect, right: _HasRect) -> bool: ... @@ -169,7 +168,7 @@ class collide_rect_ratio: def __call__(self, left: _HasRect, right: _HasRect) -> bool: ... # Must have rect attribute, may optionally have radius attribute -_SupportsCollideCircle = _HasRect +_SupportsCollideCircle: TypeAlias = _HasRect def collide_circle( left: _SupportsCollideCircle, right: _SupportsCollideCircle @@ -184,11 +183,11 @@ class collide_circle_ratio: # Arguments to collide_mask must either have mask or have image attribute, in # addition to mandatorily having a rect attribute -_SupportsCollideMask = Union[_HasImageAndRect, _HasMaskAndRect] +_SupportsCollideMask: TypeAlias = _HasImageAndRect | _HasMaskAndRect def collide_mask( left: _SupportsCollideMask, right: _SupportsCollideMask -) -> Optional[tuple[int, int]]: ... +) -> tuple[int, int] | None: ... _HasRectT = TypeVar("_HasRectT", bound=_HasRect) @@ -196,17 +195,17 @@ def spritecollide( sprite: _HasRectT, group: AbstractGroup[_SpriteT], dokill: bool, - collided: Optional[Callable[[_HasRectT, _SpriteT], bool]] = None, + collided: Callable[[_HasRectT, _SpriteT], bool] | None = None, ) -> list[_SpriteT]: ... def groupcollide( groupa: AbstractGroup[_SpriteT], groupb: AbstractGroup[_SpriteT2], dokilla: bool, dokillb: bool, - collided: Optional[Callable[[_SpriteT, _SpriteT2], bool]] = None, + collided: Callable[[_SpriteT, _SpriteT2], bool] | None = None, ) -> dict[_SpriteT, list[_SpriteT2]]: ... def spritecollideany( sprite: _HasRectT, group: AbstractGroup[_SpriteT], - collided: Optional[Callable[[_HasRectT, _SpriteT], bool]] = None, -) -> Optional[_SpriteT]: ... + collided: Callable[[_HasRectT, _SpriteT], bool] | None = None, +) -> _SpriteT | None: ... diff --git a/buildconfig/stubs/pygame/surface.pyi b/buildconfig/stubs/pygame/surface.pyi index f3c93bdba0..b7bef9d3ea 100644 --- a/buildconfig/stubs/pygame/surface.pyi +++ b/buildconfig/stubs/pygame/surface.pyi @@ -1,5 +1,5 @@ from collections.abc import Iterable -from typing import Any, Literal, Optional, Union, overload +from typing import Any, Literal, TypeAlias, overload from pygame.bufferproxy import BufferProxy from pygame.color import Color @@ -12,7 +12,7 @@ from pygame.typing import ( ) from typing_extensions import deprecated # added in 3.13 -_ViewKind = Literal[ +_ViewKind: TypeAlias = Literal[ "0", "1", "2", @@ -125,7 +125,7 @@ class Surface: size: Point, flags: int = 0, depth: int = 0, - masks: Optional[ColorLike] = None, + masks: ColorLike | None = None, ) -> None: ... @overload def __init__( @@ -139,8 +139,8 @@ class Surface: def blit( self, source: Surface, - dest: Union[Point, RectLike] = (0, 0), - area: Optional[RectLike] = None, + dest: Point | RectLike = (0, 0), + area: RectLike | None = None, special_flags: int = 0, ) -> Rect: """Draw another Surface onto this one. @@ -196,14 +196,14 @@ class Surface: def blits( self, blit_sequence: Iterable[ - Union[ - tuple[Surface, Union[Point, RectLike]], - tuple[Surface, Union[Point, RectLike], Union[RectLike, int]], - tuple[Surface, Union[Point, RectLike], RectLike, int], - ] + ( + tuple[Surface, Point | RectLike] + | tuple[Surface, Point | RectLike, RectLike | int] + | tuple[Surface, Point | RectLike, RectLike, int] + ) ], - doreturn: Union[int, bool] = 1, - ) -> Union[list[Rect], None]: + doreturn: int | bool = 1, + ) -> list[Rect] | None: """Draw many Surfaces onto this Surface at their corresponding location. The ``blits`` method efficiently draws a sequence of Surfaces onto this ``Surface``. @@ -255,7 +255,7 @@ class Surface: def fblits( self, - blit_sequence: Iterable[tuple[Surface, Union[Point, RectLike]]], + blit_sequence: Iterable[tuple[Surface, Point | RectLike]], special_flags: int = 0, /, ) -> None: @@ -348,7 +348,7 @@ class Surface: def fill( self, color: ColorLike, - rect: Optional[RectLike] = None, + rect: RectLike | None = None, special_flags: int = 0, ) -> Rect: """Fill Surface with a solid color. @@ -418,7 +418,7 @@ class Surface: will be slower to modify, but quicker to blit as a source. """ - def get_colorkey(self) -> Optional[tuple[int, int, int, int]]: + def get_colorkey(self) -> tuple[int, int, int, int] | None: """Get the current transparent colorkey. Return the current colorkey value for the Surface. If the colorkey is not @@ -449,7 +449,7 @@ class Surface: will be slower to modify, but quicker to blit as a source. """ - def get_alpha(self) -> Optional[int]: + def get_alpha(self) -> int | None: """Get the current Surface transparency value. Return the current alpha value for the Surface. @@ -649,7 +649,7 @@ class Surface: and pixel formats. """ - def set_clip(self, rect: Optional[RectLike], /) -> None: + def set_clip(self, rect: RectLike | None, /) -> None: """Set the current clipping area of the Surface. Each Surface has an active clipping area. This is a rectangle that diff --git a/buildconfig/stubs/pygame/system.pyi b/buildconfig/stubs/pygame/system.pyi index 6c529297fb..36e909af74 100644 --- a/buildconfig/stubs/pygame/system.pyi +++ b/buildconfig/stubs/pygame/system.pyi @@ -3,7 +3,7 @@ .. versionadded:: 2.2.0 """ -from typing import Optional, TypedDict +from typing import TypedDict from pygame._data_classes import PowerState @@ -25,7 +25,7 @@ class _InstructionSets(TypedDict): class _Locale(TypedDict): language: str - country: Optional[str] + country: str | None def get_cpu_instruction_sets() -> _InstructionSets: """Get the information of CPU instruction sets. @@ -144,7 +144,7 @@ def get_pref_locales() -> list[_Locale]: .. versionadded:: 2.2.0 """ -def get_power_state() -> Optional[PowerState]: +def get_power_state() -> PowerState | None: """Get the current power supply state. **Experimental:** feature available for testing and feedback. diff --git a/buildconfig/stubs/pygame/time.pyi b/buildconfig/stubs/pygame/time.pyi index 930bb5e899..1c4ba7e46c 100644 --- a/buildconfig/stubs/pygame/time.pyi +++ b/buildconfig/stubs/pygame/time.pyi @@ -5,7 +5,7 @@ constant frame rate. Times in pygame-ce are represented in milliseconds (1/1000 of a second). """ -from typing import Union, final +from typing import final from pygame.event import Event @@ -37,7 +37,7 @@ def delay(milliseconds: int, /) -> int: This returns the actual number of milliseconds used. """ -def set_timer(event: Union[int, Event], millis: int, loops: int = 0) -> None: +def set_timer(event: int | Event, millis: int, loops: int = 0) -> None: """Repeatedly create an event on the event queue. Set an event to appear on the event queue every given number of milliseconds. diff --git a/buildconfig/stubs/pygame/transform.pyi b/buildconfig/stubs/pygame/transform.pyi index 8a063ac408..63d95c352b 100644 --- a/buildconfig/stubs/pygame/transform.pyi +++ b/buildconfig/stubs/pygame/transform.pyi @@ -15,7 +15,7 @@ Instead, always begin with the original image and scale to the desired size.) .. versionchangedold:: 2.0.2 transform functions now support keyword arguments. """ -from typing import Literal, Optional, Union +from typing import Literal from pygame.surface import Surface from pygame.typing import ColorLike, Point, RectLike, SequenceLike @@ -32,7 +32,7 @@ def flip(surface: Surface, flip_x: bool, flip_y: bool) -> Surface: def scale( surface: Surface, size: Point, - dest_surface: Optional[Surface] = None, + dest_surface: Surface | None = None, ) -> Surface: """Resize to new resolution. @@ -50,8 +50,8 @@ def scale( def scale_by( surface: Surface, - factor: Union[float, SequenceLike[float]], - dest_surface: Optional[Surface] = None, + factor: float | SequenceLike[float], + dest_surface: Surface | None = None, ) -> Surface: """Resize to new resolution, using scalar(s). @@ -92,7 +92,7 @@ def rotozoom(surface: Surface, angle: float, scale: float) -> Surface: A negative rotation angle will rotate clockwise. """ -def scale2x(surface: Surface, dest_surface: Optional[Surface] = None) -> Surface: +def scale2x(surface: Surface, dest_surface: Surface | None = None) -> Surface: """Specialized image doubler. This will return a new image that is double the size of the original. It @@ -111,7 +111,7 @@ def scale2x(surface: Surface, dest_surface: Optional[Surface] = None) -> Surface def smoothscale( surface: Surface, size: Point, - dest_surface: Optional[Surface] = None, + dest_surface: Surface | None = None, ) -> Surface: """Scale a surface to an arbitrary size smoothly. @@ -134,8 +134,8 @@ def smoothscale( def smoothscale_by( surface: Surface, - factor: Union[float, SequenceLike[float]], - dest_surface: Optional[Surface] = None, + factor: float | SequenceLike[float], + dest_surface: Surface | None = None, ) -> Surface: """Resize to new resolution, using scalar(s). @@ -192,7 +192,7 @@ def chop(surface: Surface, rect: RectLike) -> Surface: rect, you can blit with a rect to a new surface or copy a subsurface. """ -def laplacian(surface: Surface, dest_surface: Optional[Surface] = None) -> Surface: +def laplacian(surface: Surface, dest_surface: Surface | None = None) -> Surface: """Find edges in a surface. Finds the edges in a surface using the laplacian algorithm. @@ -208,7 +208,7 @@ def box_blur( surface: Surface, radius: int, repeat_edge_pixels: bool = True, - dest_surface: Optional[Surface] = None, + dest_surface: Surface | None = None, ) -> Surface: """Blur a surface using box blur. @@ -234,7 +234,7 @@ def gaussian_blur( surface: Surface, radius: int, repeat_edge_pixels: bool = True, - dest_surface: Optional[Surface] = None, + dest_surface: Surface | None = None, ) -> Surface: """Blur a surface using gaussian blur. @@ -263,8 +263,8 @@ def gaussian_blur( def average_surfaces( surfaces: SequenceLike[Surface], - dest_surface: Optional[Surface] = None, - palette_colors: Union[bool, int] = 1, + dest_surface: Surface | None = None, + palette_colors: bool | int = 1, ) -> Surface: """Find the average surface from many surfaces. @@ -287,7 +287,7 @@ def average_surfaces( """ def average_color( - surface: Surface, rect: Optional[RectLike] = None, consider_alpha: bool = False + surface: Surface, rect: RectLike | None = None, consider_alpha: bool = False ) -> tuple[int, int, int, int]: """Finds the average color of a surface. @@ -299,7 +299,7 @@ def average_color( .. versionaddedold:: 2.1.2 ``consider_alpha`` argument """ -def invert(surface: Surface, dest_surface: Optional[Surface] = None) -> Surface: +def invert(surface: Surface, dest_surface: Surface | None = None) -> Surface: """Inverts the RGB elements of a surface. Inverts each RGB pixel contained within the Surface, does not affect alpha channel. @@ -311,7 +311,7 @@ def invert(surface: Surface, dest_surface: Optional[Surface] = None) -> Surface: .. versionadded:: 2.2.0 """ -def grayscale(surface: Surface, dest_surface: Optional[Surface] = None) -> Surface: +def grayscale(surface: Surface, dest_surface: Surface | None = None) -> Surface: """Grayscale a surface. Returns a grayscaled version of the original surface using the luminosity formula which weights red, green and blue according to their wavelengths. @@ -330,7 +330,7 @@ def grayscale(surface: Surface, dest_surface: Optional[Surface] = None) -> Surfa def solid_overlay( surface: Surface, color: ColorLike, - dest_surface: Optional[Surface] = None, + dest_surface: Surface | None = None, keep_alpha: bool = False, ) -> Surface: """Replaces non transparent pixels with the provided color. @@ -356,13 +356,13 @@ def solid_overlay( """ def threshold( - dest_surface: Optional[Surface], + dest_surface: Surface | None, surface: Surface, - search_color: Optional[ColorLike], + search_color: ColorLike | None, threshold: ColorLike = (0, 0, 0, 0), - set_color: Optional[ColorLike] = (0, 0, 0, 0), + set_color: ColorLike | None = (0, 0, 0, 0), set_behavior: int = 1, - search_surf: Optional[Surface] = None, + search_surf: Surface | None = None, inverse_set: bool = False, ) -> int: """Finds which, and how many pixels in a surface are within a threshold of a 'search_color' or a 'search_surf'. @@ -427,7 +427,7 @@ def hsl( hue: float = 0, saturation: float = 0, lightness: float = 0, - dest_surface: Optional[Surface] = None, + dest_surface: Surface | None = None, ) -> Surface: """Change the hue, saturation, and lightness of a surface. @@ -463,7 +463,7 @@ def hsl( def pixelate( surface: Surface, pixel_size: int, - dest_surface: Optional[Surface] = None, + dest_surface: Surface | None = None, ) -> Surface: """Returns a pixelated version of the original surface. diff --git a/buildconfig/stubs/pygame/typing.pyi b/buildconfig/stubs/pygame/typing.pyi index b373ea1cbf..eaa8e5c57c 100644 --- a/buildconfig/stubs/pygame/typing.pyi +++ b/buildconfig/stubs/pygame/typing.pyi @@ -15,15 +15,15 @@ __all__ = [ from abc import abstractmethod from collections.abc import Callable from os import PathLike as _PathProtocol -from typing import IO, Protocol, TypeVar, Union +from typing import IO, Protocol, TypeAlias, TypeVar from pygame.color import Color from pygame.rect import FRect, Rect # For functions that take a file name -_PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] +_PathLike: TypeAlias = str | bytes | _PathProtocol[str] | _PathProtocol[bytes] # Most pygame functions that take a file argument should be able to handle a FileLike type -FileLike = Union[_PathLike, IO[bytes], IO[str]] +FileLike: TypeAlias = _PathLike | IO[bytes] | IO[str] _T_co = TypeVar("_T_co", covariant=True) @@ -42,24 +42,24 @@ class SequenceLike(Protocol[_T_co]): # Modify typehints when it is possible to annotate sizes # Pygame handles float without errors in most cases where a point is expected, -# usually rounding to int. Also, 'Union[int, float] == float' -Point = SequenceLike[float] +# usually rounding to int. Also, 'int | float == float' +Point: TypeAlias = SequenceLike[float] # This is used where ints are strictly required -IntPoint = SequenceLike[int] +IntPoint: TypeAlias = SequenceLike[int] -ColorLike = Union[Color, SequenceLike[int], str, int] +ColorLike: TypeAlias = Color | SequenceLike[int] | str | int class _HasRectAttribute(Protocol): # An object that has a rect attribute that is either a rect, or a function # that returns a rect conforms to the rect protocol @property - def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... + def rect(self) -> "RectLike | Callable[[], RectLike]": ... -RectLike = Union[ - Rect, FRect, SequenceLike[float], SequenceLike[Point], _HasRectAttribute -] +RectLike: TypeAlias = ( + Rect | FRect | SequenceLike[float] | SequenceLike[Point] | _HasRectAttribute +) # cleanup namespace @@ -70,7 +70,6 @@ del ( FRect, IO, Callable, - Union, TypeVar, Protocol, ) diff --git a/buildconfig/stubs/pygame/window.pyi b/buildconfig/stubs/pygame/window.pyi index f92bb3f9b5..89b2d55e60 100644 --- a/buildconfig/stubs/pygame/window.pyi +++ b/buildconfig/stubs/pygame/window.pyi @@ -1,12 +1,10 @@ -from typing import Optional, Union - from pygame.locals import WINDOWPOS_UNDEFINED from pygame.rect import Rect from pygame.surface import Surface from pygame.typing import Point, RectLike from typing_extensions import deprecated # added in 3.13 -def get_grabbed_window() -> Optional[Window]: ... +def get_grabbed_window() -> Window | None: ... class Window: """Pygame object that represents a window. @@ -93,7 +91,7 @@ class Window: self, title: str = "pygame window", size: Point = (640, 480), - position: Union[int, Point] = WINDOWPOS_UNDEFINED, + position: int | Point = WINDOWPOS_UNDEFINED, *, fullscreen: bool = ..., fullscreen_desktop: bool = ..., @@ -233,7 +231,7 @@ class Window: """Get the unique window ID (**read-only**).""" @property - def mouse_rect(self) -> Optional[Rect]: + def mouse_rect(self) -> Rect | None: """Get or set the mouse confinement rectangle of the window. Setting this attribute to a rect-like object confines the @@ -248,7 +246,7 @@ class Window: """ @mouse_rect.setter - def mouse_rect(self, value: Optional[RectLike]) -> None: ... + def mouse_rect(self, value: RectLike | None) -> None: ... @property def size(self) -> tuple[int, int]: """Get or set the window size in pixels.""" @@ -305,7 +303,7 @@ class Window: """ @position.setter - def position(self, value: Union[int, Point]) -> None: ... + def position(self, value: int | Point) -> None: ... opacity: float """Get or set the window opacity, between 0.0 (fully transparent) and 1.0 (fully opaque). diff --git a/dev.py b/dev.py index c3a68287c1..5ddffecc89 100644 --- a/dev.py +++ b/dev.py @@ -14,7 +14,7 @@ import sysconfig from enum import Enum from pathlib import Path -from typing import Any, Union +from typing import Any from buildconfig.get_version import version @@ -86,7 +86,7 @@ def pprint(arg: str, col: Colors = Colors.YELLOW): def cmd_run( - cmd: list[Union[str, Path]], + cmd: list[str | Path], capture_output: bool = False, error_on_output: bool = False, ) -> str: diff --git a/src_c/doc/display_doc.h b/src_c/doc/display_doc.h index f449077645..9e990e8248 100644 --- a/src_c/doc/display_doc.h +++ b/src_c/doc/display_doc.h @@ -4,7 +4,7 @@ #define DOC_DISPLAY_QUIT "quit() -> None\nUninitialize the display module." #define DOC_DISPLAY_GETINIT "get_init() -> bool\nReturns True if the display module has been initialized." #define DOC_DISPLAY_SETMODE "set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0) -> Surface\nInitialize a window or screen for display." -#define DOC_DISPLAY_GETSURFACE "get_surface() -> Optional[Surface]\nGet a reference to the currently set display surface." +#define DOC_DISPLAY_GETSURFACE "get_surface() -> Surface | None\nGet a reference to the currently set display surface." #define DOC_DISPLAY_FLIP "flip() -> None\nUpdate the full display Surface to the screen." #define DOC_DISPLAY_UPDATE "update() -> None\nupdate(rectangle, /) -> None\nupdate(rectangles, /) -> None\nupdate(x, y, w, h, /) -> None\nupdate(xy, wh, /) -> None\nUpdate all, or a portion, of the display. For non-OpenGL displays." #define DOC_DISPLAY_GETDRIVER "get_driver() -> str\nGet the name of the pygame display backend." diff --git a/src_c/doc/image_doc.h b/src_c/doc/image_doc.h index 9830b39291..f323255a96 100644 --- a/src_c/doc/image_doc.h +++ b/src_c/doc/image_doc.h @@ -4,7 +4,7 @@ #define DOC_IMAGE_LOADSIZEDSVG "load_sized_svg(file, size) -> Surface\nLoad an SVG image from a file (or file-like object) with the given size." #define DOC_IMAGE_LOADANIMATION "load_animation(file, namehint='') -> list[tuple[Surface, float]]\nLoad an animation (GIF/WEBP) from a file (or file-like object) as a list of frames." #define DOC_IMAGE_SAVE "save(surface, file, namehint='') -> None\nSave an image to file (or file-like object)." -#define DOC_IMAGE_GETSDLIMAGEVERSION "get_sdl_image_version(linked=True) -> Optional[tuple[int, int, int]]\nGet version number of the SDL_Image library being used." +#define DOC_IMAGE_GETSDLIMAGEVERSION "get_sdl_image_version(linked=True) -> tuple[int, int, int] | None\nGet version number of the SDL_Image library being used." #define DOC_IMAGE_GETEXTENDED "get_extended() -> bool\nTest if extended image formats can be loaded." #define DOC_IMAGE_TOSTRING "tostring(surface, format, flipped=False, pitch=-1) -> bytes\nTransfer image to byte buffer." #define DOC_IMAGE_TOBYTES "tobytes(surface, format, flipped=False, pitch=-1) -> bytes\nTransfer image to byte buffer." diff --git a/src_c/doc/surface_doc.h b/src_c/doc/surface_doc.h index eff7fb42a3..4034af252c 100644 --- a/src_c/doc/surface_doc.h +++ b/src_c/doc/surface_doc.h @@ -1,7 +1,7 @@ /* Auto generated file: with make_docs.py . Docs go in docs/reST/ref/ . */ #define DOC_SURFACE "Surface(size, flags=0, depth=0, masks=None) -> Surface\nSurface(size, flags=0, surface=...) -> Surface\nPygame object for representing images." #define DOC_SURFACE_BLIT "blit(source, dest=(0, 0), area=None, special_flags=0) -> Rect\nDraw another Surface onto this one." -#define DOC_SURFACE_BLITS "blits(blit_sequence, doreturn=1) -> Union[list[Rect], None]\nDraw many Surfaces onto this Surface at their corresponding location." +#define DOC_SURFACE_BLITS "blits(blit_sequence, doreturn=1) -> list[Rect] | None\nDraw many Surfaces onto this Surface at their corresponding location." #define DOC_SURFACE_FBLITS "fblits(blit_sequence, special_flags=0, /) -> None\nDraw many Surfaces onto this Surface at their corresponding location and with the same special_flags." #define DOC_SURFACE_CONVERT "convert(surface, /) -> Surface\nconvert(depth, flags=0, /) -> Surface\nconvert(masks, flags=0, /) -> Surface\nconvert() -> Surface\nChange the pixel format of a Surface." #define DOC_SURFACE_CONVERTALPHA "convert_alpha() -> Surface\nChange the pixel format of a Surface including per pixel alphas." @@ -9,9 +9,9 @@ #define DOC_SURFACE_FILL "fill(color, rect=None, special_flags=0) -> Rect\nFill Surface with a solid color." #define DOC_SURFACE_SCROLL "scroll(dx=0, dy=0, scroll_flag=0, /) -> None\nShift the Surface pixels in place." #define DOC_SURFACE_SETCOLORKEY "set_colorkey(color, flags=0, /) -> None\nset_colorkey(color, /) -> None\nSet the transparent colorkey." -#define DOC_SURFACE_GETCOLORKEY "get_colorkey() -> Optional[tuple[int, int, int, int]]\nGet the current transparent colorkey." +#define DOC_SURFACE_GETCOLORKEY "get_colorkey() -> tuple[int, int, int, int] | None\nGet the current transparent colorkey." #define DOC_SURFACE_SETALPHA "set_alpha(value, flags=0, /) -> None\nset_alpha(value, /) -> None\nSet the alpha value for the full Surface." -#define DOC_SURFACE_GETALPHA "get_alpha() -> Optional[int]\nGet the current Surface transparency value." +#define DOC_SURFACE_GETALPHA "get_alpha() -> int | None\nGet the current Surface transparency value." #define DOC_SURFACE_LOCK "lock() -> None\nLock the Surface memory for pixel access." #define DOC_SURFACE_UNLOCK "unlock() -> None\nUnlock the Surface memory from pixel access." #define DOC_SURFACE_MUSTLOCK "mustlock() -> bool\nTest if the Surface requires locking." diff --git a/src_c/doc/system_doc.h b/src_c/doc/system_doc.h index 449f7defbe..d0b939a170 100644 --- a/src_c/doc/system_doc.h +++ b/src_c/doc/system_doc.h @@ -4,4 +4,4 @@ #define DOC_SYSTEM_GETTOTALRAM "get_total_ram() -> int\nGet the amount of RAM configured in the system." #define DOC_SYSTEM_GETPREFPATH "get_pref_path(org, app) -> str\nGet a writeable folder for your app." #define DOC_SYSTEM_GETPREFLOCALES "get_pref_locales() -> list[_Locale]\nGet preferred locales set on the system." -#define DOC_SYSTEM_GETPOWERSTATE "get_power_state() -> Optional[PowerState]\nGet the current power supply state." +#define DOC_SYSTEM_GETPOWERSTATE "get_power_state() -> PowerState | None\nGet the current power supply state." diff --git a/src_c/doc/window_doc.h b/src_c/doc/window_doc.h index 6495da670e..5de38f1bf3 100644 --- a/src_c/doc/window_doc.h +++ b/src_c/doc/window_doc.h @@ -10,7 +10,7 @@ #define DOC_WINDOW_BORDERLESS "borderless -> bool\nGets or sets whether the window is borderless." #define DOC_WINDOW_ALWAYSONTOP "always_on_top -> bool\nGet or set whether the window is always on top." #define DOC_WINDOW_ID "id -> int\nGet the unique window ID (**read-only**)." -#define DOC_WINDOW_MOUSERECT "mouse_rect -> Optional[Rect]\nGet or set the mouse confinement rectangle of the window." +#define DOC_WINDOW_MOUSERECT "mouse_rect -> Rect | None\nGet or set the mouse confinement rectangle of the window." #define DOC_WINDOW_SIZE "size -> tuple[int, int]\nGet or set the window size in pixels." #define DOC_WINDOW_MINIMUMSIZE "minimum_size -> tuple[int, int]\nGet or set the minimum size of the window's client area." #define DOC_WINDOW_MAXIMUMSIZE "maximum_size -> tuple[int, int]\nGet or set the maximum size of the window's client area." diff --git a/src_py/_data_classes.py b/src_py/_data_classes.py index 2c4f558531..53e717b797 100644 --- a/src_py/_data_classes.py +++ b/src_py/_data_classes.py @@ -1,11 +1,10 @@ from dataclasses import dataclass -from typing import Optional @dataclass(frozen=True) class PowerState: - battery_percent: Optional[int] - battery_seconds: Optional[int] + battery_percent: int | None + battery_seconds: int | None on_battery: bool no_battery: bool charging: bool diff --git a/src_py/_debug.py b/src_py/_debug.py index a39f599bc4..4e22952310 100644 --- a/src_py/_debug.py +++ b/src_py/_debug.py @@ -4,15 +4,11 @@ import platform import sys import traceback -from collections.abc import Callable from os import environ -from typing import Optional from pygame.system import get_cpu_instruction_sets from pygame.version import ver -ImportResult = tuple[str, bool, Optional[Callable]] - def str_from_tuple(version_tuple): """Converts a tuple like (2, 0, 20) into a string joined by periods diff --git a/src_py/sprite.py b/src_py/sprite.py index 9e72c3f2e1..5eebc03e8c 100644 --- a/src_py/sprite.py +++ b/src_py/sprite.py @@ -85,7 +85,6 @@ # specialized cases. import types -from typing import Optional from warnings import warn import pygame @@ -111,8 +110,8 @@ class Sprite: def __init__(self, *groups): self.__g = {} # The groups the sprite is in - self.__image: Optional[pygame.surface.Surface] = None - self.__rect: Optional[pygame.rect.Rect] = None + self.__image: pygame.surface.Surface | None = None + self.__rect: pygame.rect.Rect | None = None if groups: self.add(*groups) @@ -121,7 +120,7 @@ def image(self): return self.__image @image.setter - def image(self, value: Optional[pygame.surface.Surface]): + def image(self, value: pygame.surface.Surface | None): self.__image = value @property @@ -129,7 +128,7 @@ def rect(self): return self.__rect @rect.setter - def rect(self, value: Optional[pygame.rect.Rect]): + def rect(self, value: pygame.rect.Rect | None): self.__rect = value def add(self, *groups): diff --git a/src_py/typing.py b/src_py/typing.py index b373ea1cbf..eaa8e5c57c 100644 --- a/src_py/typing.py +++ b/src_py/typing.py @@ -15,15 +15,15 @@ from abc import abstractmethod from collections.abc import Callable from os import PathLike as _PathProtocol -from typing import IO, Protocol, TypeVar, Union +from typing import IO, Protocol, TypeAlias, TypeVar from pygame.color import Color from pygame.rect import FRect, Rect # For functions that take a file name -_PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] +_PathLike: TypeAlias = str | bytes | _PathProtocol[str] | _PathProtocol[bytes] # Most pygame functions that take a file argument should be able to handle a FileLike type -FileLike = Union[_PathLike, IO[bytes], IO[str]] +FileLike: TypeAlias = _PathLike | IO[bytes] | IO[str] _T_co = TypeVar("_T_co", covariant=True) @@ -42,24 +42,24 @@ def __len__(self) -> int: ... # Modify typehints when it is possible to annotate sizes # Pygame handles float without errors in most cases where a point is expected, -# usually rounding to int. Also, 'Union[int, float] == float' -Point = SequenceLike[float] +# usually rounding to int. Also, 'int | float == float' +Point: TypeAlias = SequenceLike[float] # This is used where ints are strictly required -IntPoint = SequenceLike[int] +IntPoint: TypeAlias = SequenceLike[int] -ColorLike = Union[Color, SequenceLike[int], str, int] +ColorLike: TypeAlias = Color | SequenceLike[int] | str | int class _HasRectAttribute(Protocol): # An object that has a rect attribute that is either a rect, or a function # that returns a rect conforms to the rect protocol @property - def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... + def rect(self) -> "RectLike | Callable[[], RectLike]": ... -RectLike = Union[ - Rect, FRect, SequenceLike[float], SequenceLike[Point], _HasRectAttribute -] +RectLike: TypeAlias = ( + Rect | FRect | SequenceLike[float] | SequenceLike[Point] | _HasRectAttribute +) # cleanup namespace @@ -70,7 +70,6 @@ def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... FRect, IO, Callable, - Union, TypeVar, Protocol, )