From 0f5bd4952fa4f556d5d1f5c3c2df9860336663cb Mon Sep 17 00:00:00 2001 From: Thomas Feldmann Date: Thu, 4 Jan 2024 16:36:22 +0100 Subject: [PATCH] add some type hints --- organize/cli.py | 3 ++- organize/config.py | 6 +++--- organize/output/output.py | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/organize/cli.py b/organize/cli.py index a8fdec25..15bc111f 100644 --- a/organize/cli.py +++ b/organize/cli.py @@ -52,7 +52,7 @@ from organize import Config, ConfigError from organize.find_config import ConfigNotFound, find_config, list_configs -from organize.output import JSONL, Default +from organize.output import JSONL, Default, Output from .__version__ import __is_prerelease__, __version__ @@ -93,6 +93,7 @@ def execute( simulate: bool, ) -> None: output = JSONL() if format == "jsonl" else Default() + assert isinstance(output, Output) config_path = find_config(name_or_path=config) Config.from_path(config_path).execute( simulate=simulate, diff --git a/organize/config.py b/organize/config.py index c493a042..4e02c1c2 100644 --- a/organize/config.py +++ b/organize/config.py @@ -59,7 +59,7 @@ class Config: _config_path: Optional[Path] = None @classmethod - def from_string(cls, config: str, config_path: Optional[Path] = None): + def from_string(cls, config: str, config_path: Optional[Path] = None) -> "Config": dedented = textwrap.dedent(config) as_dict = yaml.load(dedented, Loader=yaml.SafeLoader) try: @@ -71,7 +71,7 @@ def from_string(cls, config: str, config_path: Optional[Path] = None): raise ConfigError(e=e, config_path=config_path) from e @classmethod - def from_path(cls, config_path: Path): + def from_path(cls, config_path: Path) -> "Config": text = config_path.read_text(encoding="utf-8") inst = cls.from_string(text, config_path=config_path) return inst @@ -83,7 +83,7 @@ def execute( tags: Tags = set(), skip_tags: Tags = set(), working_dir: Union[str, Path] = ".", - ): + ) -> None: working_path = Path(render(str(working_dir))) os.chdir(working_path) output.start( diff --git a/organize/output/output.py b/organize/output/output.py index ef80dbdf..86bde517 100644 --- a/organize/output/output.py +++ b/organize/output/output.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Literal, Optional, Protocol +from typing import TYPE_CHECKING, Literal, Optional, Protocol, runtime_checkable if TYPE_CHECKING: from pathlib import Path @@ -13,6 +13,7 @@ Level = Literal["info", "warn", "error"] +@runtime_checkable class Output(Protocol): """ The protocol all of organize's outputs must adhere to. @@ -44,5 +45,5 @@ def confirm( ) -> bool: ... - def end(self, success_count: int, error_count: int): + def end(self, success_count: int, error_count: int) -> None: ...