Skip to content

Commit d93e8b6

Browse files
committed
repo: Apply PEP 604 for optional and union types
From Python 3.10 union types can be written with the | operator, and is the preferred notation for linters. Replace `Optional[type]` with `type | None`. Replace `Union[X, Y]` with `X | Y` Signed-off-by: Pieter De Gendt <[email protected]>
1 parent 4aaae11 commit d93e8b6

File tree

6 files changed

+121
-122
lines changed

6 files changed

+121
-122
lines changed

src/west/app/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from io import StringIO
2727
from pathlib import Path, PurePath
2828
from subprocess import CalledProcessError
29-
from typing import NamedTuple, Optional
29+
from typing import NamedTuple
3030

3131
import colorama
3232

@@ -84,9 +84,9 @@ class EarlyArgs(NamedTuple):
8484
# Expected arguments:
8585
help: bool # True if -h was given
8686
version: bool # True if -V was given
87-
zephyr_base: Optional[str] # -z argument value
87+
zephyr_base: str | None # -z argument value
8888
verbosity: int # 0 if not given, otherwise counts
89-
command_name: Optional[str]
89+
command_name: str | None
9090

9191
# Other arguments are appended here.
9292
unexpected_arguments: list[str]

src/west/commands.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from enum import IntEnum
1919
from pathlib import Path
2020
from types import ModuleType
21-
from typing import Callable, NoReturn, Optional
21+
from typing import Callable, NoReturn
2222

2323
import colorama
2424
import pykwalify
@@ -152,7 +152,7 @@ def __init__(self, name: str, help: str, description: str,
152152
self.accepts_unknown_args: bool = accepts_unknown_args
153153
self.requires_workspace = requires_workspace
154154
self.verbosity = verbosity
155-
self.topdir: Optional[str] = None
155+
self.topdir: str | None = None
156156
self.manifest = None
157157
self.config = None
158158
self._hooks: list[Callable[[WestCommand], None]] = []
@@ -170,8 +170,8 @@ def add_pre_run_hook(self,
170170

171171
def run(self, args: argparse.Namespace, unknown: list[str],
172172
topdir: PathType,
173-
manifest: Optional[Manifest] = None,
174-
config: Optional[Configuration] = None) -> None:
173+
manifest: Manifest | None = None,
174+
config: Configuration | None = None) -> None:
175175
'''Run the command.
176176
177177
This raises `west.commands.CommandContextError` if the command
@@ -269,7 +269,7 @@ def _get_manifest(self) -> Manifest:
269269
'Try "west -vv manifest --validate" to debug.')
270270
return self._manifest
271271

272-
def _set_manifest(self, manifest: Optional[Manifest]):
272+
def _set_manifest(self, manifest: Manifest | None):
273273
self._manifest = manifest
274274

275275
# Do not use @property decorator syntax to avoid a false positive
@@ -294,7 +294,7 @@ def _get_config(self) -> Configuration:
294294
"variables, which were not available.")
295295
return self._config
296296

297-
def _set_config(self, config: Optional[Configuration]):
297+
def _set_config(self, config: Configuration | None):
298298
self._config = config
299299

300300
config = property(_get_config, _set_config)
@@ -602,7 +602,7 @@ class WestExtCommandSpec:
602602
factory: _ExtFactory
603603

604604
def extension_commands(config: Configuration,
605-
manifest: Optional[Manifest] = None):
605+
manifest: Manifest | None = None):
606606
# Get descriptions of available extension commands.
607607
#
608608
# The return value is an ordered map from project paths to lists of

src/west/configuration.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from collections.abc import Iterable
4343
from enum import Enum
4444
from pathlib import Path, PureWindowsPath
45-
from typing import TYPE_CHECKING, Any, Optional, Union
45+
from typing import TYPE_CHECKING, Any
4646

4747
from west.util import WEST_DIR, PathType, WestNotFound, west_dir
4848

@@ -67,7 +67,7 @@ def parse_key(dotted_name: str):
6767
return section_child
6868

6969
@staticmethod
70-
def from_path(path: Optional[Path]) -> Optional['_InternalCF']:
70+
def from_path(path: Path | None) -> '_InternalCF | None':
7171
return _InternalCF(path) if path and path.exists() else None
7272

7373
def __init__(self, path: Path):
@@ -160,7 +160,7 @@ class Configuration:
160160
161161
'''
162162

163-
def __init__(self, topdir: Optional[PathType] = None):
163+
def __init__(self, topdir: PathType | None = None):
164164
'''Load the system, global, and workspace configurations and
165165
make them available for the user.
166166
@@ -179,8 +179,8 @@ def __init__(self, topdir: Optional[PathType] = None):
179179
self._local = _InternalCF.from_path(self._local_path)
180180

181181
def get(self, option: str,
182-
default: Optional[str] = None,
183-
configfile: ConfigFile = ConfigFile.ALL) -> Optional[str]:
182+
default: str | None = None,
183+
configfile: ConfigFile = ConfigFile.ALL) -> str | None:
184184
'''Get a configuration option's value as a string.
185185
186186
:param option: option to get, in 'foo.bar' form
@@ -204,8 +204,8 @@ def getboolean(self, option: str,
204204
return self._get(lambda cf: cf.getboolean(option), default, configfile)
205205

206206
def getint(self, option: str,
207-
default: Optional[int] = None,
208-
configfile: ConfigFile = ConfigFile.ALL) -> Optional[int]:
207+
default: int | None = None,
208+
configfile: ConfigFile = ConfigFile.ALL) -> int | None:
209209
'''Get a configuration option's value as an int.
210210
211211
:param option: option to get, in 'foo.bar' form
@@ -215,8 +215,8 @@ def getint(self, option: str,
215215
return self._get(lambda cf: cf.getint(option), default, configfile)
216216

217217
def getfloat(self, option: str,
218-
default: Optional[float] = None,
219-
configfile: ConfigFile = ConfigFile.ALL) -> Optional[float]:
218+
default: float | None = None,
219+
configfile: ConfigFile = ConfigFile.ALL) -> float | None:
220220
'''Get a configuration option's value as a float.
221221
222222
:param option: option to get, in 'foo.bar' form
@@ -306,7 +306,7 @@ def _create(path: Path) -> _InternalCF:
306306
return ret
307307

308308
def delete(self, option: str,
309-
configfile: Optional[ConfigFile] = None) -> None:
309+
configfile: ConfigFile | None = None) -> None:
310310
'''Delete an option from the given file or files.
311311
312312
If *option* is not set in the given *configfile*, KeyError is raised.
@@ -401,7 +401,7 @@ def _local_as_dict(self):
401401
return self._cf_to_dict(self._local)
402402

403403
@staticmethod
404-
def _cf_to_dict(cf: Optional[_InternalCF]) -> dict[str, Any]:
404+
def _cf_to_dict(cf: _InternalCF | None) -> dict[str, Any]:
405405
ret: dict[str, Any] = {}
406406
if cf is None:
407407
return ret
@@ -423,9 +423,9 @@ def _deprecated(old_function):
423423
'use a west.configuration.Configuration object',
424424
DeprecationWarning, stacklevel=2)
425425

426-
def read_config(configfile: Optional[ConfigFile] = None,
426+
def read_config(configfile: ConfigFile | None = None,
427427
config: configparser.ConfigParser = config,
428-
topdir: Optional[PathType] = None) -> None:
428+
topdir: PathType | None = None) -> None:
429429
'''Read configuration files into *config*.
430430
431431
Reads the files given by *configfile*, storing the values into the
@@ -458,7 +458,7 @@ def read_config(configfile: Optional[ConfigFile] = None,
458458

459459
def update_config(section: str, key: str, value: Any,
460460
configfile: ConfigFile = ConfigFile.LOCAL,
461-
topdir: Optional[PathType] = None) -> None:
461+
topdir: PathType | None = None) -> None:
462462
'''Sets ``section.key`` to *value* in the given configuration file.
463463
464464
:param section: config section; will be created if it does not exist
@@ -493,8 +493,8 @@ def update_config(section: str, key: str, value: Any,
493493
config.write(f)
494494

495495
def delete_config(section: str, key: str,
496-
configfile: Union[Optional[ConfigFile], list[ConfigFile]] = None,
497-
topdir: Optional[PathType] = None) -> None:
496+
configfile: ConfigFile | list[ConfigFile] | None = None,
497+
topdir: PathType | None = None) -> None:
498498
'''Delete the option section.key from the given file or files.
499499
500500
:param section: section whose key to delete
@@ -556,7 +556,7 @@ def delete_config(section: str, key: str,
556556
if not found:
557557
raise KeyError(f'{section}.{key}')
558558

559-
def _location(cfg: ConfigFile, topdir: Optional[PathType] = None,
559+
def _location(cfg: ConfigFile, topdir: PathType | None = None,
560560
find_local: bool = True) -> str:
561561
# Making this a function that gets called each time you ask for a
562562
# configuration file makes it respect updated environment
@@ -628,7 +628,7 @@ def _location(cfg: ConfigFile, topdir: Optional[PathType] = None,
628628
else:
629629
raise ValueError(f'invalid configuration file {cfg}')
630630

631-
def _gather_configs(cfg: ConfigFile, topdir: Optional[PathType]) -> list[str]:
631+
def _gather_configs(cfg: ConfigFile, topdir: PathType | None) -> list[str]:
632632
# Find the paths to the given configuration files, in increasing
633633
# precedence order.
634634
ret = []
@@ -645,7 +645,7 @@ def _gather_configs(cfg: ConfigFile, topdir: Optional[PathType]) -> list[str]:
645645

646646
return ret
647647

648-
def _ensure_config(configfile: ConfigFile, topdir: Optional[PathType]) -> str:
648+
def _ensure_config(configfile: ConfigFile, topdir: PathType | None) -> str:
649649
# Ensure the given configfile exists, returning its path. May
650650
# raise permissions errors, WestNotFound, etc.
651651
loc = _location(configfile, topdir=topdir)

0 commit comments

Comments
 (0)