Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
ankith26 merged 18 commits intopygame:mainfromandrewhong04:vector-subclass
Apr 23, 2022
Merged

Fix Vector subclass methods to return the correct subtype instance#3088

ankith26 merged 18 commits intopygame:mainfromandrewhong04:vector-subclass
Apr 23, 2022

Conversation

@andrewhong04
Copy link
Contributor

Fix:#2996

Starbuck5 and ankith26 reacted with heart emoji
@andrewhong04
Copy link
ContributorAuthor

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.

Copy link
Contributor

@ankith26ankith26 left a 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

@Starbuck5
Copy link
Contributor

This is starting to look really clean! As Ankith mentioned, it needs tests. It could also probably use a note in the docs.

Copy link
Contributor

@ankith26ankith26 left a 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
Copy link
Contributor

Starbuck5 commentedMar 27, 2022
edited
Loading

You got the explicit methods, but it still fails on math.

>>> class VEC(pygame.Vector2):...     pass...>>> a = VEC(5,6)>>> b = VEC(7,8)>>> a + b<Vector2(12, 14)>>>> type(a + b)<class 'pygame.math.Vector2'>>>> d = a * 2>>> type(d)<class 'pygame.math.Vector2'>

It passes-a though. You should also add a test for that.

MyreMylar
MyreMylar previously requested changesApr 3, 2022
Copy link
Contributor

@MyreMylarMyreMylar left a comment
edited
Loading

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.

NovialRiptideand others added3 commitsApril 4, 2022 16:46
Co-Authored-By: Ankith <46915066+ankith26@users.noreply.github.com>
@andrewhong04
Copy link
ContributorAuthor

Apologies if this has already been done somewhere, but I couldn't see it in the PR comment thread.

Yup, Starbuck5 has mentioned this issue. I'm currently working on a solution to that.

is this what we want to happen or should it also return an instance of the subtype?

Nope, this isn't what is supposed to happen.

Copy link
Contributor

@ankith26ankith26 left a 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

@andrewhong04
Copy link
ContributorAuthor

There are a few more that needs to support subtype, but I'm pushing my latest changes to show some progress.

@andrewhong04
Copy link
ContributorAuthor

andrewhong04 commentedApr 19, 2022
edited
Loading

pygame math tests are failing right now, so please hold the review.
image

@andrewhong04
Copy link
ContributorAuthor

andrewhong04 commentedApr 20, 2022
edited
Loading

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 aMyVector2 type instead of apygame.Vector2, but what if we add this at the end?

print(MyVector2(3,5).xyx

This will return apygame.Vector3 type instead of aMyVector2 since it has 3 dimensions instead of 2.

Any ideas on this problem? Should we disable swizzling if the Vector class has been subclassed, only returnpygame.VectorN for swizzling, or keep it how I described it, returnpygame.VectorN if it can't return itself.

@andrewhong04
Copy link
ContributorAuthor

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.

Copy link
Contributor

@ankith26ankith26 left a 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 like
pgVector *_vector_subtype_new(pgVector *base);
so that the call simplifies to_vector_subtype_new(var);

Copy link
Contributor

@ankith26ankith26 left a 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

Co-Authored-By: Ankith <46915066+ankith26@users.noreply.github.com>
Copy link
Contributor

@Starbuck5Starbuck5 left a 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.

andrewhong04 reacted with hooray emoji
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
Copy link
Contributor

@ankith26ankith26 left a 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)

@ankith26
Copy link
Contributor

For those OOTL, we discussed swizzling return types on discord, and the current decision is to keep old behaviour for swizzling, returningVectorN instances even when vector subclasses are swizzled.

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

@illumeillume added the bug labelSep 12, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@MyreMylarMyreMylarAwaiting requested review from MyreMylar

2 more reviewers

@Starbuck5Starbuck5Starbuck5 approved these changes

@ankith26ankith26ankith26 approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

bugmathpygame.math

Projects

None yet

Milestone

2.1.3

Development

Successfully merging this pull request may close these issues.

Vector subclassing

5 participants

@andrewhong04@Starbuck5@ankith26@MyreMylar@illume

[8]ページ先頭

©2009-2025 Movatter.jp