From 6dd0de0f0f8595d4e464481a75c06dcd07cdf691 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 24 Feb 2022 21:45:55 -0500 Subject: [PATCH] allow use in Python 3.7 --- circuitpython_typing/__init__.py | 16 +++++++++++++--- circuitpython_typing/socket.py | 9 ++++++++- requirements.txt | 2 ++ setup.py | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/circuitpython_typing/__init__.py b/circuitpython_typing/__init__.py index 7f922aa..9c03d66 100644 --- a/circuitpython_typing/__init__.py +++ b/circuitpython_typing/__init__.py @@ -14,7 +14,15 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Typing.git" -from typing import Union, Protocol, Optional + +from typing import Union, Optional + +# Protocol was introduced in Python 3.8. +try: + from typing import Protocol +except ImportError: + from typing_extensions import Protocol + from array import array ReadableBuffer = Union[bytes, bytearray, memoryview, array] @@ -42,7 +50,8 @@ class ByteStream(Protocol): * `usb_cdc.Serial` """ - def read(self, count: Optional[int] = None, /) -> Optional[bytes]: + # Should be `, /)`, but not available in Python 3.7. + def read(self, count: Optional[int] = None) -> Optional[bytes]: """Read ``count`` bytes from the stream. If ``count`` bytes are not immediately available, or if the parameter is not specified in the call, @@ -50,6 +59,7 @@ def read(self, count: Optional[int] = None, /) -> Optional[bytes]: """ ... - def write(self, buf: ReadableBuffer, /) -> Optional[int]: + # Should be `, /)`, but not available in Python 3.7. + def write(self, buf: ReadableBuffer) -> Optional[int]: """Write the bytes in ``buf`` to the stream.""" ... diff --git a/circuitpython_typing/socket.py b/circuitpython_typing/socket.py index 84c7473..f096b04 100644 --- a/circuitpython_typing/socket.py +++ b/circuitpython_typing/socket.py @@ -11,7 +11,14 @@ from ssl import SSLContext from types import ModuleType -from typing import Any, Optional, Protocol, Tuple, Union +from typing import Any, Optional, Tuple, Union + +# Protocol was introduced in Python 3.8. +try: + from typing import Protocol +except ImportError: + from typing_extensions import Protocol + # Based on https://github.com/python/typeshed/blob/master/stdlib/_socket.pyi diff --git a/requirements.txt b/requirements.txt index 8a71bbb..3435401 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,5 @@ # SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries # # SPDX-License-Identifier: MIT + +typing_extensions; python_version <= '3.7' diff --git a/setup.py b/setup.py index bab5ae1..fae35dc 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ # Author details author="Adafruit Industries", author_email="circuitpython@adafruit.com", - install_requires=[], + install_requires=["typing_extensions; python_version <= '3.7'"], # Choose your license license="MIT", # See https://pypi.python.org/pypi?%3Aaction=list_classifiers