Skip to content

Commit 0351e41

Browse files
Fix compatibility with older Python versions (#18)
* fix(typing): fallback to `typing_extensions` when importing Self * fix(typing): replace 3.10 union types with older-compatible `Union` types References #17 --------- Co-authored-by: Brendan <2bndy5@gmail.com>
1 parent 7cbf31a commit 0351e41

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

circuitpython_mocks/_mixins.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from collections import deque
2-
from typing import Self, Deque, Union, TYPE_CHECKING
2+
from typing import Deque, Union, TYPE_CHECKING
3+
4+
try:
5+
from typing import Self
6+
except ImportError: # pragma: no cover
7+
from typing_extensions import Self
8+
39

410
if TYPE_CHECKING:
511
from circuitpython_mocks.busio.operations import (

circuitpython_mocks/busio/__init__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from enum import Enum, auto
2222
import sys
23-
from typing import List, Optional
23+
from typing import List, Optional, Union
2424

2525
import circuitpython_typing
2626

@@ -189,8 +189,8 @@ def __new__(cls, clock: Pin, MOSI: Pin, MISO: Pin, **kwargs) -> "SPI":
189189
def __init__(
190190
self,
191191
clock: Pin,
192-
MOSI: Pin | None = None,
193-
MISO: Pin | None = None,
192+
MOSI: Union[Pin, None] = None,
193+
MISO: Union[Pin, None] = None,
194194
half_duplex: bool = False,
195195
):
196196
"""A class to mock :external:py:class:`busio.SPI`."""
@@ -297,16 +297,16 @@ class Parity(Enum):
297297

298298
def __init__(
299299
self,
300-
tx: Pin | None = None,
301-
rx: Pin | None = None,
300+
tx: Union[Pin, None] = None,
301+
rx: Union[Pin, None] = None,
302302
*,
303-
rts: Pin | None = None,
304-
cts: Pin | None = None,
305-
rs485_dir: Pin | None = None,
303+
rts: Union[Pin, None] = None,
304+
cts: Union[Pin, None] = None,
305+
rs485_dir: Union[Pin, None] = None,
306306
rs485_invert: bool = False,
307307
baudrate: int = 9600,
308308
bits: int = 8,
309-
parity: Parity | None = None,
309+
parity: Union[Parity, None] = None,
310310
stop: int = 1,
311311
timeout: float = 1,
312312
receiver_buffer_size: int = 64,
@@ -315,7 +315,7 @@ def __init__(
315315
self._timeout = timeout
316316
super().__init__()
317317

318-
def read(self, nbytes: int | None = None) -> Optional[bytes]:
318+
def read(self, nbytes: Union[int, None] = None) -> Optional[bytes]:
319319
"""A function that mocks :external:py:meth:`busio.UART.read()`.
320320
321321
.. mock-expects::
@@ -362,7 +362,7 @@ def readline(self) -> Optional[bytes]:
362362
op.assert_response(buf, 0, len_buf)
363363
return None if buf else bytes(buf)
364364

365-
def write(self, buf: circuitpython_typing.ReadableBuffer) -> int | None:
365+
def write(self, buf: circuitpython_typing.ReadableBuffer) -> Union[int, None]:
366366
"""A function that mocks :external:py:meth:`busio.UART.write()`.
367367
368368
.. mock-expects::

circuitpython_mocks/digitalio/operations.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from typing import Union
2+
3+
14
class _State:
2-
def __init__(self, state: bool | int) -> None:
5+
def __init__(self, state: Union[bool, int]) -> None:
36
self.state = bool(state)
47

5-
def assert_state(self, value: bool | int):
8+
def assert_state(self, value: Union[bool, int]):
69
assert self.state is bool(
710
value
811
), "Expected pin state does not match given pin state"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
adafruit-circuitpython-typing
22
pytest
3+
typing-extensions~=4.0

0 commit comments

Comments
 (0)