pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
939 stars 155 forks source link

Include the explicit type in `ColorLike` and `RectLike` #3183

Closed aatle closed 1 month ago

aatle commented 1 month ago

Add the concrete classes to the ColorLike and RectLike type unions. They are already implied by the other types, but should be added for clarity.

Since they are only type aliases to unions, their variable name is not always given, so what they represent might not be entirely obvious to users.

Bonus: Sort types in ColorLike by how often they are used.

Example mypy error message, before:

$ mypy test.py
test.py:38: error: Incompatible types in assignment (expression has type "float", variable has type "int | str | SequenceLike[int]")  [assignment]
test.py:39: error: Incompatible types in assignment (expression has type "int", variable has type "SequenceLike[float] | SequenceLike[SequenceLike[float]] | _HasRectAttribute")  [assignment]

After (much more clear!):

$ mypy test.py
test.py:38: error: Incompatible types in assignment (expression has type "float", variable has type "Color | SequenceLike[int] | str | int")  [assignment]
test.py:39: error: Incompatible types in assignment (expression has type "int", variable has type "Rect | FRect | SequenceLike[float] | SequenceLike[SequenceLike[float]] | _HasRectAttribute")  [assignment]

Note: typing.py now imports things from pygame. I don't think there is any risk of circular imports though.

Note 2: The _<Shape>Like type unions in geometry.pyi include their concrete classes already.

ankith26 commented 1 month ago

I'll just leave this PR open for a bit more before merging incase anyone else wants to chime in