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

Mark old xType aliases as deprecated #3288

Open
wants to merge 1 commit 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
30 changes: 28 additions & 2 deletions buildconfig/stubs/pygame/event.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
from typing import Any, Optional, Union, final
from typing_extensions import deprecated # added in 3.13

from pygame.typing import SequenceLike

@final
class Event:
# Event fields here duplicated below into EventType because Event is
# marked as final and can't be subclassed like the other xType aliases
# are to allow @deprecated.

@property
def type(self) -> int: ...
__dict__: dict[str, Any]
__hash__: None # type: ignore
def __init__(
self, type: int, dict: dict[str, Any] = ..., **kwargs: Any
) -> None: ...
def __getattribute__(self, name: str) -> Any: ...
def __setattr__(self, name: str, value: Any) -> None: ...
def __delattr__(self, name: str) -> None: ...
def __bool__(self) -> bool: ...

# this is at the bottom because mypy complains if this declaration comes
# before any uses of the dict[] typehinting because of the same naming
@property
def dict(self) -> dict[str, Any]: ...

@final
@deprecated("Use `Event` instead (this is an old alias)")
class EventType:
# Duplicated Event fields here because it is marked as final and can't be
# subclassed like the other xType aliases are to allow @deprecated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how all other instances are elegantly marked as deprecated without duplication, and there's there's this that sticks out.
I get why it's done, but can we find a workaround that doesn't need the duplication? Something like

EventType = deprecated("Use Event instead (this is an old alias)")(Event) should be a one-liner that could work? If it does, this style could be used everywhere

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a clever idea, but I get a stubtest error.

error: pygame.event.EventType variable differs from runtime type builtins.type
Stub: in file C:\Users\charl\Desktop\pygame-ce\buildconfig\stubs\pygame\event.pyi:29
def (type: builtins.int, dict: builtins.dict[builtins.str, Any] =, **kwargs: Any) -> pygame.event.Event
Runtime:
<class 'pygame.event.Event'>

Copy link
Member Author

@Starbuck5 Starbuck5 Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did get it almost perfect with

class _EventStuff:

    @property
    def type(self) -> int: ...
    __dict__: dict[str, Any]
    __hash__: None  # type: ignore
    def __init__(
        self, type: int, dict: dict[str, Any] = ..., **kwargs: Any
    ) -> None: ...
    def __getattribute__(self, name: str) -> Any: ...
    def __setattr__(self, name: str, value: Any) -> None: ...
    def __delattr__(self, name: str) -> None: ...
    def __bool__(self) -> bool: ...

    # this is at the bottom because mypy complains if this declaration comes
    # before any uses of the dict[] typehinting because of the same naming
    @property
    def dict(self) -> dict[str, Any]: ...

@final
class Event(_EventStuff):
    pass

@final
@deprecated("Use `Event` instead (this is an old alias)")
class EventType(_EventStuff):
    pass

The problem is that _EventStuff is also exported, shows up in the list of suggestions when typing pygame.event. I tried del _EventStuff but that didn't change it.

@property
def type(self) -> int: ...
__dict__: dict[str, Any]
Expand Down Expand Up @@ -41,5 +69,3 @@ def set_grab(grab: bool, /) -> None: ...
def get_grab() -> bool: ...
def post(event: Event, /) -> bool: ...
def custom_type() -> int: ...

EventType = Event
4 changes: 3 additions & 1 deletion buildconfig/stubs/pygame/font.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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

Expand Down Expand Up @@ -87,4 +88,5 @@ class Font:
def get_point_size(self) -> int: ...
def set_point_size(self, val: int, /) -> None: ...

FontType = Font
@deprecated("Use `Font` instead (this is an old alias)")
class FontType(Font): ...
4 changes: 3 additions & 1 deletion buildconfig/stubs/pygame/mask.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Optional, Union
from typing_extensions import deprecated # added in 3.13

from pygame.rect import Rect
from pygame.surface import Surface
Expand Down Expand Up @@ -54,4 +55,5 @@ class Mask:
dest: Union[RectLike, Point] = (0, 0),
) -> Surface: ...

MaskType = Mask
@deprecated("Use `Mask` instead (this is an old alias)")
class MaskType(Mask): ...
8 changes: 6 additions & 2 deletions buildconfig/stubs/pygame/mixer.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Optional, Union, overload
from typing_extensions import deprecated # added in 3.13

import numpy

Expand Down Expand Up @@ -100,5 +101,8 @@ class Channel:
def set_endevent(self, type: Union[int, Event] = 0, /) -> None: ...
def get_endevent(self) -> int: ...

SoundType = Sound
ChannelType = Channel
@deprecated("Use `Sound` instead (this is an old alias)")
class SoundType(Sound): ...

@deprecated("Use `Channel` instead (this is an old alias)")
class ChannelType(Channel): ...
8 changes: 6 additions & 2 deletions buildconfig/stubs/pygame/rect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from typing import (
overload,
Optional,
)
from typing_extensions import deprecated # added in 3.13

from pygame.typing import Point, RectLike, SequenceLike

Expand Down Expand Up @@ -276,5 +277,8 @@ class Rect(_GenericRect[int]):
class FRect(_GenericRect[float]):
...

RectType = Rect
FRectType = FRect
@deprecated("Use `Rect` instead (this is an old alias)")
class RectType(Rect): ...

@deprecated("Use `FRect` instead (this is an old alias)")
class FRectType(FRect): ...
Starbuck5 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion buildconfig/stubs/pygame/surface.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,5 @@ class Surface:
def premul_alpha(self) -> Surface: ...
def premul_alpha_ip(self) -> Surface: ...

SurfaceType = Surface
@deprecated("Use `Surface` instead (this is an old alias)")
class SurfaceType(Surface): ...
Loading