-
Notifications
You must be signed in to change notification settings - Fork 192
Is there a smarter way to handle the Optional type for shape.body? #279
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
Comments
From my experience with type hinting, for the given code there is not a way to avoid one of the following:
If I encounter an annoying optional, I either try to replace it with raising an error, provide/define another function that does the narrowing, replace with an iterable, or rewrite the code to avoid it. From these options, I see solutions to your examples.
space = pymunk.Space()
for body in space.bodies:
position = body.position
for shape in body.shapes:
...
class Arbiter:
...
@property
def bodies(self) -> Tuple["Body", "Body"]: ... (side note: in my code, for performance reasons, the arbiter proxy caches the shapes because they are accessed at least once and are used in additional properties) |
This reminds me of an annoyance I've had: the QueryInfo types all have optional |
Ah true! Only one case did actually use the |
In addition to the Arbiter case there's at least two more cases I can think of:
Im a little hesitant to add a body shortcut to |
Answering myself about the QueryInfo objects: Maybe the |
I would avoid returning heterogenous raw tuples,
If not adding a new query info, then possibly a I do think it is beneficial to add
As always, the impact of these shortcuts should be weighed against the extra convenience of avoiding an optional check per access. |
A, there's one issue, you are allowed to update the values. This is why they are not NamedTuples. I guess this is not be most well known feature, and its not very well documented, but it should work.
Ah, maybe this would be a good idea. While you are allowed to change properties on the points, you are not allowed to change the number of points, so a tuple could make it clearer.
A, yes, should be fixed.
Good comment. In general I dont think ctrl-f to look around is so bad, but maybe its a good idea to break it up more now. It has had this design for a very long time, and at that time there were much less docs.
Yeah, I think I will add it, it does make sense as you note. ps. |
Hm, this still confuses me. I see that
That sounds awful. |
Yeah, cant say its a perfect API here. Underneath it wont matter if you make a new point set or modify the one you got out of the arbiter. In the end it will copy over the values to an internal struct anyway. But it is easier to to reuse the existing one than to make a new, since most likely you want to only modify some parts of it (you are not even allowed to modify everything, i.e. the number of points must be the same). The reason for this style is that its how it works in Chipmunk, you get a struct out, and you can pass a struct in. I'm open for adjustments here, but its not clear to me how. Since you get a copy it will live as long as you like and you don't need to think about lifetime like with the Arbiter. And you are in control when to assign its values back and so on.
Yeah, its very annoying and time consuming to debug this. If it happened on all os/pythons it would be so much easier, and if it was not GC-triggered seg faults in the c code it would also be much easier.. The current one is a good example: |
Closing this one now since a number of changes were committed. |
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.
Is there some smarter way to handle this to reduce the useless asserts?
The text was updated successfully, but these errors were encountered: