Skip to content

Add colordict and sysfont to typing, move PowerState class to typing module #3133

New issue

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

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

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion buildconfig/stubs/mypy_allow_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ pygame\.newbuffer
pygame\.pkgdata
pygame\.pypm
pygame\._sdl2\.mixer
pygame\.sysfont.*
pygame\.docs.*
3 changes: 1 addition & 2 deletions buildconfig/stubs/pygame/color.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
1 change: 1 addition & 0 deletions buildconfig/stubs/pygame/colordict.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
THECOLORS: dict[str, tuple[int, int, int, int]]
22 changes: 7 additions & 15 deletions buildconfig/stubs/pygame/font.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions buildconfig/stubs/pygame/sysfont.pyi
Original file line number Diff line number Diff line change
@@ -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: ...
2 changes: 1 addition & 1 deletion buildconfig/stubs/pygame/system.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion buildconfig/stubs/pygame/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -73,4 +86,6 @@ del (
Union,
TypeVar,
Protocol,
Optional,
dataclass,
)
3 changes: 1 addition & 2 deletions src_c/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions src_py/_data_classes.py

This file was deleted.

1 change: 0 additions & 1 deletion src_py/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
python_sources = files(
'__init__.py',
'_camera_opencv.py',
'_data_classes.py',
'_debug.py',
'_sprite.py',
'camera.py',
Expand Down
7 changes: 7 additions & 0 deletions src_py/sysfont.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,10 @@ def match_font(name, bold=False, italic=False):
break

return fontname


__all__ = [
"get_fonts",
"match_font",
"SysFont",
]
17 changes: 16 additions & 1 deletion src_py/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -73,4 +86,6 @@ def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ...
Union,
TypeVar,
Protocol,
Optional,
dataclass,
)
Loading