Skip to content

Commit 96a6e86

Browse files
committed
pybricksdev: use Python 3.10 syntax for optionals
Replace `Optional[...]` with `... | None` since we now require Python 3.10+.
1 parent dab2dcc commit 96a6e86

File tree

8 files changed

+24
-25
lines changed

8 files changed

+24
-25
lines changed

pybricksdev/ble/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import asyncio
55
import logging
6-
from typing import Optional
76

87
from bleak import BleakClient, BleakScanner
98
from bleak.backends.device import BLEDevice
@@ -16,7 +15,7 @@
1615

1716

1817
async def find_device(
19-
name: Optional[str] = None,
18+
name: str | None = None,
2019
service: str = PYBRICKS_SERVICE_UUID,
2120
timeout: float = 10,
2221
) -> BLEDevice:

pybricksdev/ble/lwp3/messages.py

Lines changed: 6 additions & 6 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, Dict, List, NamedTuple, Optional, Tuple, Type, Union, overload
16+
from typing import Any, Dict, List, NamedTuple, Tuple, Type, Union, overload
1717

1818
from pybricksdev.ble.lwp3.bytecodes import (
1919
MAX_NAME_SIZE,
@@ -144,7 +144,7 @@ def __repr__(self) -> str:
144144
class _HubPropertyType(NamedTuple):
145145
type: type
146146
fmt: str
147-
max_size: Optional[int] = None
147+
max_size: int | None = None
148148

149149

150150
# specifies payload type information for each property
@@ -1500,10 +1500,10 @@ def __init__(
15001500
self,
15011501
port1: PortID,
15021502
feedback1: Feedback,
1503-
port2: Optional[PortID] = None,
1504-
feedback2: Optional[Feedback] = None,
1505-
port3: Optional[PortID] = None,
1506-
feedback3: Optional[Feedback] = None,
1503+
port2: PortID | None = None,
1504+
feedback2: Feedback | None = None,
1505+
port3: PortID | None = None,
1506+
feedback3: Feedback | None = None,
15071507
) -> None:
15081508
length = 5
15091509

pybricksdev/cli/flash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import zlib
1010
from tempfile import NamedTemporaryFile
11-
from typing import BinaryIO, Dict, Optional
11+
from typing import BinaryIO, Dict
1212

1313
from bleak import BleakClient, BleakScanner
1414
from bleak.backends.device import BLEDevice
@@ -421,7 +421,7 @@ async def tick(callback):
421421
print("Done.")
422422

423423

424-
async def flash_firmware(firmware_zip: BinaryIO, new_name: Optional[str]) -> None:
424+
async def flash_firmware(firmware_zip: BinaryIO, new_name: str | None) -> None:
425425
"""
426426
Command line tool for flashing firmware.
427427

pybricksdev/compile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import os
77
from modulefinder import ModuleFinder
8-
from typing import List, Optional, Tuple, Union
8+
from typing import List, Tuple, Union
99

1010
import mpy_cross_v5
1111
import mpy_cross_v6
@@ -30,7 +30,7 @@ def make_build_dir():
3030

3131

3232
async def compile_file(
33-
proj_dir: str, proj_path: str, abi: int, compile_args: Optional[List[str]] = None
33+
proj_dir: str, proj_path: str, abi: int, compile_args: list[str] | None = None
3434
):
3535
"""Compiles a Python file with ``mpy-cross``.
3636

pybricksdev/connections/ev3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import enum
66
import itertools
77
import struct
8-
from typing import Callable, Optional, Tuple
8+
from typing import Callable, Tuple
99

1010
import hid
1111

@@ -75,7 +75,7 @@ def close(self) -> None:
7575
"""
7676
self._device.close()
7777

78-
def _send_command(self, command: Command, payload: Optional[bytes] = None) -> int:
78+
def _send_command(self, command: Command, payload: bytes | None = None) -> int:
7979
length = 4
8080

8181
if payload is not None:
@@ -145,7 +145,7 @@ def _receive_reply(
145145
def download_sync(
146146
self,
147147
data: bytes,
148-
progress: Optional[Callable[[int], None]] = None,
148+
progress: Callable[[int], None] | None = None,
149149
) -> None:
150150
"""
151151
Blocking version of :meth:`download`.
@@ -169,7 +169,7 @@ def download_sync(
169169
async def download(
170170
self,
171171
data: bytes,
172-
progress: Optional[Callable[[int], None]] = None,
172+
progress: Callable[[int], None] | None = None,
173173
) -> None:
174174
"""
175175
Downloads a firmware blob to the EV3.

pybricksdev/connections/pybricks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
import os
88
import struct
9-
from typing import Awaitable, Callable, List, Optional, TypeVar
9+
from typing import Awaitable, Callable, List, TypeVar
1010

1111
import reactivex.operators as op
1212
import semver
@@ -76,7 +76,7 @@ class HubPowerButtonPressedError(RuntimeError):
7676
class PybricksHub:
7777
EOL = b"\r\n" # MicroPython EOL
7878

79-
fw_version: Optional[Version]
79+
fw_version: Version | None
8080
"""
8181
Firmware version of the connected hub or ``None`` if not connected yet.
8282
"""
@@ -580,7 +580,7 @@ async def download(self, script_path: str) -> None:
580580

581581
async def run(
582582
self,
583-
py_path: Optional[str] = None,
583+
py_path: str | None = None,
584584
wait: bool = True,
585585
print_output: bool = True,
586586
line_handler: bool = True,

pybricksdev/firmware.py

Lines changed: 4 additions & 4 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, List, Literal, Optional, Tuple, TypedDict, Union
14+
from typing import BinaryIO, List, Literal, Tuple, TypedDict, Union
1515

1616
if sys.version_info < (3, 10):
1717
from typing_extensions import TypeGuard
@@ -127,7 +127,7 @@ def _firmware_metadata_is_v2(
127127

128128

129129
async def _create_firmware_v1(
130-
metadata: AnyFirmwareV1Metadata, archive: zipfile.ZipFile, name: Optional[str]
130+
metadata: AnyFirmwareV1Metadata, archive: zipfile.ZipFile, name: str | None
131131
) -> bytearray:
132132
base = archive.open("firmware-base.bin").read()
133133

@@ -189,7 +189,7 @@ async def _create_firmware_v1(
189189

190190

191191
async def _create_firmware_v2(
192-
metadata: AnyFirmwareV2Metadata, archive: zipfile.ZipFile, name: Optional[str]
192+
metadata: AnyFirmwareV2Metadata, archive: zipfile.ZipFile, name: str | None
193193
) -> bytearray:
194194
base = archive.open("firmware-base.bin").read()
195195

@@ -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: Optional[str] = None
236+
firmware_zip: Union[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

pybricksdev/flash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import platform
88
import struct
99
from collections import namedtuple
10-
from typing import Dict, List, Optional, Tuple
10+
from typing import Dict, List, Tuple
1111

1212
from tqdm.auto import tqdm
1313
from tqdm.contrib.logging import logging_redirect_tqdm
@@ -47,7 +47,7 @@ def __init__(
4747
self.reply_len += 1
4848
self.write_with_response = write_with_response
4949

50-
def make_request(self, payload: Optional[bytes] = None) -> bytearray:
50+
def make_request(self, payload: bytes | None = None) -> bytearray:
5151
request = bytearray([self.command])
5252
if payload is not None:
5353
request += payload

0 commit comments

Comments
 (0)