Skip to content

Commit 36d6ec8

Browse files
authored
Use 3.12's buffer protocol annotation if available (#1927)
* Import the new Buffer protocol type on Python >= 3.12 * Explain why the typing_extensions stub is worse for arcade than a Union
1 parent 5c33fc9 commit 36d6ec8

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

arcade/types.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import annotations
77

8+
import sys
89
from array import array
910
import ctypes
1011
import random
@@ -449,11 +450,11 @@ class TiledObject(NamedTuple):
449450
type: Optional[str] = None
450451

451452

452-
# This is a temporary workaround for the lack of a way to type annotate
453-
# objects implementing the buffer protocol. Although there is a PEP to
454-
# add typing, it is scheduled for 3.12. Since that is years away from
455-
# being our minimum Python version, we have to use a workaround. See
456-
# the PEP and Python doc for more information:
457-
# https://peps.python.org/pep-0688/
458-
# https://docs.python.org/3/c-api/buffer.html
459-
BufferProtocol = Union[ByteString, memoryview, array, ctypes.Array]
453+
if sys.version_info >= (3, 12):
454+
from collections.abc import Buffer as BufferProtocol
455+
else:
456+
# This is used instead of the typing_extensions version since they
457+
# use an ABC which registers virtual subclasses. This will not work
458+
# with ctypes.Array since virtual subclasses must be concrete.
459+
# See: https://peps.python.org/pep-0688/
460+
BufferProtocol = Union[ByteString, memoryview, array, ctypes.Array]

0 commit comments

Comments
 (0)