Skip to content

Commit 149b24a

Browse files
committed
pybricksdev: replace Union with | for type hints
Replace typing.Union with the | operator for type hints. This is possible now that we require Python 3.10 or later.
1 parent 59bc299 commit 149b24a

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
("py:class", "bleak.backends.device.BLEDevice"),
6666
("py:exc", "asyncio.TimeoutError"),
6767
("py:class", "bleak.BleakClient"),
68-
("py:obj", "typing.Union"),
6968
("py:class", "os.PathLike"),
7069
("py:obj", "typing.BinaryIO"),
7170
("py:class", "BinaryIO"), # yes, we need both!

pybricksdev/ble/lwp3/bytecodes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""
1212

1313
from enum import IntEnum, IntFlag, unique
14-
from typing import Union
1514

1615

1716
def _create_pseudo_member_(cls: type[IntEnum], value: int) -> IntEnum:
@@ -122,7 +121,7 @@ class BluetoothAddress(bytes):
122121
identifying individual Bluetooth devices instead of network cards.
123122
"""
124123

125-
def __new__(cls, value: Union[str, bytes]) -> "BluetoothAddress":
124+
def __new__(cls, value: str | bytes) -> "BluetoothAddress":
126125
if isinstance(value, str):
127126
# if it is a string, assume the format "XX:XX:XX:XX:XX:XX"
128127
value = [int(x, 16) for x in value.split(":")]

pybricksdev/ble/lwp3/messages.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import abc
1414
import struct
1515
from enum import IntEnum
16-
from typing import Any, NamedTuple, Union, overload
16+
from typing import Any, NamedTuple, overload
1717

1818
from pybricksdev.ble.lwp3.bytecodes import (
1919
MAX_NAME_SIZE,
@@ -1211,7 +1211,7 @@ def __repr__(self) -> str:
12111211

12121212

12131213
class PortValueMessage(AbstractMessage):
1214-
def __init__(self, port: PortID, fmt: str, *values: Union[int, float]) -> None:
1214+
def __init__(self, port: PortID, fmt: str, *values: int | float) -> None:
12151215
super().__init__(4 + struct.calcsize(fmt), MessageKind.PORT_VALUE)
12161216

12171217
self._data[3] = port
@@ -1221,7 +1221,7 @@ def __init__(self, port: PortID, fmt: str, *values: Union[int, float]) -> None:
12211221
def port(self) -> PortID:
12221222
return PortID(self._data[3])
12231223

1224-
def unpack(self, fmt: str) -> tuple[Union[int, float], ...]:
1224+
def unpack(self, fmt: str) -> tuple[int | float, ...]:
12251225
return struct.unpack_from(fmt, self._data, 4)
12261226

12271227
def __repr__(self) -> str:
@@ -1232,7 +1232,7 @@ def __repr__(self) -> str:
12321232

12331233
class PortValueComboMessage(AbstractMessage):
12341234
def __init__(
1235-
self, port: PortID, modes: list[int], fmt: str, *values: Union[int, float]
1235+
self, port: PortID, modes: list[int], fmt: str, *values: int | float
12361236
) -> None:
12371237
super().__init__(6 + struct.calcsize(fmt), MessageKind.PORT_VALUE_COMBO)
12381238

@@ -1253,7 +1253,7 @@ def modes(self) -> list[int]:
12531253
(flags,) = struct.unpack_from("<H", self._data, 4)
12541254
return [m for m in range(16) if flags & (1 << m)]
12551255

1256-
def unpack(self, fmt: str) -> tuple[Union[int, float], ...]:
1256+
def unpack(self, fmt: str) -> tuple[int | float, ...]:
12571257
return struct.unpack_from(fmt, self._data, 6)
12581258

12591259
def __repr__(self) -> str:
@@ -1450,7 +1450,7 @@ def __init__(
14501450
end: EndInfo,
14511451
mode: int,
14521452
fmt: str,
1453-
*values: Union[int, float],
1453+
*values: int | float,
14541454
) -> None:
14551455
super().__init__(
14561456
7 + struct.calcsize(fmt),
@@ -1467,7 +1467,7 @@ def __init__(
14671467
def mode(self) -> int:
14681468
return self._data[6]
14691469

1470-
def unpack(self, fmt: str) -> tuple[Union[int, float], ...]:
1470+
def unpack(self, fmt: str) -> tuple[int | float, ...]:
14711471
return struct.unpack_from(fmt, self._data, 7)
14721472

14731473
def __repr__(self) -> str:
@@ -1577,7 +1577,7 @@ class _Lookup(NamedTuple):
15771577
index: int
15781578
"""The index of the bytecode that determines the type."""
15791579

1580-
value: Union[dict[IntEnum, type[AbstractMessage]], dict[IntEnum, "_Lookup"]]
1580+
value: dict[IntEnum, type[AbstractMessage]] | dict[IntEnum, "_Lookup"]
15811581
"""
15821582
A dictionary mapping a bytecode to the cooresponding Python type if the type can be determined or
15831583
a dictionary mapping a bytecode to another lookup if more discrimination is required.

pybricksdev/compile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
import os
77
from modulefinder import ModuleFinder
8-
from typing import Union
98

109
import mpy_cross_v5
1110
import mpy_cross_v6
@@ -81,7 +80,7 @@ async def compile_file(
8180
return mpy
8281

8382

84-
async def compile_multi_file(path: str, abi: Union[int, tuple[int, int]]):
83+
async def compile_multi_file(path: str, abi: int | tuple[int, int]):
8584
"""Compiles a Python file and its dependencies with ``mpy-cross``.
8685
8786
On the hub, all dependencies behave as independent modules. Any (leading)

pybricksdev/firmware.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import struct
1212
import sys
1313
import zipfile
14-
from typing import BinaryIO, Literal, TypedDict, Union
14+
from typing import BinaryIO, Literal, TypedDict
1515

1616
if sys.version_info < (3, 10):
1717
from typing_extensions import TypeGuard
@@ -98,17 +98,17 @@ class FirmwareMetadataV210(
9898
"""
9999

100100

101-
AnyFirmwareV1Metadata = Union[FirmwareMetadataV100, FirmwareMetadataV110]
101+
AnyFirmwareV1Metadata = FirmwareMetadataV100 | FirmwareMetadataV110
102102
"""
103103
Type for data contained in ``firmware.metadata.json`` files of any 1.x version.
104104
"""
105105

106-
AnyFirmwareV2Metadata = Union[FirmwareMetadataV200, FirmwareMetadataV210]
106+
AnyFirmwareV2Metadata = FirmwareMetadataV200 | FirmwareMetadataV210
107107
"""
108108
Type for data contained in ``firmware.metadata.json`` files of any 2.x version.
109109
"""
110110

111-
AnyFirmwareMetadata = Union[AnyFirmwareV1Metadata, AnyFirmwareV2Metadata]
111+
AnyFirmwareMetadata = AnyFirmwareV1Metadata | AnyFirmwareV2Metadata
112112
"""
113113
Type for data contained in ``firmware.metadata.json`` files of any version.
114114
"""
@@ -233,7 +233,7 @@ async def _create_firmware_v2(
233233

234234

235235
async def create_firmware_blob(
236-
firmware_zip: Union[str, os.PathLike, BinaryIO], name: str | None = None
236+
firmware_zip: str | os.PathLike | BinaryIO, name: str | None = None
237237
) -> tuple[bytes, AnyFirmwareMetadata, str]:
238238
"""Creates a firmware blob from base firmware and an optional custom name.
239239

0 commit comments

Comments
 (0)