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

Added Rect typing and docs that it is iterable#2969

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
illume merged 2 commits intopygame:mainfromandrewhong04:novial-rect-typing
Feb 5, 2022
Merged

Added Rect typing and docs that it is iterable#2969

illume merged 2 commits intopygame:mainfromandrewhong04:novial-rect-typing
Feb 5, 2022

Conversation

@andrewhong04
Copy link
Contributor

Fix:#2681

@Starbuck5
Copy link
Contributor

Starbuck5 commentedJan 3, 2022
edited
Loading

Typing might want to be something like this instead:https://stackoverflow.com/questions/49427944/typehints-for-sized-iterable-in-python

MightyJosip or Ankith would have a better idea of this than I do.

Copy link
Contributor

@ankith26ankith26 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.

WhileRect is indeed iterable, the C code does not actually implement the__iter__ method as quickly demonstrated by this example.

>>> a = pygame.Rect(0, 0, 0, 0)>>> a.__iter__()Traceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: 'pygame.Rect' object has no attribute '__iter__'. Did you mean: '__str__'?

However, it does implement a subset of theSequence protocol, including a__len__ method, which does need typehints here. AnIterable would implement an__iter__ method, but aSequence only needs__getitem__ and python puts a mixin for__iter__ that uses this under the hood. Perhaps theRect typestub class should inherit from theSequence ABC?

andrewhong04 reacted with thumbs up emoji
Copy link
Member

@illumeillume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

🎉 👍

@ankith26
Copy link
Contributor

So I did more digging, and posting some notes of my findings here

  1. Objects that provide__getitem__ dunder (examples in pygame includeRect,Color,Vector and possibly more things) are practicallyIterable but they don't actually confirm tocollections.abc.Iterable if they don't provide an__iter__ method as also highlighted by thedocs for the same.
  2. This PR does not fix the issue at hand.mypy fails to recognise these pygame objects asIterable because they don't confirm tocollections.abc.Iterable and lack an__iter__ method. The only proper way to fix this is to implement an__iter__ function in C code for these objects, and also typehint it. I know some people are against making these objectsIterable, but the fact is that these are already mostly iterable. By putting an__iter__ method we will be explicitly declaring these as properIterables. We cannot make it not iterate and break lots of compatibility. Doing something likelist(colour_object) orx, y, w, h = rect_obj is fairly commonplace, and all these would break if we changed this

We can still merge this PR because adding__len__ typehint toRect is still a good addition, the iter fix can happen in future PR(s) because it involves changes to C code, tests, docs and types for many objects

@illume
Copy link
Member

I took this "We can still merge this PR" as an approval :)

Thankses.

andrewhong04 and ankith26 reacted with hooray emoji

@illumeillume merged commit43687d7 intopygame:mainFeb 5, 2022
@illumeillume added bug rectpygame.rect labelsOct 29, 2022
@illumeillume changed the titleAdded Rect typingAdded Rect typing that it is iterableOct 29, 2022
@illumeillume changed the titleAdded Rect typing that it is iterableAdded Rect typing and docs that it is iterableOct 29, 2022
@illumeillume added docs typesType hint checking related tasks labelsOct 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@illumeillumeillume approved these changes

@ankith26ankith26Awaiting requested review from ankith26

Assignees

No one assigned

Labels

bugdocsrectpygame.recttypesType hint checking related tasks

Projects

None yet

Milestone

2.1.3

Development

Successfully merging this pull request may close these issues.

Rect is iterable but not typed or documented

4 participants

@andrewhong04@Starbuck5@ankith26@illume

[8]ページ先頭

©2009-2025 Movatter.jp