Skip to content

Commit

Permalink
Fix compatibility with older Python versions (#18)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
sworisbreathing and 2bndy5 authored Sep 15, 2024
1 parent 7cbf31a commit 0351e41
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
8 changes: 7 additions & 1 deletion circuitpython_mocks/_mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from collections import deque
from typing import Self, Deque, Union, TYPE_CHECKING
from typing import Deque, Union, TYPE_CHECKING

try:
from typing import Self
except ImportError: # pragma: no cover
from typing_extensions import Self


if TYPE_CHECKING:
from circuitpython_mocks.busio.operations import (
Expand Down
22 changes: 11 additions & 11 deletions circuitpython_mocks/busio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from enum import Enum, auto
import sys
from typing import List, Optional
from typing import List, Optional, Union

import circuitpython_typing

Expand Down Expand Up @@ -189,8 +189,8 @@ def __new__(cls, clock: Pin, MOSI: Pin, MISO: Pin, **kwargs) -> "SPI":
def __init__(
self,
clock: Pin,
MOSI: Pin | None = None,
MISO: Pin | None = None,
MOSI: Union[Pin, None] = None,
MISO: Union[Pin, None] = None,
half_duplex: bool = False,
):
"""A class to mock :external:py:class:`busio.SPI`."""
Expand Down Expand Up @@ -297,16 +297,16 @@ class Parity(Enum):

def __init__(
self,
tx: Pin | None = None,
rx: Pin | None = None,
tx: Union[Pin, None] = None,
rx: Union[Pin, None] = None,
*,
rts: Pin | None = None,
cts: Pin | None = None,
rs485_dir: Pin | None = None,
rts: Union[Pin, None] = None,
cts: Union[Pin, None] = None,
rs485_dir: Union[Pin, None] = None,
rs485_invert: bool = False,
baudrate: int = 9600,
bits: int = 8,
parity: Parity | None = None,
parity: Union[Parity, None] = None,
stop: int = 1,
timeout: float = 1,
receiver_buffer_size: int = 64,
Expand All @@ -315,7 +315,7 @@ def __init__(
self._timeout = timeout
super().__init__()

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

def write(self, buf: circuitpython_typing.ReadableBuffer) -> int | None:
def write(self, buf: circuitpython_typing.ReadableBuffer) -> Union[int, None]:
"""A function that mocks :external:py:meth:`busio.UART.write()`.
.. mock-expects::
Expand Down
7 changes: 5 additions & 2 deletions circuitpython_mocks/digitalio/operations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import Union


class _State:
def __init__(self, state: bool | int) -> None:
def __init__(self, state: Union[bool, int]) -> None:
self.state = bool(state)

def assert_state(self, value: bool | int):
def assert_state(self, value: Union[bool, int]):
assert self.state is bool(
value
), "Expected pin state does not match given pin state"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
adafruit-circuitpython-typing
pytest
typing-extensions~=4.0

0 comments on commit 0351e41

Please sign in to comment.