Skip to content

Is there a smarter way to handle the Optional type for shape.body? #279

@viblo

Description

@viblo

Quite often you iterate over the shapes of the space, or maybe you get a shape out from a arbiter, and then you need to fetch the body of the shape. In these cases you know for sure that the body is set, because a shape cannot be added to a space unless it has a body which is also added to the space. But the type checked doesn't know this and will warn that it might be None.

space = pymunk.Space()
for shape in space.shapes
    position = shape.body.position  # type error here since I didnt assert body is not None.

Is there some smarter way to handle this to reduce the useless asserts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions