~gotmax23/tomcli

tomcli: CI: actually run type checks and fix typo v2 APPLIED

Maxwell G: 2
 CI: actually run type checks and fix typo
 fix type errors

 6 files changed, 39 insertions(+), 31 deletions(-)
#974008 nox-lint.yml success
#974009 nox-mockbuild-36.yml success
#974010 nox-mockbuild.yml success
#974011 nox.yml success
tomcli/patches: SUCCESS in 5m26s

[CI: actually run type checks and fix typo][0] v2 from [Maxwell G][1]

[0]: https://lists.sr.ht/~gotmax23/tomcli/patches/40392
[1]: mailto:maxwell@gtmx.me

✓ #974010 SUCCESS tomcli/patches/nox-mockbuild.yml    https://builds.sr.ht/~gotmax23/job/974010
✓ #974008 SUCCESS tomcli/patches/nox-lint.yml         https://builds.sr.ht/~gotmax23/job/974008
✓ #974011 SUCCESS tomcli/patches/nox.yml              https://builds.sr.ht/~gotmax23/job/974011
✓ #974009 SUCCESS tomcli/patches/nox-mockbuild-36.yml https://builds.sr.ht/~gotmax23/job/974009
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~gotmax23/tomcli/patches/40392/mbox | git am -3
Learn more about email & git

[PATCH tomcli v2 1/2] CI: actually run type checks and fix typo Export this patch

:facepalm:
---
 noxfile.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/noxfile.py b/noxfile.py
index 85c1617..d0a8042 100644
--- a/noxfile.py
+++ b/noxfile.py
@@ -74,6 +74,7 @@ def test(session: nox.Session):
@nox.session
def typing(session: nox.Session):
    install(session, ".[typing]", editable=True)
    session.run("mypy", "src")


@nox.session
@@ -189,8 +190,8 @@ def _sign_artifacts(session: nox.Session) -> None:
    dist = Path("dist")
    artifacts = [str(p) for p in (*dist.glob("*.whl"), *dist.glob("*.tar.gz"))]
    for path in artifacts:
        if Path(path).exists():
            session.warn(f"{path} already exists. Not signing it.")
        if Path(path + ".asc").exists():
            session.warn(f"{path}.asc already exists. Not signing it.")
            continue
        session.run(
            "gpg", "--local-user", uid, "--armor", "--detach-sign", path, external=True
-- 
2.39.2
Applied!
--
Maxwell G (@gotmax23)
Pronouns: He/They

[PATCH tomcli v2 2/2] fix type errors Export this patch

These would've been caught if we were actually running type checks.
---
 pyproject.toml          |  2 ++
 src/tomcli/cli/_util.py |  8 ++++----
 src/tomcli/cli/get.py   |  4 ++--
 src/tomcli/cli/set.py   | 41 +++++++++++++++++++++++------------------
 src/tomcli/toml.py      | 10 +++++-----
 5 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/pyproject.toml b/pyproject.toml
index 0f5bf32..1210722 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -58,6 +58,8 @@ formatters = [
    "ruff",
]
typing = [
    "tomcli[tomli]",
    "tomcli[tomlkit]",
    "mypy",
]
test = [
diff --git a/src/tomcli/cli/_util.py b/src/tomcli/cli/_util.py
index a485d62..6a221fc 100644
--- a/src/tomcli/cli/_util.py
+++ b/src/tomcli/cli/_util.py
@@ -3,15 +3,15 @@
# SPDX-License-Identifier: MIT
from __future__ import annotations

from collections.abc import Iterable
from collections.abc import Iterator
from contextlib import contextmanager
from typing import IO, Any
from typing import IO, AnyStr, cast


@contextmanager
def _std_cm(path: str, dash_stream: str, mode: str) -> Iterable[IO[Any]]:
def _std_cm(path: str, dash_stream: IO[AnyStr], mode: str) -> Iterator[IO[AnyStr]]:
    if str(path) == "-":
        yield dash_stream
    else:
        with open(path, mode) as fp:
            yield fp
            yield cast(IO[AnyStr], fp)
diff --git a/src/tomcli/cli/get.py b/src/tomcli/cli/get.py
index 232c57f..cd14f7a 100644
--- a/src/tomcli/cli/get.py
+++ b/src/tomcli/cli/get.py
@@ -10,7 +10,7 @@ import sys
from collections.abc import Mapping, MutableMapping
from typing import Any, Optional

from typer import Argument, Exit, Typer
from typer import Argument, Typer

from tomcli.cli._util import _std_cm
from tomcli.toml import Reader, Writer, dump, load
@@ -31,7 +31,7 @@ def get_part(data: MutableMapping[str, Any], selector: str) -> Any:
    except (IndexError, KeyError):
        up_to = ".".join(parts[: idx + 1])
        msg = f"Invalid selector {selector!r}: could not find {up_to!r}"
        raise Exit(msg) from None
        sys.exit(msg)
    return cur


diff --git a/src/tomcli/cli/set.py b/src/tomcli/cli/set.py
index ba14355..e0c4ac8 100644
--- a/src/tomcli/cli/set.py
+++ b/src/tomcli/cli/set.py
@@ -12,7 +12,12 @@ import sys
from collections.abc import Callable, Mapping, MutableMapping, MutableSequence
from typing import Any, List, Optional, TypeVar

from typer import Argument, Context, Exit, Option, Typer
if sys.version_info >= (3, 10):
    from types import EllipsisType
else:
    EllipsisType = type(Ellipsis)

from typer import Argument, Context, Option, Typer

from tomcli.cli._util import _std_cm
from tomcli.toml import Reader, Writer, dump, load
@@ -29,8 +34,8 @@ SELECTOR_HELP = (
class ModderCtx:
    path: str
    out: str
    reader: str | None = None
    writer: str | None = None
    reader: Reader | None = None
    writer: Writer | None = None
    allow_fallback_r: bool = True
    allow_fallback_w: bool = True

@@ -81,7 +86,7 @@ def delete(
    """
    Delete a value from a TOML file.
    """
    modder: ModderCtx = ctx.find_object(ModderCtx)
    modder: ModderCtx = ctx.ensure_object(ModderCtx)
    modder.set_default_rw(Reader.TOMLKIT, Writer.TOMLKIT)
    fun_msg = "Thank you for your patronage, but we won't delete the whole file."
    set_type(
@@ -98,7 +103,7 @@ def string(
    """
    Set a string value in a TOML file
    """
    modder: ModderCtx = ctx.find_object(ModderCtx)
    modder: ModderCtx = ctx.ensure_object(ModderCtx)
    modder.set_default_rw(Reader.TOMLKIT, Writer.TOMLKIT)
    fun_msg = (
        "Your heart is in the right place,"
@@ -106,7 +111,6 @@ def string(
    )
    return set_type(
        typ=str,
        callback=operator.setitem,
        default=dict,
        fun_msg=fun_msg,
        modder=modder,
@@ -128,17 +132,18 @@ def integer(
        "Go outside and contemplate your choice"
        " to replace the whole file with integer."
    )
    modder: ModderCtx = ctx.find_object(ModderCtx)
    modder: ModderCtx = ctx.ensure_object(ModderCtx)
    modder.set_default_rw(Reader.TOMLKIT, Writer.TOMLKIT)
    final: Any = value
    if "." in value:
        value = round(float(value))
        final = round(float(value))
    return set_type(
        typ=int,
        default=dict,
        fun_msg=fun_msg,
        modder=modder,
        selector=selector,
        value=value,
        value=final,
    )


@@ -151,7 +156,7 @@ def float_(ctx: Context, selector: str = Argument(...), value: float = Argument(
        "I'll be very sad if you replace the whole TOML file with a string."
        " Computers have feelings too, ya know."
    )
    modder: ModderCtx = ctx.find_object(ModderCtx)
    modder: ModderCtx = ctx.ensure_object(ModderCtx)
    modder.set_default_rw(Reader.TOMLKIT, Writer.TOMLKIT)
    return set_type(
        typ=float,
@@ -168,7 +173,7 @@ def lst(ctx: Context, selector: str = Argument(...), value: List[str] = Argument
    """
    Create a list of strings in a TOML file
    """
    modder: ModderCtx = ctx.find_object(ModderCtx)
    modder: ModderCtx = ctx.ensure_object(ModderCtx)
    modder.set_default_rw(Reader.TOMLKIT, Writer.TOMLKIT)
    fun_msg = (
        "A list is not a Mapping and therefore can't be the root."
@@ -192,7 +197,7 @@ def append(
    """
    Append strings to an existing list in a TOML file
    """
    modder: ModderCtx = ctx.find_object(ModderCtx)
    modder: ModderCtx = ctx.ensure_object(ModderCtx)
    modder.set_default_rw(Reader.TOMLKIT, Writer.TOMLKIT)
    return set_type(
        fun_msg=None,
@@ -206,7 +211,7 @@ def append(
def _append_callback(cur: MutableMapping[str, Any], part: str, value: list[Any]):
    lst = cur.get(part)
    if not isinstance(lst, MutableSequence):
        raise Exit(
        sys.exit(
            "You can only append values to an existing list."
            " Use the 'list' subcommand to create a new list"
        )
@@ -221,11 +226,11 @@ def set_type(
    typ: Callable[[Any], T] = lambda x: x,
    callback: Callable[[MutableMapping[str, Any], str, T], Any]
    | Callable[[MutableMapping[str, Any], str], Any] = operator.setitem,
    default: Callable[[], Any] | Ellipsis = ...,
    default: Callable[[], Any] | EllipsisType = ...,
    fun_msg: str | None = "Invalid selector: '.'",
    modder: ModderCtx,
    selector: str,
    value: str | Ellipsis = ...,
    value: Any = ...,
):
    """
    Iterate over a TOML file based on a dot-separated selector and preform on
@@ -262,7 +267,7 @@ def set_type(
    parts = selector.split(".")
    if selector == ".":
        if fun_msg:
            raise Exit(fun_msg)
            sys.exit(fun_msg)
        else:
            cur = {"data": cur}
            parts = ["data"]
@@ -275,9 +280,9 @@ def set_type(
        else:
            cur = cur[part]
    if value is ...:
        callback(cur, part)
        callback(cur, part)  # type: ignore[call-arg]
    else:
        callback(cur, part, typ(value))
        callback(cur, part, typ(value))  # type: ignore[call-arg]
    if selector == ".":
        data = data["data"]
    modder.dump(data)
diff --git a/src/tomcli/toml.py b/src/tomcli/toml.py
index 845d0c1..a238ffa 100644
--- a/src/tomcli/toml.py
+++ b/src/tomcli/toml.py
@@ -10,7 +10,7 @@ import sys
from collections.abc import Iterator, Mapping, MutableMapping
from contextlib import contextmanager
from types import ModuleType
from typing import IO, Any, BinaryIO
from typing import IO, Any


class Reader(enum.Enum):
@@ -33,7 +33,7 @@ class Writer(enum.Enum):

DEFAULT_READER = Reader.TOMLKIT
DEFAULT_WRITER = Writer.TOMLKIT
NEEDS_STR: tuple[Writer | Reader] = [Writer.TOMLKIT]
NEEDS_STR: tuple[Writer | Reader, ...] = (Writer.TOMLKIT,)

AVAILABLE_READERS: dict[Reader, ModuleType] = {}
AVAILABLE_WRITERS: dict[Writer, ModuleType] = {}
@@ -67,7 +67,7 @@ else:


@contextmanager
def _get_stream(fp: BinaryIO, backend: Reader | Writer) -> Iterator[IO[Any]]:
def _get_stream(fp: IO[bytes], backend: Reader | Writer) -> Iterator[IO[Any]]:
    if backend in NEEDS_STR:
        fp.flush()
        wrapper = io.TextIOWrapper(fp, "utf-8")
@@ -81,7 +81,7 @@ def _get_stream(fp: BinaryIO, backend: Reader | Writer) -> Iterator[IO[Any]]:


def load(
    __fp: BinaryIO,
    __fp: IO[bytes],
    prefered_reader: Reader | None = None,
    allow_fallback: bool = True,
) -> MutableMapping[str, Any]:
@@ -120,7 +120,7 @@ def load(

def dump(
    __data: Mapping[str, Any],
    __fp: BinaryIO,
    __fp: IO[bytes],
    prefered_writer: Writer | None = None,
    allow_fallback: bool = True,
) -> None:
-- 
2.39.2
tomcli/patches: SUCCESS in 5m26s

[CI: actually run type checks and fix typo][0] v2 from [Maxwell G][1]

[0]: https://lists.sr.ht/~gotmax23/tomcli/patches/40392
[1]: mailto:maxwell@gtmx.me

✓ #974010 SUCCESS tomcli/patches/nox-mockbuild.yml    https://builds.sr.ht/~gotmax23/job/974010
✓ #974008 SUCCESS tomcli/patches/nox-lint.yml         https://builds.sr.ht/~gotmax23/job/974008
✓ #974011 SUCCESS tomcli/patches/nox.yml              https://builds.sr.ht/~gotmax23/job/974011
✓ #974009 SUCCESS tomcli/patches/nox-mockbuild-36.yml https://builds.sr.ht/~gotmax23/job/974009