Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project: Fix and check pyupgrade issues #763

Merged
merged 11 commits into from
Nov 4, 2024
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ include-package-data = true
[tool.setuptools.packages.find]
where = ["src"]
namespaces = false

[tool.ruff.lint]
extend-select = [
"UP", # pyupgrade
]
ignore = [
"UP027", # deprecated pyupgrade rule
]
9 changes: 4 additions & 5 deletions src/west/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import textwrap
import traceback
from typing import NamedTuple, Optional
from typing import List as ListType

from west import log
import west.configuration
Expand Down Expand Up @@ -65,9 +64,9 @@ class EarlyArgs(NamedTuple):
command_name: Optional[str]

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

def parse_early_args(argv: ListType[str]) -> EarlyArgs:
def parse_early_args(argv: list[str]) -> EarlyArgs:
# Hand-rolled argument parser for early arguments.

help = False
Expand Down Expand Up @@ -922,7 +921,7 @@ def __init__(self, *args, **kwargs):
# come first as our override of that method relies on it.
self.west_optionals = []
self.west_app = kwargs.pop('west_app', None)
super(WestArgumentParser, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def print_help(self, file=None, top_level=False):
print(self.format_help(top_level=top_level), end='',
Expand All @@ -938,7 +937,7 @@ def format_help(self, top_level=False):
# one of the subcommand parsers, and we delegate to super.

if not top_level:
return super(WestArgumentParser, self).format_help()
return super().format_help()

# Format the help to be at most 75 columns wide, the maximum
# generally recommended by typographers for readability.
Expand Down
2 changes: 1 addition & 1 deletion src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class ManifestCommand(_ProjectCommand):
# west.manifest.Manifest.

def __init__(self):
super(ManifestCommand, self).__init__(
super().__init__(
'manifest',
'manage the west manifest',
textwrap.dedent('''\
Expand Down
14 changes: 7 additions & 7 deletions src/west/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import subprocess
import sys
from types import ModuleType
from typing import Callable, Dict, List, NoReturn, Optional
from typing import Callable, NoReturn, Optional

import colorama
import pykwalify
Expand All @@ -41,7 +41,7 @@

# Cache which maps files implementing extension commands to their
# imported modules.
_EXT_MODULES_CACHE: Dict[str, ModuleType] = {}
_EXT_MODULES_CACHE: dict[str, ModuleType] = {}
# Infinite iterator of "fresh" extension command module names.
_EXT_MODULES_NAME_IT = (f'west.commands.ext.cmd_{i}'
for i in itertools.count(1))
Expand All @@ -62,7 +62,7 @@ class ExtensionCommandError(CommandError):

def __init__(self, **kwargs):
self.hint = kwargs.pop('hint', None)
super(ExtensionCommandError, self).__init__(**kwargs)
super().__init__(**kwargs)

def _no_topdir_msg(cwd, name):
return f'''\
Expand Down Expand Up @@ -149,7 +149,7 @@ def __init__(self, name: str, help: str, description: str,
self.topdir: Optional[str] = None
self.manifest = None
self.config = None
self._hooks: List[Callable[['WestCommand'], None]] = []
self._hooks: list[Callable[[WestCommand], None]] = []

def add_pre_run_hook(self,
hook: Callable[['WestCommand'], None]) -> None:
Expand All @@ -162,7 +162,7 @@ def add_pre_run_hook(self,
'''
self._hooks.append(hook)

def run(self, args: argparse.Namespace, unknown: List[str],
def run(self, args: argparse.Namespace, unknown: list[str],
topdir: PathType,
manifest: Optional[Manifest] = None,
config: Optional[Configuration] = None) -> None:
Expand Down Expand Up @@ -229,7 +229,7 @@ def do_add_parser(self, parser_adder) -> argparse.ArgumentParser:
'''

@abstractmethod
def do_run(self, args: argparse.Namespace, unknown: List[str]):
def do_run(self, args: argparse.Namespace, unknown: list[str]):
'''Subclasses must implement; called to run the command.

:param args: ``argparse.Namespace`` of parsed arguments
Expand Down Expand Up @@ -647,7 +647,7 @@ def _ext_specs(project):
continue

# Load the spec file and check the schema.
with open(spec_file, 'r') as f:
with open(spec_file) as f:
try:
commands_spec = yaml.safe_load(f.read())
except yaml.YAMLError as e:
Expand Down
13 changes: 7 additions & 6 deletions src/west/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
from pathlib import PureWindowsPath, Path
import platform
from enum import Enum
from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING, Union
from typing import Any, Optional, TYPE_CHECKING, Union
from collections.abc import Iterable
import warnings

from west.util import WEST_DIR, west_dir, WestNotFound, PathType
Expand Down Expand Up @@ -366,7 +367,7 @@ def load(cf: _InternalCF):
load(self._local)

def items(self, configfile: ConfigFile = ConfigFile.ALL
) -> Iterable[Tuple[str, Any]]:
) -> Iterable[tuple[str, Any]]:
'''Iterator of option, value pairs.'''
if configfile == ConfigFile.ALL:
ret = {}
Expand Down Expand Up @@ -399,8 +400,8 @@ def _local_as_dict(self):
return self._cf_to_dict(self._local)

@staticmethod
def _cf_to_dict(cf: Optional[_InternalCF]) -> Dict[str, Any]:
ret: Dict[str, Any] = {}
def _cf_to_dict(cf: Optional[_InternalCF]) -> dict[str, Any]:
ret: dict[str, Any] = {}
if cf is None:
return ret
for section, contents in cf.cp.items():
Expand Down Expand Up @@ -491,7 +492,7 @@ def update_config(section: str, key: str, value: Any,
config.write(f)

def delete_config(section: str, key: str,
configfile: Union[Optional[ConfigFile], List[ConfigFile]] = None,
configfile: Union[Optional[ConfigFile], list[ConfigFile]] = None,
topdir: Optional[PathType] = None) -> None:
'''Delete the option section.key from the given file or files.

Expand Down Expand Up @@ -626,7 +627,7 @@ def _location(cfg: ConfigFile, topdir: Optional[PathType] = None,
else:
raise ValueError(f'invalid configuration file {cfg}')

def _gather_configs(cfg: ConfigFile, topdir: Optional[PathType]) -> List[str]:
def _gather_configs(cfg: ConfigFile, topdir: Optional[PathType]) -> list[str]:
# Find the paths to the given configuration files, in increasing
# precedence order.
ret = []
Expand Down
Loading