-
-
Notifications
You must be signed in to change notification settings - Fork 162
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
Add pygame.typing module #3002
Add pygame.typing module #3002
Conversation
So before we add a whole new module for this, we should think about some things. How do other libraries do this? Does this need to be a module or can type variables be scattered where they are relevant? Like pygame.rect.RectLike? How can we keep this stable? This exposes more stuff that we need to keep stable, are we okay with that? |
|
As for how other libraries do it, here's Note that the |
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.
Overall, I really like where this PR is going! I am approving the code changes from my side. I left a few reviews for improving the test file.
As this is a significant change, I would like for this PR to have 3 approving reviews.
Addressing some of Starbuck's points (in favour of this PR) from my POV
As Matt already pointed out,
This PR already adds a test file, that should help with maintaining stability. OFC, the file could be expanded in future iterations. |
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, I sense this will be a great addition to pygame-ce
Maybe the type aliases could be hinted as TypeAlias
es, but if type checkers don't complain then it's probably that important.
I suppose sometime in the future we could restructure this a bit so that there's no need to literally copy-paste code from typing.py
to typing.pyi
, but I suppose it should be fine for now.
Thanks! 🚀
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.
Overall LGTM, I left some requested changes for the documentation part.
I feel like for people reading the docs, a list of structure for each type in pygame.type
is a lot better for the user. Even though it's abstract and can technically handles an infinite number of type as long as it respects the structure, making a list of the most common types used feels essential for the user.
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.
pygame.gfxdraw
does not handle string colors and mapped color, only sequences of 3 or 4 values are accepted (RGB and RGBA).
so should i say supported by "almost" all pygame functions or do you want me to edit gfxdraw.pyi? the typehints were there already and they were wrong before this PR. |
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.
None of the sprite.pyi
types seem to be exposed, is this intended? I find it would be greatly beneficial to expose those types as well. It would help with some testing I plan to do regarding those types and thus it would require access to concrete objects.
Do you mean types such as _SuportsSprite and _SupportsDirtySprite? It is kinda intended. This PR only implements the contents of common.pyi to keep the scope simple. extra typings can be added later with other PRs based on their need (at the start of the PR I added a lot more types but keeping it small is better). @ankith26 might have a say in the matter. |
Fair enough, it can be handled in a later PR then 👍 |
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 PR still has my approval. This has been some nice work, thanks again for working on this! 😎
I think we can just merge it in for now (as it is a pretty safe change that wouldn't break any code) and see how it goes. If needed we can also mark this as experimental before the next release.
This PR would potentially implement #1682
As the title says, this PR adds the
typing
module to pygame. I've made some libraries myself and I often wanted to annotate properly things as colors and rects, but the types they accept are a wide range, especially the rect, so it would be really nice to putrect: pygame.typing.RectLike
in an argument for example. I see no reason not to have this.Details about the PR:
_common.pyi
have been ported. This can be easily expanded in the future._common.pyi
has been removedsrc_py/typing.py
has been added, being similar to_common.pyi
gen_stubs.py
has the functionality of copyingtyping.py
totyping.pyi
, to make type checkers happy__init__.py
was modified to correctly import the typing modulepygame.Rect
is preferred overpygame.typing.RectLike
, while if the object is going to be passed to a pygame function that acceptsRectLike
, there's no point in forcing apygame.Rect
. You get the idea.)Tests and docs have been added. Docs are currently temporary, but might not be when you reader read this message.