From cf3eff0c2edde91a08abd9a20744d7c38541a371 Mon Sep 17 00:00:00 2001 From: Ankith Date: Sat, 5 Oct 2024 21:57:06 +0530 Subject: [PATCH] Use python 3.9isms in code, examples and docs --- docs/reST/ref/sprite.rst | 2 +- examples/stars.py | 9 ++++----- examples/textinput.py | 7 +++---- src_c/cython/pygame/_sdl2/audio.pyx | 2 +- src_c/doc/sprite_doc.h | 2 +- src_py/_debug.py | 5 +++-- src_py/pkgdata.py | 31 ++++++++++------------------- 7 files changed, 23 insertions(+), 35 deletions(-) diff --git a/docs/reST/ref/sprite.rst b/docs/reST/ref/sprite.rst index 268727a1d0..a1e96a71c9 100644 --- a/docs/reST/ref/sprite.rst +++ b/docs/reST/ref/sprite.rst @@ -291,7 +291,7 @@ Sprites are not thread safe. So lock them yourself if using threads. .. method:: draw | :sl:`blit the Sprite images` - | :sg:`draw(Surface) -> List[Rect]` + | :sg:`draw(Surface) -> list[Rect]` Draws the contained Sprites to the Surface argument. This uses the ``Sprite.image`` attribute for the source surface, and ``Sprite.rect`` diff --git a/examples/stars.py b/examples/stars.py index 0b7bd32942..0b63d922f4 100644 --- a/examples/stars.py +++ b/examples/stars.py @@ -11,7 +11,6 @@ """ import random -from typing import List import pygame @@ -19,7 +18,7 @@ class Particle: def __init__( self, - pos: List[int], + pos: list[int], vel: pygame.Vector2, ): """ @@ -55,7 +54,7 @@ def update(self): self.pos += self.vel -def create_particle(particle_list: List[Particle], pos: pygame.Vector2): +def create_particle(particle_list: list[Particle], pos: pygame.Vector2): """ Creates a new particle Parameters: @@ -71,7 +70,7 @@ def create_particle(particle_list: List[Particle], pos: pygame.Vector2): ) -def update_particles(particle_list: List[Particle], screen_rect: pygame.Rect): +def update_particles(particle_list: list[Particle], screen_rect: pygame.Rect): """ Updates the particles Parameters: @@ -87,7 +86,7 @@ def update_particles(particle_list: List[Particle], screen_rect: pygame.Rect): particle.update() -def draw_particles(particle_list: List[Particle], display: pygame.Surface): +def draw_particles(particle_list: list[Particle], display: pygame.Surface): """ Draws the particles Parameters: diff --git a/examples/textinput.py b/examples/textinput.py index 76cfb38902..c783de8bd8 100644 --- a/examples/textinput.py +++ b/examples/textinput.py @@ -6,7 +6,6 @@ Shows how to use the TEXTEDITING and TEXTINPUT events. """ -from typing import Tuple, List import sys import os @@ -39,8 +38,8 @@ class TextInput: def __init__( self, prompt: str, - pos: Tuple[int, int], - screen_dimensions: Tuple[int, int], + pos: tuple[int, int], + screen_dimensions: tuple[int, int], print_event: bool, text_color="white", fps: int = 50, @@ -77,7 +76,7 @@ def __init__( print("Using font: " + self.font.name) - def update(self, events: List[pygame.Event]) -> None: + def update(self, events: list[pygame.Event]) -> None: """ Updates the text input widget """ diff --git a/src_c/cython/pygame/_sdl2/audio.pyx b/src_c/cython/pygame/_sdl2/audio.pyx index c3667d5e32..97c6504a08 100644 --- a/src_c/cython/pygame/_sdl2/audio.pyx +++ b/src_c/cython/pygame/_sdl2/audio.pyx @@ -51,7 +51,7 @@ def get_audio_device_names(iscapture = False): If True return devices available for capture. :return: list of devicenames. - :rtype: List[string] + :rtype: list[string] """ cdef int count = SDL_GetNumAudioDevices(iscapture) diff --git a/src_c/doc/sprite_doc.h b/src_c/doc/sprite_doc.h index 86e61f3565..857f1c6d70 100644 --- a/src_c/doc/sprite_doc.h +++ b/src_c/doc/sprite_doc.h @@ -15,7 +15,7 @@ #define DOC_SPRITE_GROUP_REMOVE "remove(*sprites) -> None\nremove Sprites from the Group" #define DOC_SPRITE_GROUP_HAS "has(*sprites) -> bool\ntest if a Group contains Sprites" #define DOC_SPRITE_GROUP_UPDATE "update(*args, **kwargs) -> None\ncall the update method on contained Sprites" -#define DOC_SPRITE_GROUP_DRAW "draw(Surface) -> List[Rect]\nblit the Sprite images" +#define DOC_SPRITE_GROUP_DRAW "draw(Surface) -> list[Rect]\nblit the Sprite images" #define DOC_SPRITE_GROUP_CLEAR "clear(Surface_dest, background) -> None\ndraw a background over the Sprites" #define DOC_SPRITE_GROUP_EMPTY "empty() -> None\nremove all Sprites" #define DOC_SPRITE_RENDERUPDATES "RenderUpdates(*sprites) -> RenderUpdates\nGroup sub-class that tracks dirty updates." diff --git a/src_py/_debug.py b/src_py/_debug.py index 3d39850a60..c8d73fa512 100644 --- a/src_py/_debug.py +++ b/src_py/_debug.py @@ -4,13 +4,14 @@ import sys import traceback import importlib -from typing import Tuple, Optional, Callable +from collections.abc import Callable +from typing import Optional from os import environ from pygame.version import ver from pygame.system import get_cpu_instruction_sets -ImportResult = Tuple[str, bool, Optional[Callable]] +ImportResult = tuple[str, bool, Optional[Callable]] def str_from_tuple(version_tuple): diff --git a/src_py/pkgdata.py b/src_py/pkgdata.py index 8076900e3e..7cff0c383f 100644 --- a/src_py/pkgdata.py +++ b/src_py/pkgdata.py @@ -22,27 +22,16 @@ def getResource(identifier, pkgname=__name__): import os try: - if sys.version_info[:2] > (3, 8): - from importlib.resources import files - - def resource_exists(_package_or_requirement, _resource_name): - _package_or_requirement = _package_or_requirement.split(".")[0] - return files(_package_or_requirement).joinpath(_resource_name).is_file() - - def resource_stream(_package_or_requirement, _resource_name): - _package_or_requirement = _package_or_requirement.split(".")[0] - ref = files(_package_or_requirement).joinpath(_resource_name) - return ref.open('rb') - else: - from importlib import resources - - def resource_exists(_package_or_requirement, _resource_name): - _package_or_requirement = _package_or_requirement.split(".")[0] - return resources.is_resource(_package_or_requirement, _resource_name) # pylint: disable=deprecated-method - - def resource_stream(_package_or_requirement, _resource_name): - _package_or_requirement = _package_or_requirement.split(".")[0] - return resources.open_binary(_package_or_requirement, _resource_name) # pylint: disable=deprecated-method + from importlib.resources import files + + def resource_exists(_package_or_requirement, _resource_name): + _package_or_requirement = _package_or_requirement.split(".")[0] + return files(_package_or_requirement).joinpath(_resource_name).is_file() + + def resource_stream(_package_or_requirement, _resource_name): + _package_or_requirement = _package_or_requirement.split(".")[0] + ref = files(_package_or_requirement).joinpath(_resource_name) + return ref.open("rb") except ImportError: