Skip to content

Commit

Permalink
Upgrade ruff and fix resulting issues (#260)
Browse files Browse the repository at this point in the history
Make options annotation parsing work with common ForwardRefs
  • Loading branch information
nat-n committed Nov 24, 2024
1 parent 0d5025e commit dde728d
Show file tree
Hide file tree
Showing 47 changed files with 283 additions and 393 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
name: CI

on: [push, pull_request]
on:
pull_request:
branches:
- main
- development
push:
branches:
- main
- development

jobs:

Expand Down
12 changes: 6 additions & 6 deletions docs/global_options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ Global options
The following options can be set for all tasks in a project directly under ``[tool.poe]``. These are called global options, in contrast with :doc:`task options<tasks/options>`.


**env** : ``Dict[str, str]`` :ref:`📖<Global environment variables>`
**env** : ``dict[str, str]`` :ref:`📖<Global environment variables>`
Define environment variables to be exposed to all tasks. These can be :ref:`extended on the task level<Setting task specific environment variables>`.

**envfile** : ``str`` | ``List[str]`` :ref:`📖<Global environment variables>`
**envfile** : ``str`` | ``list[str]`` :ref:`📖<Global environment variables>`
Link to one or more files defining environment variables to be exposed to all tasks.

**executor** : ``Dict[str, str]`` :ref:`📖<Change the executor type>`
**executor** : ``dict[str, str]`` :ref:`📖<Change the executor type>`
Override the default behavior for selecting an executor for tasks in this project.

**include** : ``str`` | ``List[str]`` | ``Dict[str, str]`` :doc:`📖<../guides/include_guide>`
**include** : ``str`` | ``list[str]`` | ``dict[str, str]`` :doc:`📖<../guides/include_guide>`
Specify one or more other toml or json files to load tasks from.

**shell_interpreter** : ``str`` | ``List[str]`` :ref:`📖<Change the default shell interpreter>`
**shell_interpreter** : ``str`` | ``list[str]`` :ref:`📖<Change the default shell interpreter>`
Change the default interpreter to use for executing :doc:`shell tasks<../tasks/task_types/shell>`.

**poetry_command** : ``str`` :ref:`📖<Configuring the plugin>`
Change the name of the task poe registers with poetry when used as a plugin.

**poetry_hooks** : ``Dict[str, str]`` :ref:`📖<Hooking into poetry commands>`
**poetry_hooks** : ``dict[str, str]`` :ref:`📖<Hooking into poetry commands>`
Register tasks to run automatically before or after other poetry CLI commands.

**verbosity** : ``int``
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/args_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Named arguments support the following configuration options:
- **name** : ``str``
The name of the task. Only applicable when *args* is an array.

- **options** : ``List[str]``
- **options** : ``list[str]``
A list of options to accept for this argument, similar to `argsparse name or flags <https://docs.python.org/3/library/argparse.html#name-or-flags>`_. If not provided then the name of the argument is used. You can use this option to expose a different name to the CLI vs the name that is used inside the task, or to specify long and short forms of the CLI option, e.g. ``["-h", "--help"]``.

- **positional** : ``bool``
Expand Down
10 changes: 5 additions & 5 deletions docs/tasks/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ The following options can be configured on your tasks and are not specific to an
**help** : ``str`` | ``int`` :doc:`📖<../guides/help_guide>`
Help text to be displayed next to the task name in the documentation when poe is run without specifying a task.

**args** : ``Dict[str, dict]`` | ``List[Union[str, dict]]`` :doc:`📖<../guides/args_guide>`
**args** : ``dict[str, dict]`` | ``list[Union[str, dict]]`` :doc:`📖<../guides/args_guide>`
Define CLI options, positional arguments, or flags that this task should accept.

**env** : ``Dict[str, str]`` :ref:`📖<Setting task specific environment variables>`
**env** : ``dict[str, str]`` :ref:`📖<Setting task specific environment variables>`
A map of environment variables to be set for this task.

**envfile** : ``str`` | ``List[str]`` :ref:`📖<Loading environment variables from an env file>`
**envfile** : ``str`` | ``list[str]`` :ref:`📖<Loading environment variables from an env file>`
Provide one or more env files to be loaded before running this task.

**cwd** : ``str`` :ref:`📖<Running a task with a specific working directory>`
Specify the current working directory that this task should run with. The given path is resolved relative to the parent directory of the ``pyproject.toml``, or it may be absolute.
Resolves environment variables in the format ``${VAR_NAME}``.

**deps** : ``List[str]`` :doc:`📖<../guides/composition_guide>`
**deps** : ``list[str]`` :doc:`📖<../guides/composition_guide>`
A list of task invocations that will be executed before this one.

**uses** : ``Dict[str, str]`` :doc:`📖<../guides/composition_guide>`
**uses** : ``dict[str, str]`` :doc:`📖<../guides/composition_guide>`
Allows this task to use the output of other tasks which are executed first.
The value is a map where the values are invocations of the other tasks, and the keys are environment variables by which the results of those tasks will be accessible in this task.

Expand Down
2 changes: 1 addition & 1 deletion docs/tasks/task_types/expr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Available task options

The following options are also accepted:

**imports** : ``List[str]`` :ref:`📖<Referencing imported modules in an expression>`
**imports** : ``list[str]`` :ref:`📖<Referencing imported modules in an expression>`
A list of modules to import for use in the expression.

**assert** : ``bool`` :ref:`📖<Fail if the expression result is falsey>`
Expand Down
2 changes: 1 addition & 1 deletion docs/tasks/task_types/shell.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Available task options

The following options are also accepted:

**interpreter** : ``str`` | ``List[str]`` :ref:`📖<Using a different shell interpreter>`
**interpreter** : ``str`` | ``list[str]`` :ref:`📖<Using a different shell interpreter>`
Specify the shell interpreter that this task should execute with, or a list of interpreters in order of preference.


Expand Down
19 changes: 5 additions & 14 deletions poethepoet/app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import os
import sys
from collections.abc import Mapping, Sequence
from pathlib import Path
from typing import (
IO,
TYPE_CHECKING,
Any,
Dict,
Mapping,
Optional,
Sequence,
Tuple,
Union,
)
from typing import IO, TYPE_CHECKING, Any, Optional, Union

from .exceptions import ExecutionError, PoeException

Expand Down Expand Up @@ -57,7 +48,7 @@ class PoeThePoet:
ui: "PoeUi"
config: "PoeConfig"

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

def __init__(
self,
Expand Down Expand Up @@ -242,8 +233,8 @@ def print_help(
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
7 changes: 5 additions & 2 deletions poethepoet/completion/zsh.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Any, Iterable, Set
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from collections.abc import Iterable


def get_zsh_completion_script(name: str = "") -> str:
Expand Down Expand Up @@ -50,7 +53,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
2 changes: 1 addition & 1 deletion poethepoet/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .config import PoeConfig
from .partition import KNOWN_SHELL_INTERPRETERS, ConfigPartition

__all__ = ["PoeConfig", "ConfigPartition", "KNOWN_SHELL_INTERPRETERS"]
__all__ = ["KNOWN_SHELL_INTERPRETERS", "ConfigPartition", "PoeConfig"]
23 changes: 7 additions & 16 deletions poethepoet/config/config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
from collections.abc import Iterator, Mapping, Sequence
from os import environ
from pathlib import Path
from typing import (
Any,
Dict,
Iterator,
List,
Mapping,
Optional,
Sequence,
Tuple,
Union,
)
from typing import Any, Optional, Union

from ..exceptions import ConfigValidationError, PoeException
from .file import PoeConfigFile
Expand All @@ -21,12 +12,12 @@

class PoeConfig:
_project_config: ProjectConfig
_included_config: List[IncludedConfig]
_included_config: list[IncludedConfig]

"""
The filenames to look for when loading config
"""
_config_filenames: Tuple[str, ...] = (
_config_filenames: tuple[str, ...] = (
"pyproject.toml",
"poe_tasks.toml",
"poe_tasks.yaml",
Expand Down Expand Up @@ -61,7 +52,7 @@ def __init__(

def lookup_task(
self, name: str
) -> Union[Tuple[Mapping[str, Any], ConfigPartition], Tuple[None, None]]:
) -> Union[tuple[Mapping[str, Any], ConfigPartition], tuple[None, None]]:
task = self._project_config.get("tasks", {}).get(name, None)
if task is not None:
return task, self._project_config
Expand Down Expand Up @@ -95,7 +86,7 @@ def task_names(self) -> Iterator[str]:
yield from result

@property
def tasks(self) -> Dict[str, Any]:
def tasks(self) -> dict[str, Any]:
result = dict(self._project_config.get("tasks", {}))
for config in self._included_config:
for task_name, task_def in config.get("tasks", {}).items():
Expand All @@ -117,7 +108,7 @@ def default_array_item_task_type(self) -> str:
return self._project_config.options.default_array_item_task_type

@property
def shell_interpreter(self) -> Tuple[str, ...]:
def shell_interpreter(self) -> tuple[str, ...]:
raw_value = self._project_config.options.shell_interpreter
if isinstance(raw_value, list):
return tuple(raw_value)
Expand Down
9 changes: 2 additions & 7 deletions poethepoet/config/file.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
from collections.abc import Iterator, Mapping, Sequence
from pathlib import Path
from typing import (
Any,
Iterator,
Mapping,
Optional,
Sequence,
)
from typing import Any, Optional

from ..exceptions import PoeException

Expand Down
15 changes: 4 additions & 11 deletions poethepoet/config/partition.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
from collections.abc import Mapping, Sequence
from pathlib import Path
from types import MappingProxyType
from typing import (
Any,
Mapping,
Optional,
Sequence,
Type,
TypedDict,
Union,
)
from typing import Any, Optional, TypedDict, Union

from ..exceptions import ConfigValidationError
from ..options import NoValue, PoeOptions
Expand Down Expand Up @@ -42,7 +35,7 @@ class ConfigPartition:
project_dir: Path
_cwd: Optional[Path]

ConfigOptions: Type[PoeOptions]
ConfigOptions: type[PoeOptions]
is_primary: bool = False

def __init__(
Expand Down Expand Up @@ -115,7 +108,7 @@ def normalize(
raise ConfigValidationError("Expected ")

# Normalize include option:
# > Union[str, Sequence[str], Mapping[str, str]] => List[dict]
# > Union[str, Sequence[str], Mapping[str, str]] => list[dict]
if "include" in config:
includes: Any = []
include_option = config.get("include", None)
Expand Down
3 changes: 2 additions & 1 deletion poethepoet/config/primitives.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Mapping
from types import MappingProxyType
from typing import Mapping, TypedDict
from typing import TypedDict

EmptyDict: Mapping = MappingProxyType({})

Expand Down
17 changes: 9 additions & 8 deletions poethepoet/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from collections.abc import Mapping
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Tuple, Union
from typing import TYPE_CHECKING, Any, Optional, Union

if TYPE_CHECKING:
from .config import PoeConfig
Expand All @@ -17,8 +18,8 @@ class RunContext:
poe_active: Optional[str]
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,
Expand Down Expand Up @@ -52,8 +53,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 +63,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 +77,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,7 +88,7 @@ def get_task_output(self, invocation: Tuple[str, ...]):

def get_executor(
self,
invocation: Tuple[str, ...],
invocation: tuple[str, ...],
env: "EnvVarsManager",
working_dir: Path,
*,
Expand Down
6 changes: 3 additions & 3 deletions poethepoet/env/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from os import environ
from pathlib import Path
from typing import TYPE_CHECKING, Dict, Optional, Union
from typing import TYPE_CHECKING, Optional, Union

from ..exceptions import ExecutionError

Expand All @@ -11,15 +11,15 @@


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

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

def get(self, envfile: Union[str, Path]) -> Dict[str, str]:
def get(self, envfile: Union[str, Path]) -> dict[str, str]:
"""
Parse, cache, and return the environment variables from the envfile at the
given path. The path is used as the cache key.
Expand Down
Loading

0 comments on commit dde728d

Please sign in to comment.