-
-
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
Typing problems in new pygame.typing
module
#3074
Comments
|
Feel free to open a PR for these issues, so that the fixes are correctly attributed to you :) |
Sure, I will work on a PR. After looking further into the problems (because my past testing did fail with the current implementation), I realize that you are mostly correct about 1, but maybe misunderstanding 4. 1. (Thanks for the correction.) The current implementation is nearly correct except for another thing: implicitly, the class _HasRect(Protocol):
@property
def rect(self) -> "RectLike" | Callable[[], "RectLike"]: ... 4. Sorry for the misunderstanding; def __getitem__(self, index: int, /) -> _T_co: ... Applying this would make Extra problem: Best practice is to put |
@aatle I don't want to sound stupid but why can't the rect attribute be writable? |
@damusss The |
I thought making it a property would disallow it being implemented writable, that makes sense then. Python typehinting is deeper than I thought lol |
I noticed a few fixable problems while reading the new
pygame.typing
module.Environment:
Commit 9208ee5 (not released yet)
Problems (4):
_HasRectAttribute
protocol is implemented wrongpygame-ce/src_py/typing.py
Lines 50 to 53 in 141a75d
The
Union
here means that a given class'srect
attribute must support holding both aRectLike
object and a function returning aRectLike
object. Most classes, includingpygame.Sprite
, would only support one of those, and so would be incompatible.In addition to this, the attribute must also be writeable, which is impossible if implemented as a method.
Possible fix:
Test case classes for mypy (all passed):
_CanBeRect
covers an invalid casepygame-ce/src_py/typing.py
Line 47 in 141a75d
Slightly imprecise;
[(1, 2), 3, 4]
would be compatible, but is not actually supported.Fixed:
Custom defined
_PathProtocol
uses a bound TypeVar instead of constrained TypeVarpygame-ce/src_py/typing.py
Lines 9 to 15 in 141a75d
As is common when supporting both
str
andbytes
, a constrained TypeVar should be used (which disallows mixing (str | bytes
) for the type var). (There is anAnyStr
type var included in the standardtyping
module, but we cannot use it since it is invariant and protocols can't have invariant type vars.)Modeling after
PathLike
definition in the python typeshed, type var fix:SequenceLike
protocol may be a bit strictProtocol methods:
pygame-ce/src_py/typing.py
Lines 31 to 32 in 141a75d
The
__getitem__
signature mandates that an adhering class's__getitem__
index parameter must be typed withSupportsIndex
. However, some classes do not useSupportsIndex
for the index parameter (usingint
orslice
instead), includingcollections.abc.Sequence
.This means that
collections.abc.Sequence
itself and a method signature like(self, index: int)
are not validSequenceLike
s.So, replacing
SupportsIndex
withint
will support more sequence classes, but at the cost of supporting less objects as indices.The text was updated successfully, but these errors were encountered: