Skip to content

Commit

Permalink
Add return type hints for public functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joelspadin committed Sep 14, 2024
1 parent 4f025af commit 93651b0
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 69 deletions.
14 changes: 7 additions & 7 deletions zmk/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any, TypeVar, cast, overload
from typing import Any, Self, TypeVar, cast, overload

import dacite

Expand All @@ -25,7 +25,7 @@ class BuildItem:
cmake_args: str | None = None
artifact_name: str | None = None

def __rich__(self):
def __rich__(self) -> str:
parts = []
parts.append(self.board)

Expand Down Expand Up @@ -57,11 +57,11 @@ class BuildMatrix:
_data: dict[str, Any] | None

@classmethod
def from_repo(cls, repo: Repo):
def from_repo(cls, repo: Repo) -> Self:
"""Get the build matrix for a repo"""
return cls(repo.build_matrix_path)

def __init__(self, path: Path) -> None:
def __init__(self, path: Path):
self._path = path
self._yaml = YAML(typ="rt")
self._yaml.indent(mapping=2, sequence=4, offset=2)
Expand All @@ -70,12 +70,12 @@ def __init__(self, path: Path) -> None:
except FileNotFoundError:
self._data = None

def write(self):
def write(self) -> None:
"""Updated the YAML file, creating it if necessary"""
self._yaml.dump(self._data, self._path)

@property
def path(self):
def path(self) -> Path:
"""Path to the matrix's YAML file"""
return self._path

Expand All @@ -89,7 +89,7 @@ def include(self) -> list[BuildItem]:
wrapper = dacite.from_dict(_BuildMatrixWrapper, normalized)
return wrapper.include

def has_item(self, item: BuildItem):
def has_item(self, item: BuildItem) -> bool:
"""Get whether the matrix has a build item"""
return item in self.include

Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import cd, code, config, download, init, keyboard, module, west


def register(app: typer.Typer):
def register(app: typer.Typer) -> None:
"""Register all commands with the app"""
app.command()(cd.cd)
app.command()(code.code)
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..exceptions import FatalError, FatalHomeMissing, FatalHomeNotSet


def cd(ctx: typer.Context):
def cd(ctx: typer.Context) -> None:
"""Go to the ZMK config repo."""
if not sys.stdout.isatty():
raise FatalError(
Expand Down
4 changes: 2 additions & 2 deletions zmk/commands/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def code(
"--build", "-b", help="Open the build matrix instead of a keymap."
),
] = False,
):
) -> None:
"""Open the repo or a .keymap or .conf file in a text editor."""

cfg = get_config(ctx)
Expand Down Expand Up @@ -110,7 +110,7 @@ class Editor:
def __rich__(self):
return self.name

def get_command(self):
def get_command(self) -> str | None:
"""Get the command to execute the tool, or None if it is not installed"""
if self.test and self.test():
return self.cmd
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def config(
callback=_path_callback,
),
] = False,
):
) -> None:
"""Get and set ZMK CLI settings."""

cfg = get_config(ctx)
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..repo import Repo


def download(ctx: typer.Context):
def download(ctx: typer.Context) -> None:
"""Open the web page to download firmware from GitHub."""

cfg = get_config(ctx)
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
TEXT_WIDTH = 80


def init(ctx: typer.Context):
def init(ctx: typer.Context) -> None:
"""Create a new ZMK config repo or clone an existing one."""

console = rich.get_console()
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@


@app.callback()
def keyboard():
def keyboard() -> None:
"""Add or remove keyboards from the build."""
2 changes: 1 addition & 1 deletion zmk/commands/keyboard/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def keyboard_add(
help="ID of the keyboard board/shield to add.",
),
] = None,
):
) -> None:
"""Add configuration for a keyboard and add it to the build."""

console = rich.get_console()
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/keyboard/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def keyboard_list(
"--standalone", help="List only keyboards with onboard controllers."
),
] = False,
):
) -> None:
"""List supported keyboards or keyboards in the build matrix."""

console = rich.get_console()
Expand Down
16 changes: 9 additions & 7 deletions zmk/commands/keyboard/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def keyboard_new(
force: Annotated[
bool, typer.Option("--force", "-f", help="Overwrite existing files.")
] = False,
):
) -> None:
"""Create a new keyboard from a template."""
cfg = get_config(ctx)
repo = cfg.get_repo()
Expand Down Expand Up @@ -257,23 +257,23 @@ class NamePrompt(NamePromptBase):
"""Prompt for a keyboard name."""

@classmethod
def validate(cls, value: str):
def validate(cls, value: str) -> None:
_validate_name(value)

@classmethod
def ask(cls): # pyright: ignore[reportIncompatibleMethodOverride]
def ask(cls) -> str: # pyright: ignore[reportIncompatibleMethodOverride]
return super().ask("Enter the name of the keyboard")


class ShortNamePrompt(NamePromptBase):
"""Prompt for an abbreviated keyboard name."""

@classmethod
def validate(cls, value: str):
def validate(cls, value: str) -> None:
_validate_short_name(value)

@classmethod
def ask(cls): # pyright: ignore[reportIncompatibleMethodOverride]
def ask(cls) -> str: # pyright: ignore[reportIncompatibleMethodOverride]
return super().ask(
f"Enter an abbreviated name [dim](<= {MAX_NAME_LENGTH} chars)"
)
Expand All @@ -283,11 +283,13 @@ class IdPrompt(NamePromptBase):
"""Prompt for a keyboard identifier."""

@classmethod
def validate(cls, value: str):
def validate(cls, value: str) -> None:
_validate_id(value)

@classmethod
def ask(cls, prompt: str): # pyright: ignore[reportIncompatibleMethodOverride]
def ask( # pyright: ignore[reportIncompatibleMethodOverride]
cls, prompt: str
) -> str:
result = super().ask(
"Enter an ID for the keyboard", default=_get_default_id(prompt)
)
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/keyboard/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


# TODO: add options to select items from command line
def keyboard_remove(ctx: typer.Context):
def keyboard_remove(ctx: typer.Context) -> None:
"""Remove a keyboard from the build."""
cfg = get_config(ctx)
repo = cfg.get_repo()
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@app.callback()
def keyboard():
def keyboard() -> None:
"""Add or remove Zephyr modules from the build."""
2 changes: 1 addition & 1 deletion zmk/commands/module/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def module_add(
str | None,
typer.Option("--name", "-n", help="Name of the module.", show_default=False),
] = None,
):
) -> None:
"""Add a Zephyr module to the build."""
cfg = get_config(ctx)
repo = cfg.get_repo()
Expand Down
4 changes: 1 addition & 3 deletions zmk/commands/module/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from ...config import get_config


def module_list(
ctx: typer.Context,
):
def module_list(ctx: typer.Context) -> None:
"""Print a list of installed Zephyr modules."""

console = rich.get_console()
Expand Down
2 changes: 1 addition & 1 deletion zmk/commands/module/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def module_remove(
str | None,
typer.Argument(help="Name or URL of the module to remove.", show_default=False),
] = None,
):
) -> None:
"""Remove a Zephyr module from the build."""
cfg = get_config(ctx)
repo = cfg.get_repo()
Expand Down
4 changes: 2 additions & 2 deletions zmk/commands/west.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..config import get_config


def west(ctx: typer.Context):
def west(ctx: typer.Context) -> None:
# pylint: disable=line-too-long
"""
Run [link=https://docs.zephyrproject.org/latest/develop/west/index.html]west[/link] in the config repo.
Expand All @@ -34,7 +34,7 @@ def update(
help="Names of modules to update. Updates all modules if omitted."
),
] = None,
):
) -> None:
"""Fetch the latest keyboard data."""

cfg = get_config(ctx)
Expand Down
13 changes: 7 additions & 6 deletions zmk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from collections import defaultdict
from collections.abc import Generator
from configparser import ConfigParser
from itertools import chain
from pathlib import Path
Expand All @@ -29,15 +30,15 @@ class Config:
path: Path
force_home: bool

def __init__(self, path: Path | None, force_home=False) -> None:
def __init__(self, path: Path | None, force_home=False):
self.path = path or _default_config_path()
self.force_home = force_home

self._overrides: defaultdict[str, dict[str, str]] = defaultdict(dict)
self._parser = ConfigParser()
self._parser.read(self.path, encoding="utf-8")

def write(self):
def write(self) -> None:
"""Write back to the same file used when calling read()"""
self.path.parent.mkdir(parents=True, exist_ok=True)

Expand All @@ -54,17 +55,17 @@ def getboolean(self, name: str, **kwargs) -> bool:
section, option = self._split_option(name)
return self._parser.getboolean(section, option, **kwargs)

def set(self, name: str, value: str):
def set(self, name: str, value: str) -> None:
"""Set a setting"""
section, option = self._split_option(name)
self._parser.set(section, option, value)

def remove(self, name: str):
def remove(self, name: str) -> None:
"""Remove a setting"""
section, option = self._split_option(name)
self._parser.remove_option(section, option)

def items(self):
def items(self) -> Generator[tuple[str, str], None, None]:
"""Yields ('section.option', 'value') tuples for all settings"""
sections = set(chain(self._overrides.keys(), self._parser.sections()))

Expand Down Expand Up @@ -93,7 +94,7 @@ def home_path(self) -> Path | None:
return Path(home) if home else None

@home_path.setter
def home_path(self, value: Path):
def home_path(self, value: Path) -> None:
self.set(Settings.USER_HOME, str(value.resolve()))

def get_repo(self) -> Repo:
Expand Down
2 changes: 1 addition & 1 deletion zmk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FatalError(ClickException):

highlighter: Highlighter

def __init__(self, message: str, highlighter: Highlighter | None = None) -> None:
def __init__(self, message: str, highlighter: Highlighter | None = None):
self.highlighter = highlighter or ReprHighlighter()
super().__init__(message)

Expand Down
18 changes: 10 additions & 8 deletions zmk/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass, field
from functools import reduce
from pathlib import Path
from typing import Any, Literal, TypeAlias, TypeGuard
from typing import Any, Literal, Self, TypeAlias, TypeGuard

import dacite

Expand Down Expand Up @@ -47,7 +47,7 @@ def __rich__(self) -> Any:
return f"{self.id} [dim]{self.name}"

@classmethod
def from_dict(cls, data):
def from_dict(cls, data) -> Self:
"""Read a hardware description from a dict"""
return dacite.from_dict(cls, data)

Expand Down Expand Up @@ -79,12 +79,12 @@ def __post_init__(self):
self.variants = self.variants or []

@property
def config_path(self):
def config_path(self) -> Path:
"""Path to the .conf file for this keyboard"""
return self.directory / f"{self.id}.conf"

@property
def keymap_path(self):
def keymap_path(self) -> Path:
"""Path to the .keymap file for this keyboard"""
return self.directory / f"{self.id}.keymap"

Expand Down Expand Up @@ -127,17 +127,17 @@ class GroupedHardware:

# TODO: add displays and other peripherals?

def find_keyboard(self, item_id: str):
def find_keyboard(self, item_id: str) -> Keyboard | None:
"""Find a keyboard by ID"""
item_id = item_id.casefold()
return next((i for i in self.keyboards if i.id.casefold() == item_id), None)

def find_controller(self, item_id: str):
def find_controller(self, item_id: str) -> Board | None:
"""Find a controller by ID"""
item_id = item_id.casefold()
return next((i for i in self.controllers if i.id.casefold() == item_id), None)

def find_interconnect(self, item_id: str):
def find_interconnect(self, item_id: str) -> Interconnect | None:
"""Find an interconnect by ID"""
item_id = item_id.casefold()
return next(
Expand Down Expand Up @@ -166,7 +166,9 @@ def is_interconnect(hardware: Hardware) -> TypeGuard[Interconnect]:
return isinstance(hardware, Interconnect)


def is_compatible(base: Board | Shield | Iterable[Board | Shield], shield: Shield):
def is_compatible(
base: Board | Shield | Iterable[Board | Shield], shield: Shield
) -> bool:
"""
Get whether a shield can be attached to the given hardware.
Expand Down
2 changes: 1 addition & 1 deletion zmk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def main(
is_eager=True,
),
] = False,
):
) -> None:
"""
ZMK Firmware command line tool
Expand Down
Loading

0 comments on commit 93651b0

Please sign in to comment.