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/color.pyi b/buildconfig/stubs/pygame/color.pyi index 635b05d4ea..7bf06a0168 100644 --- a/buildconfig/stubs/pygame/color.pyi +++ b/buildconfig/stubs/pygame/color.pyi @@ -1,7 +1,8 @@ import sys -from typing import Any, Dict, Iterator, SupportsIndex, Tuple, Union, overload +from typing import Any, Iterator, SupportsIndex, Tuple, Union, overload from typing_extensions import deprecated # added in 3.13 +from pygame.colordict import THECOLORS as THECOLORS from pygame.typing import ColorLike if sys.version_info >= (3, 9): @@ -9,8 +10,6 @@ if sys.version_info >= (3, 9): else: from typing import Collection -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..a5595b0b63 --- /dev/null +++ b/buildconfig/stubs/pygame/colordict.pyi @@ -0,0 +1,3 @@ +from typing import Dict, Tuple + +THECOLORS: Dict[str, Tuple[int, int, int, int]] diff --git a/buildconfig/stubs/pygame/font.pyi b/buildconfig/stubs/pygame/font.pyi index ea1074cee7..a00de7ca01 100644 --- a/buildconfig/stubs/pygame/font.pyi +++ b/buildconfig/stubs/pygame/font.pyi @@ -1,7 +1,13 @@ -from typing import Callable, Hashable, Iterable, List, Literal, Optional, Tuple, Union +from typing import List, Literal, Optional, Tuple, Union 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 # TODO: Figure out a way to type this attribute such that mypy knows it's not @@ -13,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..fc6d0c195e --- /dev/null +++ b/buildconfig/stubs/pygame/sysfont.pyi @@ -0,0 +1,17 @@ +from typing import Callable, Hashable, Iterable, List, 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/buildconfig/stubs/pygame/system.pyi b/buildconfig/stubs/pygame/system.pyi index f2e367473f..991ef23417 100644 --- a/buildconfig/stubs/pygame/system.pyi +++ b/buildconfig/stubs/pygame/system.pyi @@ -1,6 +1,6 @@ from typing import List, 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 852c5192fb..17969e7c71 100644 --- a/buildconfig/stubs/pygame/typing.pyi +++ b/buildconfig/stubs/pygame/typing.pyi @@ -14,7 +14,9 @@ __all__ = [ import sys from abc import abstractmethod -from typing import IO, Callable, Tuple, Union, TypeVar, Protocol +from dataclasses import dataclass +from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, Optional + if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol @@ -66,5 +68,28 @@ class _HasRectAttribute(Protocol): RectLike = Union[SequenceLike[float], SequenceLike[Coordinate], _HasRectAttribute] +@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 sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol +del ( + sys, + abstractmethod, + IO, + Callable, + Tuple, + Union, + TypeVar, + Protocol, + Optional, + dataclass, +) diff --git a/src_c/system.c b/src_c/system.c index 7d715821ad..1000a64364 100644 --- a/src_c/system.c +++ b/src_c/system.c @@ -261,8 +261,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 5f7e540547..3b0ed8863c 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -317,7 +317,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/sysfont.py b/src_py/sysfont.py index 8879f3e128..d7a62e00ae 100644 --- a/src_py/sysfont.py +++ b/src_py/sysfont.py @@ -557,3 +557,10 @@ def match_font(name, bold=False, italic=False): break return fontname + + +__all__ = [ + "get_fonts", + "match_font", + "SysFont", +] diff --git a/src_py/typing.py b/src_py/typing.py index 852c5192fb..17969e7c71 100644 --- a/src_py/typing.py +++ b/src_py/typing.py @@ -14,7 +14,9 @@ import sys from abc import abstractmethod -from typing import IO, Callable, Tuple, Union, TypeVar, Protocol +from dataclasses import dataclass +from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, Optional + if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol @@ -66,5 +68,28 @@ def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... RectLike = Union[SequenceLike[float], SequenceLike[Coordinate], _HasRectAttribute] +@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 sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol +del ( + sys, + abstractmethod, + IO, + Callable, + Tuple, + Union, + TypeVar, + Protocol, + Optional, + dataclass, +)