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

TypeError: type 'Group' is not subscriptable #3235

Closed
AuthentiCuber opened this issue Nov 24, 2024 · 4 comments · Fixed by #3053
Closed

TypeError: type 'Group' is not subscriptable #3235

AuthentiCuber opened this issue Nov 24, 2024 · 4 comments · Fixed by #3053
Labels
bug Not working as intended sprite pygame.sprite typing pygame.typing
Milestone

Comments

@AuthentiCuber
Copy link

Environment:

pygame-ce 2.5.2 (SDL 2.30.8, Python 3.12.1)
Platform: Windows-11-10.0.22631-SP0
System: Windows
System Version: 10.0.22631
Processor: AMD64 Family 23 Model 104 Stepping 1, AuthenticAMD SSE2: Yes AVX2: Yes NEON: No
Architecture: Bits: 64bit Linkage: WindowsPE

Python: CPython 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]
pygame version: 2.5.2
SDL versions: Linked: 2.30.8 Compiled: 2.30.8
SDL Mixer versions: Linked: 2.8.0 Compiled: 2.8.0
SDL Font versions: Linked: 2.22.0 Compiled: 2.22.0
SDL Image versions: Linked: 2.8.2 Compiled: 2.8.2
Freetype versions: Linked: 2.11.1 Compiled: 2.11.1

Display Driver: windows
Mixer Driver: wasapi

  • Relevant hardware:
    None

Current behavior:

Attempting to type hint a pygame.sprite.Group like group: pygame.sprite.Group[sprite] results in TypeError: type 'Group' is not subscriptable

Removing the [sprite] works but mypy complains:
error: Missing type parameters for generic type "Group"

Expected behavior:

No errors would occur and the type checker would be happy with group: pygame.sprite.Group[sprite]

Screenshots

N/A

Steps to reproduce:

  1. Attempt to type hint a pygame.sprite.Group

Test code

import pygame

pygame.init()


class thing(pygame.sprite.Sprite):
    def __init__(self) -> None:
        super().__init__()
        self.image = pygame.Surface((10, 10))
        self.rect = self.image.get_frect()


group: pygame.sprite.Group[thing] = pygame.sprite.Group()

Stack trace/error output/other error logs

Traceback (most recent call last):
  File "C:\test.py", line 13, in <module>
    group: pygame.sprite.Group[thing] = pygame.sprite.Group()
           ~~~~~~~~~~~~~~~~~~~^^^^^^^
TypeError: type 'Group' is not subscriptable
@AuthentiCuber AuthentiCuber added the bug Not working as intended label Nov 24, 2024
@Starbuck5 Starbuck5 added sprite pygame.sprite typing pygame.typing labels Nov 24, 2024
@Starbuck5
Copy link
Member

Our very own @Matiiss has worked up a solution in #3053, but it hasn't passed review yet.

@AuthentiCuber
Copy link
Author

Oh ok, cool, thanks.

@Starbuck5
Copy link
Member

I'd like to keep this open until it actually is fixed. Thanks for reporting btw, user reports help us know what needs our collective focus / efforts.

@Starbuck5 Starbuck5 reopened this Nov 24, 2024
@Starbuck5 Starbuck5 added this to the 2.5.3 milestone Nov 24, 2024
@Matiiss
Copy link
Member

Matiiss commented Nov 24, 2024

In the mean time (apparently until 2.5.3 is released), you have two approaches to still use subscripts for type hints without running into runtime errors:

Quotes:

my_group: "pygame.sprite.Group[MySprite]" = pygame.sprite.Group()

from __future__ import annotations (this implicitly makes all type hints be strings pretty much):

from __future__ import annotations

...

my_group: pygame.sprite.Group[MySprite] = pygame.sprite.Group()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Not working as intended sprite pygame.sprite typing pygame.typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants