Skip to content

check_for_collision_with_list does not return the right SpriteType #2576

Open
@sjrd

Description

Hi! First, thanks a lot for this library. I am using it this semester as a teaching vehicle at the university level. It is really nice. Unlike the platformer tutoriel, I actually mandate using mypy in strict mode for all students' code.


It seems to me that the type signature of arcade.check_for_collision_with_list is incorrect. It is defined as

def check_for_collision_with_list(
sprite: SpriteType,
sprite_list: SpriteList,
method: int = 0,
) -> List[SpriteType]:

You can see that it returns a List[SpriteType], where SpriteType is the type of the single sprite. However, in practice it returns elements of the given sprite_list.

Because of that, when trying to use it as follows:

    enemies: arcade.SpriteList[Enemy]

        for enemy in arcade.check_for_collision_with_list(weapon, self.enemies):
            enemy.hit_by_weapon() # method only available on Enemy; not on Sprite
            return

I get a type error telling me hit_by_weapon is not a method of Sprite.

I think the correct signature should be

def check_for_collision_with_list(
    sprite: BasicSprite,
    sprite_list: SpriteList[SpriteType],
    method: int = 0,
) -> List[SpriteType]:

Workaround

Use arcade.check_for_collision_with_lists(self, [self.enemies]) instead. For some reason the alternative method with an Iterable[SpriteList] has the correct type:

def check_for_collision_with_lists(
sprite: BasicSprite,
sprite_lists: Iterable[SpriteList[SpriteType]],
method=1,
) -> List[SpriteType]:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions