Skip to content

Commit

Permalink
remove unneeded type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Mar 17, 2024
1 parent c4085a4 commit 4bb60cc
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 41 deletions.
12 changes: 7 additions & 5 deletions copier/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@
```
"""

from __future__ import annotations

import sys
from os import PathLike
from pathlib import Path
from textwrap import dedent
from typing import Any, Callable
from typing import Any, Callable, Iterable

import yaml
from plumbum import cli, colors

from .errors import UnsafeTemplateError, UserMessageError
from .main import Worker
from .tools import copier_version
from .types import AnyByStrDict, OptStr, StrSeq
from .types import AnyByStrDict


def _handle_exceptions(method: Callable[[], None]) -> int:
Expand Down Expand Up @@ -105,7 +107,7 @@ class CopierApp(cli.Application): # type: ignore[misc]
class _Subcommand(cli.Application): # type: ignore[misc]
"""Base class for Copier subcommands."""

def __init__(self, executable: "PathLike[str]") -> None:
def __init__(self, executable: PathLike[str]) -> None:
self.data: AnyByStrDict = {}
super().__init__(executable)

Expand Down Expand Up @@ -162,7 +164,7 @@ def __init__(self, executable: "PathLike[str]") -> None:
list=True,
help="Make VARIABLE available as VALUE when rendering the template",
)
def data_switch(self, values: StrSeq) -> None:
def data_switch(self, values: Iterable[str]) -> None:
"""Update [data][] with provided values.
Arguments:
Expand Down Expand Up @@ -193,7 +195,7 @@ def data_file_switch(self, path: cli.ExistingFile) -> None:
self.data.update(updates_without_cli_overrides)

def _worker(
self, src_path: OptStr = None, dst_path: str = ".", **kwargs: Any
self, src_path: str | None = None, dst_path: str = ".", **kwargs: Any
) -> Worker:
"""Run Copier's internal API using CLI switches.
Expand Down
24 changes: 7 additions & 17 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@
from .subproject import Subproject
from .template import Task, Template
from .tools import OS, Style, normalize_git_path, printf, readlink
from .types import (
MISSING,
AnyByStrDict,
JSONSerializable,
OptStr,
RelativePath,
StrOrPath,
StrSeq,
)
from .types import MISSING, AnyByStrDict, JSONSerializable, RelativePath, StrOrPath
from .user_data import DEFAULT_DATA, AnswersMap, Question
from .vcs import get_git

Expand Down Expand Up @@ -173,11 +165,11 @@ class Worker:
src_path: str | None = None
dst_path: Path = Path(".")
answers_file: RelativePath | None = None
vcs_ref: OptStr = None
vcs_ref: str | None = None
data: AnyByStrDict = field(default_factory=dict)
exclude: StrSeq = ()
exclude: Sequence[str] = ()
use_prereleases: bool = False
skip_if_exists: StrSeq = ()
skip_if_exists: Sequence[str] = ()
cleanup_on_error: bool = True
defaults: bool = False
user_defaults: AnyByStrDict = field(default_factory=dict)
Expand All @@ -197,14 +189,12 @@ def __enter__(self) -> Worker:
return self

@overload
def __exit__(self, type: None, value: None, traceback: None) -> None:
...
def __exit__(self, type: None, value: None, traceback: None) -> None: ...

@overload
def __exit__(
self, type: type[BaseException], value: BaseException, traceback: TracebackType
) -> None:
...
) -> None: ...

def __exit__(
self,
Expand Down Expand Up @@ -494,7 +484,7 @@ def answers_relpath(self) -> Path:
return Path(template.render(**self.answers.combined))

@cached_property
def all_exclusions(self) -> StrSeq:
def all_exclusions(self) -> Sequence[str]:
"""Combine default, template and user-chosen exclusions."""
return self.template.exclude + tuple(self.exclude)

Expand Down
4 changes: 2 additions & 2 deletions copier/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
UnsupportedVersionError,
)
from .tools import copier_version, handle_remove_readonly
from .types import AnyByStrDict, Env, StrSeq, VCSTypes
from .types import AnyByStrDict, Env, VCSTypes
from .vcs import checkout_latest_tag, clone, get_git, get_repo

# Default list of files in the template to exclude from the rendered project
Expand Down Expand Up @@ -432,7 +432,7 @@ def secret_questions(self) -> set[str]:
return result

@cached_property
def skip_if_exists(self) -> StrSeq:
def skip_if_exists(self) -> Sequence[str]:
"""Get skip patterns from the template.
These files will never be rewritten when rendering the template.
Expand Down
2 changes: 0 additions & 2 deletions copier/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@

# sequences
IntSeq = Sequence[int]
StrSeq = Sequence[str]
PathSeq = Sequence[Path]

# optional types
OptBool = Optional[bool]
OptStrOrPath = Optional[StrOrPath]
OptStr = Optional[str]

# miscellaneous
T = TypeVar("T")
Expand Down
4 changes: 2 additions & 2 deletions copier/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from .errors import InvalidTypeError, UserMessageError
from .tools import cast_to_bool, cast_to_str, force_str_end
from .types import MISSING, AnyByStrDict, MissingType, OptStr, OptStrOrPath, StrOrPath
from .types import MISSING, AnyByStrDict, MissingType, OptStrOrPath, StrOrPath


# TODO Remove these two functions as well as DEFAULT_DATA in a future release
Expand Down Expand Up @@ -109,7 +109,7 @@ def combined(self) -> Mapping[str, Any]:
)
)

def old_commit(self) -> OptStr:
def old_commit(self) -> str | None:
"""Commit when the project was updated from this template the last time."""
return self.last.get("_commit")

Expand Down
9 changes: 6 additions & 3 deletions copier/vcs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Utilities related to VCS."""

from __future__ import annotations

import os
import re
import sys
Expand All @@ -13,7 +16,7 @@
from plumbum.machines import LocalCommand

from .errors import DirtyLocalWarning, ShallowCloneWarning
from .types import OptBool, OptStr, OptStrOrPath, StrOrPath
from .types import OptBool, OptStrOrPath, StrOrPath


def get_git(context_dir: OptStrOrPath = None) -> LocalCommand:
Expand Down Expand Up @@ -80,7 +83,7 @@ def is_git_bundle(path: Path) -> bool:
return bool(get_git()["bundle", "verify", path] & TF)


def get_repo(url: str) -> OptStr:
def get_repo(url: str) -> str | None:
"""Transform `url` into a git-parseable origin URL.
Args:
Expand Down Expand Up @@ -146,7 +149,7 @@ def checkout_latest_tag(local_repo: StrOrPath, use_prereleases: OptBool = False)
return latest_tag


def clone(url: str, ref: OptStr = None) -> str:
def clone(url: str, ref: str | None = None) -> str:
"""Clone repo into some temporary destination.
Includes dirty changes for local templates by copying into a temp
Expand Down
7 changes: 3 additions & 4 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from prompt_toolkit.keys import Keys

import copier
from copier.types import OptStr, StrOrPath
from copier.types import StrOrPath

PROJECT_TEMPLATE = Path(__file__).parent / "demo"

Expand Down Expand Up @@ -61,8 +61,7 @@


class Spawn(Protocol):
def __call__(self, cmd: tuple[str, ...], *, timeout: int | None) -> PopenSpawn:
...
def __call__(self, cmd: tuple[str, ...], *, timeout: int | None) -> PopenSpawn: ...


class Keyboard(str, Enum):
Expand Down Expand Up @@ -124,7 +123,7 @@ def build_file_tree(


def expect_prompt(
tui: PopenSpawn, name: str, expected_type: str, help: OptStr = None
tui: PopenSpawn, name: str, expected_type: str, help: str | None = None
) -> None:
"""Check that we get a prompt in the standard form"""
if help:
Expand Down
7 changes: 5 additions & 2 deletions tests/test_answersfile.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from pathlib import Path
from textwrap import dedent

import pytest

import copier
from copier.types import OptStr
from copier.user_data import load_answersfile_data

from .helpers import BRACKET_ENVOPS_JSON, SUFFIX_TMPL, build_file_tree
Expand Down Expand Up @@ -52,7 +53,9 @@ def template_path(tmp_path_factory: pytest.TempPathFactory) -> str:


@pytest.mark.parametrize("answers_file", [None, ".changed-by-user.yaml"])
def test_answersfile(template_path: str, tmp_path: Path, answers_file: OptStr) -> None:
def test_answersfile(
template_path: str, tmp_path: Path, answers_file: str | None
) -> None:
"""Test copier behaves properly when using an answersfile."""
round_file = tmp_path / "round.txt"

Expand Down
5 changes: 3 additions & 2 deletions tests/test_complex_questions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from collections import deque
from pathlib import Path
Expand All @@ -11,7 +13,6 @@
from plumbum.cmd import git

from copier import run_copy, run_update
from copier.types import OptStr

from .helpers import (
BRACKET_ENVOPS,
Expand Down Expand Up @@ -122,7 +123,7 @@ def check_invalid(
name: str,
format: str,
invalid_value: str,
help: OptStr = None,
help: str | None = None,
err: str = "Invalid input",
) -> None:
"""Check that invalid input is reported correctly"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_templated_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from copier import Worker
from copier.errors import InvalidTypeError
from copier.types import AnyByStrDict, OptStr
from copier.types import AnyByStrDict

from .helpers import (
BRACKET_ENVOPS,
Expand All @@ -32,7 +32,7 @@


class Prompt:
def __init__(self, name: str, format: str, help: OptStr = None) -> None:
def __init__(self, name: str, format: str, help: str | None = None) -> None:
self.name = name
self.format = format
self.help = help
Expand Down

0 comments on commit 4bb60cc

Please sign in to comment.