From 2a157a12428fb17639a7f7b683b6c8ccdaeb5bfa Mon Sep 17 00:00:00 2001 From: Ankith Date: Sun, 29 Sep 2024 22:26:49 +0530 Subject: [PATCH 1/3] Add stub for colordict --- buildconfig/stubs/pygame/color.pyi | 3 +-- buildconfig/stubs/pygame/colordict.pyi | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 buildconfig/stubs/pygame/colordict.pyi diff --git a/buildconfig/stubs/pygame/color.pyi b/buildconfig/stubs/pygame/color.pyi index 10980f347c..4c1e7043f8 100644 --- a/buildconfig/stubs/pygame/color.pyi +++ b/buildconfig/stubs/pygame/color.pyi @@ -2,11 +2,10 @@ import sys from collections.abc import Collection, Iterator from typing import Any, ClassVar, SupportsIndex, Union, overload +from pygame.colordict import THECOLORS as THECOLORS from pygame.typing import ColorLike from typing_extensions import deprecated # added in 3.13 -THECOLORS: dict[str, tuple[int, int, int, int]] - # Color confirms to the Collection ABC, since it also confirms to # Sized, Iterable and Container ABCs class Color(Collection[int]): diff --git a/buildconfig/stubs/pygame/colordict.pyi b/buildconfig/stubs/pygame/colordict.pyi new file mode 100644 index 0000000000..0a344184f3 --- /dev/null +++ b/buildconfig/stubs/pygame/colordict.pyi @@ -0,0 +1 @@ +THECOLORS: dict[str, tuple[int, int, int, int]] From 5893923f68d7f843a192d46bbf3a11cc57649f82 Mon Sep 17 00:00:00 2001 From: Ankith Date: Sun, 29 Sep 2024 22:26:52 +0530 Subject: [PATCH 2/3] Add stub for sysfont --- buildconfig/stubs/mypy_allow_list.txt | 1 - buildconfig/stubs/pygame/font.pyi | 22 +++++++--------------- buildconfig/stubs/pygame/sysfont.pyi | 18 ++++++++++++++++++ src_py/sysfont.py | 7 +++++++ 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 buildconfig/stubs/pygame/sysfont.pyi diff --git a/buildconfig/stubs/mypy_allow_list.txt b/buildconfig/stubs/mypy_allow_list.txt index 1e7c532d72..7b0c5a43b1 100644 --- a/buildconfig/stubs/mypy_allow_list.txt +++ b/buildconfig/stubs/mypy_allow_list.txt @@ -25,5 +25,4 @@ pygame\.newbuffer pygame\.pkgdata pygame\.pypm pygame\._sdl2\.mixer -pygame\.sysfont.* pygame\.docs.* diff --git a/buildconfig/stubs/pygame/font.pyi b/buildconfig/stubs/pygame/font.pyi index 99587d9b76..cdea41533c 100644 --- a/buildconfig/stubs/pygame/font.pyi +++ b/buildconfig/stubs/pygame/font.pyi @@ -1,9 +1,14 @@ -from collections.abc import Callable, Hashable, Iterable from typing import Literal, Optional, Union +from typing_extensions import deprecated # added in 3.13 + from pygame.surface import Surface +from pygame.sysfont import ( + get_fonts as get_fonts, + match_font as match_font, + SysFont as SysFont, +) from pygame.typing import ColorLike, FileLike -from typing_extensions import deprecated # added in 3.13 # TODO: Figure out a way to type this attribute such that mypy knows it's not # always defined at runtime @@ -14,19 +19,6 @@ def quit() -> None: ... def get_init() -> bool: ... 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]]], - bold: Hashable = False, - italic: Hashable = False, -) -> str: ... -def SysFont( - name: Union[str, bytes, Iterable[Union[str, bytes]], None], - size: int, - bold: Hashable = False, - italic: Hashable = False, - constructor: Optional[Callable[[Optional[str], int, bool, bool], Font]] = None, -) -> Font: ... class Font: @property diff --git a/buildconfig/stubs/pygame/sysfont.pyi b/buildconfig/stubs/pygame/sysfont.pyi new file mode 100644 index 0000000000..b0f68bf41c --- /dev/null +++ b/buildconfig/stubs/pygame/sysfont.pyi @@ -0,0 +1,18 @@ +from collections.abc import Callable, Hashable, Iterable +from typing import Optional, Union + +from pygame.font import Font + +def get_fonts() -> list[str]: ... +def match_font( + name: Union[str, bytes, Iterable[Union[str, bytes]]], + bold: Hashable = False, + italic: Hashable = False, +) -> str: ... +def SysFont( + name: Union[str, bytes, Iterable[Union[str, bytes]], None], + size: int, + bold: Hashable = False, + italic: Hashable = False, + constructor: Optional[Callable[[Optional[str], int, bool, bool], Font]] = None, +) -> Font: ... diff --git a/src_py/sysfont.py b/src_py/sysfont.py index 7c512dbac7..82c6290532 100644 --- a/src_py/sysfont.py +++ b/src_py/sysfont.py @@ -561,3 +561,10 @@ def match_font(name, bold=False, italic=False): break return fontname + + +__all__ = [ + "get_fonts", + "match_font", + "SysFont", +] From e86fb2cd0351d8525d6d49f077486ca09f7e44c0 Mon Sep 17 00:00:00 2001 From: Ankith Date: Sun, 29 Sep 2024 22:34:05 +0530 Subject: [PATCH 3/3] Move PowerState dataclass to typing.py --- buildconfig/stubs/pygame/system.pyi | 2 +- buildconfig/stubs/pygame/typing.pyi | 17 ++++++++++++++++- src_c/system.c | 3 +-- src_py/__init__.py | 2 +- src_py/_data_classes.py | 14 -------------- src_py/meson.build | 1 - src_py/typing.py | 17 ++++++++++++++++- 7 files changed, 35 insertions(+), 21 deletions(-) delete mode 100644 src_py/_data_classes.py diff --git a/buildconfig/stubs/pygame/system.pyi b/buildconfig/stubs/pygame/system.pyi index 6c529297fb..e70d0bd91b 100644 --- a/buildconfig/stubs/pygame/system.pyi +++ b/buildconfig/stubs/pygame/system.pyi @@ -5,7 +5,7 @@ from typing import Optional, TypedDict -from pygame._data_classes import PowerState +from pygame.typing import PowerState class _InstructionSets(TypedDict): ALTIVEC: bool diff --git a/buildconfig/stubs/pygame/typing.pyi b/buildconfig/stubs/pygame/typing.pyi index b373ea1cbf..f6899d114e 100644 --- a/buildconfig/stubs/pygame/typing.pyi +++ b/buildconfig/stubs/pygame/typing.pyi @@ -14,8 +14,9 @@ __all__ = [ from abc import abstractmethod from collections.abc import Callable +from dataclasses import dataclass from os import PathLike as _PathProtocol -from typing import IO, Protocol, TypeVar, Union +from typing import IO, Protocol, TypeVar, Union, Optional from pygame.color import Color from pygame.rect import FRect, Rect @@ -62,6 +63,18 @@ RectLike = Union[ ] +@dataclass(frozen=True) +class PowerState: + battery_percent: Optional[int] + battery_seconds: Optional[int] + on_battery: bool + no_battery: bool + charging: bool + charged: bool + plugged_in: bool + has_battery: bool + + # cleanup namespace del ( abstractmethod, @@ -73,4 +86,6 @@ del ( Union, TypeVar, Protocol, + Optional, + dataclass, ) diff --git a/src_c/system.c b/src_c/system.c index 2c9e2b4e5f..7aefa048df 100644 --- a/src_c/system.c +++ b/src_c/system.c @@ -283,8 +283,7 @@ MODINIT_DEFINE(system) return NULL; } - PyObject *data_classes_module = - PyImport_ImportModule("pygame._data_classes"); + PyObject *data_classes_module = PyImport_ImportModule("pygame.typing"); if (!data_classes_module) { return NULL; } diff --git a/src_py/__init__.py b/src_py/__init__.py index ccfe90b597..3d7a81a54f 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -330,7 +330,7 @@ def PixelArray(surface): # pylint: disable=unused-argument try: import pygame.system - from pygame._data_classes import PowerState as power_state + from pygame.typing import PowerState as power_state power_state.__module__ = "pygame.system" del power_state diff --git a/src_py/_data_classes.py b/src_py/_data_classes.py deleted file mode 100644 index 2c4f558531..0000000000 --- a/src_py/_data_classes.py +++ /dev/null @@ -1,14 +0,0 @@ -from dataclasses import dataclass -from typing import Optional - - -@dataclass(frozen=True) -class PowerState: - battery_percent: Optional[int] - battery_seconds: Optional[int] - on_battery: bool - no_battery: bool - charging: bool - charged: bool - plugged_in: bool - has_battery: bool diff --git a/src_py/meson.build b/src_py/meson.build index 541c54cd69..f8baf4984a 100644 --- a/src_py/meson.build +++ b/src_py/meson.build @@ -2,7 +2,6 @@ python_sources = files( '__init__.py', '_camera_opencv.py', - '_data_classes.py', '_debug.py', '_sprite.py', 'camera.py', diff --git a/src_py/typing.py b/src_py/typing.py index b373ea1cbf..f6899d114e 100644 --- a/src_py/typing.py +++ b/src_py/typing.py @@ -14,8 +14,9 @@ from abc import abstractmethod from collections.abc import Callable +from dataclasses import dataclass from os import PathLike as _PathProtocol -from typing import IO, Protocol, TypeVar, Union +from typing import IO, Protocol, TypeVar, Union, Optional from pygame.color import Color from pygame.rect import FRect, Rect @@ -62,6 +63,18 @@ def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... ] +@dataclass(frozen=True) +class PowerState: + battery_percent: Optional[int] + battery_seconds: Optional[int] + on_battery: bool + no_battery: bool + charging: bool + charged: bool + plugged_in: bool + has_battery: bool + + # cleanup namespace del ( abstractmethod, @@ -73,4 +86,6 @@ def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... Union, TypeVar, Protocol, + Optional, + dataclass, )