Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.32

### 0.32.1

- fix: kw_only dataclass inheritance mis-ordering.

### 0.32.0
- feat: Add `epilog=` field on commands for text which should go after the argument help.
- refactor: All root objects (Command/Arg/Subcommand) to have "Final" variants which represent their post-normalization shape.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "cappa"
version = "0.32.0"
version = "0.32.1"
description = "Declarative CLI argument parser."

urls = { repository = "https://github.com/dancardin/cappa" }
Expand Down
4 changes: 3 additions & 1 deletion src/cappa/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def collect(
raw_subcommands.append((arg, None, None))

else:
for field, param_view in zip(fields, function_view.parameters):
param_by_name = {p.name: p for p in function_view.parameters}
for field in fields:
param_view = param_by_name[field.name]
arg_help = help_text.args.get(param_view.name)

maybe_subcommand = Subcommand.detect(
Expand Down
23 changes: 22 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import cappa
from cappa.command import Command
from tests.utils import Backend, backends
from tests.utils import Backend, backends, require_dataclass_kw_only


@backends
Expand Down Expand Up @@ -165,6 +165,27 @@ class Example: ...
assert result == Example()


@require_dataclass_kw_only
@backends
def test_kw_only_base_class_field_ordering(backend: Backend):
kwargs = {"kw_only": True}

@dataclass(**kwargs)
class Base:
name: str = "hello"

@dataclass
class Child(Base):
number: int = 0

result = cappa.parse(
cappa.Command(Child, default_long=True),
argv=["--name", "world"],
backend=backend,
)
assert result == Child(number=0, name="world")


@backends
def test_collect_skips_non_init_fields(backend: Backend):
@dataclass
Expand Down
6 changes: 6 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import os
import sys
from dataclasses import dataclass
from typing import Any, Union
from unittest.mock import patch
Expand All @@ -23,9 +24,14 @@
"invoke",
"invoke_async",
"parse",
"require_dataclass_kw_only",
"runner",
]

require_dataclass_kw_only = pytest.mark.skipif(
sys.version_info < (3, 10), reason="kw_only requires Python 3.10+"
)

backends = pytest.mark.parametrize(
"backend",
[
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading