pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

`pygame.sprite.Group.has()` method behavior for no sprites given

Open aatle opened this issue 7 months ago • 4 comments

Environment:
Platform:               Windows-11-10.0.22631-SP0
System:                 Windows
System Version:         10.0.22631
Processor:              Intel64 Family 6 Model 167 Stepping 1, GenuineIntel
Architecture:           Bits: 64bit     Linkage: WindowsPE

Python:                 CPython 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]
pygame version:         2.5.0
SDL versions:           Linked: 2.30.3  Compiled: 2.30.3
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:         Display Not Initialized
Mixer Driver:           Mixer Not Initialized

Current behavior:

According to the documentation, pygame.sprite.Group.has() (and __contains__) will return True if all given sprites are contained in the group, False otherwise. However, for the edge case of no sprites given (or equivalently, empty iterable), this method incorrectly returns False. This is probably what was intended, but is wrong.

In has() method: https://github.com/pygame-community/pygame-ce/blob/cdc27561f973bd062172c2e4b765e91090f6a8d9/src_py/sprite.py#L521-L522

Expected behavior:

The has() method should return True if no sprites (no arguments, empty group, etc.) are given. This is similar to the built-in all() function. The technical term is "vacuous truth". If you need more explanation, ask me.

Steps to reproduce:

import pygame
group = pygame.sprite.Group()
print('has():', group.has(), group.has([]), group.has(group))

aatle avatar Jul 14 '24 22:07 aatle