Skip to content
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

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*
5 changes: 2 additions & 3 deletions buildconfig/stubs/pygame/color.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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):
from collections.abc import Collection
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]):
Expand Down
3 changes: 3 additions & 0 deletions buildconfig/stubs/pygame/colordict.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from typing import Dict, Tuple

THECOLORS: Dict[str, Tuple[int, int, int, int]]
21 changes: 7 additions & 14 deletions buildconfig/stubs/pygame/font.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions buildconfig/stubs/pygame/sysfont.pyi
Original file line number Diff line number Diff line change
@@ -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: ...
2 changes: 1 addition & 1 deletion buildconfig/stubs/pygame/system.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 27 additions & 2 deletions buildconfig/stubs/pygame/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
3 changes: 1 addition & 2 deletions src_c/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -557,3 +557,10 @@ def match_font(name, bold=False, italic=False):
break

return fontname


__all__ = [
"get_fonts",
"match_font",
"SysFont",
]
29 changes: 27 additions & 2 deletions src_py/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Loading