Skip to content

Commit

Permalink
Modernize types with All checks passed!
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Sep 24, 2024
1 parent 3c9fd8b commit fea1336
Show file tree
Hide file tree
Showing 30 changed files with 379 additions and 323 deletions.
34 changes: 18 additions & 16 deletions poethepoet/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import sys
from pathlib import Path
Expand Down Expand Up @@ -54,18 +56,18 @@ class PoeThePoet:
"""

cwd: Path
ui: "PoeUi"
config: "PoeConfig"
ui: PoeUi
config: PoeConfig

_task_specs: Optional[Dict[str, "PoeTask.TaskSpec"]] = None
_task_specs: dict[str, PoeTask.TaskSpec] | None = None

def __init__(
self,
cwd: Optional[Union[Path, str]] = None,
config: Optional[Union[Mapping[str, Any], "PoeConfig"]] = None,
cwd: Path | str | None = None,
config: Mapping[str, Any] | PoeConfig | None = None,
output: IO = sys.stdout,
poetry_env_path: Optional[str] = None,
config_name: Optional[str] = None,
poetry_env_path: str | None = None,
config_name: str | None = None,
program_name: str = "poe",
):
from .config import PoeConfig
Expand Down Expand Up @@ -134,7 +136,7 @@ def task_specs(self):
self._task_specs = TaskSpecFactory(self.config)
return self._task_specs

def resolve_task(self, allow_hidden: bool = False) -> Optional["PoeTask"]:
def resolve_task(self, allow_hidden: bool = False) -> PoeTask | None:
from .task.base import TaskContext

task = tuple(self.ui["task"])
Expand Down Expand Up @@ -167,8 +169,8 @@ def resolve_task(self, allow_hidden: bool = False) -> Optional["PoeTask"]:
)

def run_task(
self, task: "PoeTask", context: Optional["RunContext"] = None
) -> Optional[int]:
self, task: PoeTask, context: RunContext | None = None
) -> int | None:
if context is None:
context = self.get_run_context()
try:
Expand All @@ -180,7 +182,7 @@ def run_task(
self.print_help(error=error)
return 1

def run_task_graph(self, task: "PoeTask") -> Optional[int]:
def run_task_graph(self, task: PoeTask) -> int | None:
from .task.graph import TaskExecutionGraph

context = self.get_run_context(multistage=True)
Expand All @@ -207,7 +209,7 @@ def run_task_graph(self, task: "PoeTask") -> Optional[int]:
return 1
return 0

def get_run_context(self, multistage: bool = False) -> "RunContext":
def get_run_context(self, multistage: bool = False) -> RunContext:
from .context import RunContext

result = RunContext(
Expand All @@ -226,16 +228,16 @@ def get_run_context(self, multistage: bool = False) -> "RunContext":

def print_help(
self,
info: Optional[str] = None,
error: Optional[Union[str, PoeException]] = None,
info: str | None = None,
error: str | PoeException | None = None,
):
from .task.args import PoeTaskArgs

if isinstance(error, str):
error = PoeException(error)

tasks_help: Dict[
str, Tuple[str, Sequence[Tuple[Tuple[str, ...], str, str]]]
tasks_help: dict[
str, tuple[str, Sequence[tuple[tuple[str, ...], str, str]]]
] = {
task_name: (
(
Expand Down
4 changes: 3 additions & 1 deletion poethepoet/completion/zsh.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any, Iterable, Set


Expand Down Expand Up @@ -50,7 +52,7 @@ def format_exclusions(excl_option_strings):
tuple(),
)
# collect all option strings that are exclusive with this one
excl_option_strings: Set[str] = {
excl_option_strings: set[str] = {
option_string
for excl_option in excl_options
for option_string in excl_option.option_strings
Expand Down
40 changes: 21 additions & 19 deletions poethepoet/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Tuple, Union
Expand All @@ -10,25 +12,25 @@


class RunContext:
config: "PoeConfig"
ui: "PoeUi"
env: "EnvVarsManager"
config: PoeConfig
ui: PoeUi
env: EnvVarsManager
dry: bool
poe_active: Optional[str]
poe_active: str | None
project_dir: Path
multistage: bool = False
exec_cache: Dict[str, Any]
captured_stdout: Dict[Tuple[str, ...], str]
exec_cache: dict[str, Any]
captured_stdout: dict[tuple[str, ...], str]

def __init__(
self,
config: "PoeConfig",
ui: "PoeUi",
config: PoeConfig,
ui: PoeUi,
env: Mapping[str, str],
dry: bool,
poe_active: Optional[str],
poe_active: str | None,
multistage: bool = False,
cwd: Optional[Union[Path, str]] = None,
cwd: Path | str | None = None,
):
from .env.manager import EnvVarsManager

Expand All @@ -52,8 +54,8 @@ def __init__(
)

def _get_dep_values(
self, used_task_invocations: Mapping[str, Tuple[str, ...]]
) -> Dict[str, str]:
self, used_task_invocations: Mapping[str, tuple[str, ...]]
) -> dict[str, str]:
"""
Get env vars from upstream tasks declared via the uses option.
"""
Expand All @@ -62,7 +64,7 @@ def _get_dep_values(
for var_name, invocation in used_task_invocations.items()
}

def save_task_output(self, invocation: Tuple[str, ...], captured_stdout: bytes):
def save_task_output(self, invocation: tuple[str, ...], captured_stdout: bytes):
"""
Store the stdout data from a task so that it can be reused by other tasks
"""
Expand All @@ -76,7 +78,7 @@ def save_task_output(self, invocation: Tuple[str, ...], captured_stdout: bytes):
else:
raise

def get_task_output(self, invocation: Tuple[str, ...]):
def get_task_output(self, invocation: tuple[str, ...]):
"""
Get the stored stdout data from a task so that it can be reused by other tasks
Expand All @@ -87,12 +89,12 @@ def get_task_output(self, invocation: Tuple[str, ...]):

def get_executor(
self,
invocation: Tuple[str, ...],
env: "EnvVarsManager",
invocation: tuple[str, ...],
env: EnvVarsManager,
working_dir: Path,
executor_config: Optional[Mapping[str, str]] = None,
capture_stdout: Union[str, bool] = False,
) -> "PoeExecutor":
executor_config: Mapping[str, str] | None = None,
capture_stdout: str | bool = False,
) -> PoeExecutor:
from .executor import PoeExecutor

if not executor_config:
Expand Down
10 changes: 6 additions & 4 deletions poethepoet/env/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from os import environ
from pathlib import Path
from typing import TYPE_CHECKING, Dict, Optional, Union
Expand All @@ -11,15 +13,15 @@


class EnvFileCache:
_cache: Dict[str, Dict[str, str]] = {}
_ui: Optional["PoeUi"]
_cache: dict[str, dict[str, str]] = {}
_ui: PoeUi | None
_project_dir: Path

def __init__(self, project_dir: Path, ui: Optional["PoeUi"]):
def __init__(self, project_dir: Path, ui: PoeUi | None):
self._project_dir = project_dir
self._ui = ui

def get(self, envfile: Union[str, Path]) -> Dict[str, str]:
def get(self, envfile: str | Path) -> dict[str, str]:
from .parse import parse_env_file

envfile_path_str = str(envfile)
Expand Down
28 changes: 15 additions & 13 deletions poethepoet/env/manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Mapping, Optional, Union
Expand All @@ -11,18 +13,18 @@


class EnvVarsManager(Mapping):
_config: "PoeConfig"
_ui: Optional["PoeUi"]
_vars: Dict[str, str]
envfiles: "EnvFileCache"
_config: PoeConfig
_ui: PoeUi | None
_vars: dict[str, str]
envfiles: EnvFileCache

def __init__( # TODO: check if we still need all these args!
self,
config: "PoeConfig",
ui: Optional["PoeUi"],
parent_env: Optional["EnvVarsManager"] = None,
base_env: Optional[Mapping[str, str]] = None,
cwd: Optional[Union[Path, str]] = None,
config: PoeConfig,
ui: PoeUi | None,
parent_env: EnvVarsManager | None = None,
base_env: Mapping[str, str] | None = None,
cwd: Path | str | None = None,
):
from ..helpers.git import GitRepo
from .cache import EnvFileCache
Expand Down Expand Up @@ -58,7 +60,7 @@ def __iter__(self):
def __len__(self):
return len(self._vars)

def get(self, key: Any, /, default: Any = None) -> Optional[str]:
def get(self, key: Any, /, default: Any = None) -> str | None:
if key == "POE_GIT_DIR":
# This is a special case environment variable that is only set if requested
self._vars["POE_GIT_DIR"] = str(self._git_repo.path or "")
Expand All @@ -74,8 +76,8 @@ def set(self, key: str, value: str):

def apply_env_config(
self,
envfile: Optional[Union[str, List[str]]],
config_env: Optional[Mapping[str, Union[str, Mapping[str, str]]]],
envfile: str | list[str] | None,
config_env: Mapping[str, str | Mapping[str, str]] | None,
config_dir: Path,
config_working_dir: Path,
):
Expand Down Expand Up @@ -120,7 +122,7 @@ def apply_env_config(

def update(self, env_vars: Mapping[str, Any]):
# ensure all values are strings
str_vars: Dict[str, str] = {}
str_vars: dict[str, str] = {}
for key, value in env_vars.items():
if isinstance(value, list):
str_vars[key] = " ".join(str(item) for item in value)
Expand Down
4 changes: 3 additions & 1 deletion poethepoet/env/parse.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from enum import Enum
from typing import Iterable, Optional, Sequence
Expand Down Expand Up @@ -63,7 +65,7 @@ def parse_env_file(content_lines: Sequence[str]):
result = {}
cursor = 0
state = ParserState.SCAN_VAR_NAME
var_name: Optional[str] = ""
var_name: str | None = ""
var_content = []

while cursor < len(content):
Expand Down
26 changes: 14 additions & 12 deletions poethepoet/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

from typing import Optional


# ruff: noqa: N818
class PoeException(RuntimeError):
cause: Optional[str]
cause: str | None

def __init__(self, msg, *args):
self.msg = msg
Expand All @@ -20,21 +22,21 @@ class ExpressionParseError(PoeException):


class ConfigValidationError(PoeException):
context: Optional[str]
task_name: Optional[str]
index: Optional[int]
global_option: Optional[str]
filename: Optional[str]
context: str | None
task_name: str | None
index: int | None
global_option: str | None
filename: str | None

def __init__(
self,
msg,
*args,
context: Optional[str] = None,
task_name: Optional[str] = None,
index: Optional[int] = None,
global_option: Optional[str] = None,
filename: Optional[str] = None
context: str | None = None,
task_name: str | None = None,
index: int | None = None,
global_option: str | None = None,
filename: str | None = None
):
super().__init__(msg, *args)
self.context = context
Expand All @@ -45,7 +47,7 @@ def __init__(


class ExecutionError(RuntimeError):
cause: Optional[str]
cause: str | None

def __init__(self, msg, *args):
self.msg = msg
Expand Down
Loading

0 comments on commit fea1336

Please sign in to comment.