|
1 | 1 | from os import PathLike |
2 | | -from typing import IO, Callable, Sequence, Tuple, Union |
| 2 | +from typing import IO, Callable, Tuple, Union, TypeVar |
3 | 3 |
|
4 | | -from typing_extensions import Literal as Literal |
| 4 | +from typing_extensions import Literal as Literal, SupportsIndex as SupportsIndex |
5 | 5 | from typing_extensions import Protocol |
6 | 6 |
|
7 | | -from pygame.color import Color |
8 | | -from pygame.math import Vector2 |
9 | | -from pygame.rect import Rect, FRect |
10 | | - |
11 | 7 | # For functions that take a file name |
12 | 8 | AnyPath = Union[str, bytes, PathLike[str], PathLike[bytes]] |
13 | 9 |
|
14 | 10 | # Most pygame functions that take a file argument should be able to handle |
15 | 11 | # a FileArg type |
16 | 12 | FileArg = Union[AnyPath, IO[bytes], IO[str]] |
17 | 13 |
|
18 | | -Coordinate = Union[Tuple[float, float], Sequence[float], Vector2] |
| 14 | +_T = TypeVar("_T", covariant=True) |
| 15 | + |
| 16 | +class Sequence(Protocol[_T]): |
| 17 | + """ |
| 18 | + This is different from the standard python 'Sequence' abc. This is used in places |
| 19 | + where only __getitem__ and __len__ is actually needed (basically almost all places |
| 20 | + where a sequence is used). The standard 'Sequence' abc has some extra methods. |
| 21 | + """ |
| 22 | + def __getitem__(self, __i: SupportsIndex) -> _T: ... |
| 23 | + def __len__(self) -> int: ... |
| 24 | + |
| 25 | +# Right now, it isn't possible to annotate sizes (popular tools don't support it) but |
| 26 | +# when it is, the below types should be appropriately annotated |
| 27 | + |
| 28 | +# Yes, float. The reason being, pygame handles float without erroring in a lot of places |
| 29 | +# where a coordinate is expected (usually by rounding to int). |
| 30 | +# Also, 'Union[int, float] == float' |
| 31 | +Coordinate = Sequence[float] |
| 32 | + |
| 33 | +# This is used in places where ints are strictly required |
| 34 | +IntCoordinate = Sequence[int] |
19 | 35 |
|
20 | | -# This typehint is used when a function would return an RGBA tuble |
| 36 | +# This typehint is used when a function would return an RGBA tuple |
21 | 37 | RGBAOutput = Tuple[int, int, int, int] |
22 | | -ColorValue = Union[Color, int, str, Tuple[int, int, int], RGBAOutput, Sequence[int]] |
23 | | - |
24 | | -_CanBeRect = Union[ |
25 | | - Rect, |
26 | | - FRect, |
27 | | - Tuple[int, int, int, int], |
28 | | - Tuple[float, float, float, float], |
29 | | - Tuple[Coordinate, Coordinate], |
30 | | - Sequence[int], |
31 | | - Sequence[float], |
32 | | - Sequence[Coordinate], |
33 | | -] |
| 38 | +ColorValue = Union[int, str, Sequence[int]] |
| 39 | + |
| 40 | +_CanBeRect = Sequence[Union[float, Coordinate]] |
34 | 41 |
|
35 | 42 | class _HasRectAttribute(Protocol): |
36 | 43 | # An object that has a rect attribute that is either a rect, or a function |
37 | 44 | # that returns a rect confirms to the rect protocol |
38 | 45 | rect: Union[RectValue, Callable[[], RectValue]] |
39 | 46 |
|
40 | 47 | RectValue = Union[_CanBeRect, _HasRectAttribute] |
| 48 | + |
| 49 | +""" |
| 50 | +# testing code |
| 51 | +def a(b: Coordinate): |
| 52 | + b[0] |
| 53 | + b[1] |
| 54 | + len(b) |
| 55 | + e1, e2 = b |
| 56 | + for i in b: |
| 57 | + i -= 1 |
| 58 | +
|
| 59 | +
|
| 60 | +import numpy |
| 61 | +from pygame import Vector2 |
| 62 | +
|
| 63 | +class MyAmoger: |
| 64 | + def __init__(self): |
| 65 | + pass |
| 66 | +
|
| 67 | + def __getitem__(self, index): |
| 68 | + if index not in (0, 1): |
| 69 | + raise IndexError() |
| 70 | +
|
| 71 | + return 42 if index else 69 |
| 72 | +
|
| 73 | + def __len__(self): |
| 74 | + return 2 |
| 75 | +
|
| 76 | +
|
| 77 | +# should pass |
| 78 | +a([1, 2]) |
| 79 | +a([4.2, 5.2]) |
| 80 | +a((1, 2)) |
| 81 | +a((1.4, 2.8)) |
| 82 | +a(MyAmoger()) |
| 83 | +a(range(2, 4)) # yes, range object is a 'Sequence' |
| 84 | +a(numpy.array([1.3, 2.1])) |
| 85 | +a(b"ab") # weird but this actually works in code (this represents (97, 98) btw) |
| 86 | +a(bytearray([1, 2])) |
| 87 | +a(Vector2()) |
| 88 | +
|
| 89 | +print("Done testing the passes!") |
| 90 | +
|
| 91 | +# should technically error, but right now we can't annotate sizes so they pass on |
| 92 | +# type testing |
| 93 | +a([1, 2, 3]) |
| 94 | +a([4.2, 5.2, 2, 4]) |
| 95 | +a((1,)) |
| 96 | +a(numpy.array([1.3, 2.1, 4.2])) |
| 97 | +
|
| 98 | +# all of the below should always error |
| 99 | +a({}) |
| 100 | +a({1: 2}) |
| 101 | +a("abc") |
| 102 | +a({1, 2}) |
| 103 | +
|
| 104 | +""" |
0 commit comments