-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from DanCardin/dc/msgspec
feat: Support msgspec defined classes as source classes for cappa.
- Loading branch information
Showing
9 changed files
with
420 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
import cappa | ||
import msgspec | ||
from typing_extensions import Annotated | ||
|
||
from tests.utils import backends, parse | ||
|
||
|
||
class PydanticCommand(msgspec.Struct): | ||
name: str | ||
foo: Annotated[int, cappa.Arg(short=True)] | ||
|
||
|
||
@backends | ||
def test_base_model(backend): | ||
result = parse(PydanticCommand, "meow", "-f", "4", backend=backend) | ||
assert result == PydanticCommand(name="meow", foo=4) | ||
|
||
|
||
class OptSub(msgspec.Struct): | ||
name: Optional[str] = None | ||
|
||
|
||
class OptionalSubcommand(msgspec.Struct): | ||
sub: cappa.Subcommands[Optional[OptSub]] = None | ||
|
||
|
||
@backends | ||
def test_optional_subcommand(backend): | ||
result = parse(OptionalSubcommand, backend=backend) | ||
assert result == OptionalSubcommand(sub=None) | ||
|
||
result = parse(OptionalSubcommand, "opt-sub", backend=backend) | ||
assert result == OptionalSubcommand(sub=OptSub(name=None)) | ||
|
||
result = parse(OptionalSubcommand, "opt-sub", "foo", backend=backend) | ||
assert result == OptionalSubcommand(sub=OptSub(name="foo")) |