- Notifications
You must be signed in to change notification settings - Fork3.8k
Fix Vector subclass methods to return the correct subtype instance#3088
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
I think this is ready to be reviewed. Note to the person who will be pressing the Merge button, can you merge and squash all commits into one so the commit history doesn't look so messy? I made a mistake with merging branches. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Needs some unit tests too
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
This is starting to look really clean! As Ankith mentioned, it needs tests. It could also probably use a note in the docs. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM! Thanks for the PR 👍 🎉
Starbuck5 commentedMar 27, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
You got the explicit methods, but it still fails on math. It passes |
Uh oh!
There was an error while loading.Please reload this page.
MyreMylar left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This resolves the original issue mentioned however I noticed some inconsistency in whether we now return a VectorN or whether we return a subtype.
For example:
c = MyVector(5,10)d = c.copy()type(d)<class '__main__.MyVector'>e = c + dtype(e)<class 'pygame.math.Vector2'>Why does adding two MyVector objects produce a Vector2? Is this what we want to happen or should it also return an instance of the subtype? There are still a handful of places (including these basic math ops) where we are returning an instance of the parent class rather than the subclass and I think we should definitely have a solid think about each one.
Apologies if this has already been done somewhere, but I couldn't see it in the PR comment thread.
Co-Authored-By: Ankith <46915066+ankith26@users.noreply.github.com>
Yup, Starbuck5 has mentioned this issue. I'm currently working on a solution to that.
Nope, this isn't what is supposed to happen. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
In addition to the generic math function not being fixed that is already mentioned before, I noticed that vector swizzling also returns a pygame vector instance... which should probably be fixed too...
>>> class MyVec(pygame.Vector2): pass... >>> MyVec(2, 3).xx <Vector2(2, 2)>And there are some elementwiseproxy functions too that seem to deal with vectors directly
There are a few more that needs to support subtype, but I'm pushing my latest changes to show some progress. |
andrewhong04 commentedApr 19, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
andrewhong04 commentedApr 20, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There's a little bit of an oopsie with the subclassing PR, so for swizzling, let's say you have this piece of code classMyVector2(pygame.Vector2):passprint(MyVector2(3,5).xy) This piece of code will work just fine, assuming it'll return a print(MyVector2(3,5).xyx This will return a Any ideas on this problem? Should we disable swizzling if the Vector class has been subclassed, only return |
Something strange happened on my end while trying to push my new commits, I thought I initially squashed some of these commits into one and they just somehow reappeared? I think it's best to squash and merge this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Since I see this function call pattern(pgVector *)_vector_subtype_new(Py_TYPE(var), var->dim); is almost universal in this file, I'd suggest implementing the function signature likepgVector *_vector_subtype_new(pgVector *base);
so that the call simplifies to_vector_subtype_new(var);
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Also needs typehint changes in addition to some extra reviews I left
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Co-Authored-By: Ankith <46915066+ankith26@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for sticking through this Novial! This is cool. 🎉
The type test might want a test that the swizzle type is unrelated to the subtype, since we've decided to do it that way.
Ankith says something about needing type hints, I'm not sure how that would work at all. Typing makes my brain hurt.
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM! Thanks for the PR 👍 🎉
(I mentioned that typestubs need fixes after this PR, but as discussed on discord I will be opening a new PR for that)
For those OOTL, we discussed swizzling return types on discord, and the current decision is to keep old behaviour for swizzling, returning This PR is bit of an API change, but it's a step in the right direction. User code that subclasses vectors should not break if those subclasses were designed with Liskov substitution principle in mind |

Fix:#2996