Skip to content

Commit

Permalink
fix: Handle TypeError in mapping failure also.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Dec 15, 2023
1 parent a0c133e commit 911bb39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cappa"
version = "0.14.2"
version = "0.14.3"
description = "Declarative CLI argument parser."

repository = "https://github.com/dancardin/cappa"
Expand Down
2 changes: 1 addition & 1 deletion src/cappa/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def union_mapper(value):
for mapper in mappers:
try:
return mapper(value)
except ValueError:
except (ValueError, TypeError):
pass

raise ValueError(
Expand Down
16 changes: 16 additions & 0 deletions tests/arg/test_mapping_failure.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Union

import cappa
import pytest
from typing_extensions import Annotated

from tests.utils import backends, parse

Expand All @@ -22,3 +25,16 @@ class ArgTest:
e.value.message
== "Invalid value for 'default' with value 'foo': invalid literal for int() with base 10: 'foo'"
)


@backends
def test_other_exception_types(backend):
@dataclass
class ArgTest:
path: Annotated[Union[Path, None], cappa.Arg(long=True)] = None

result = parse(ArgTest, backend=backend)
assert result.path is None

result = parse(ArgTest, "--path", "asdf", backend=backend)
assert result.path == Path("asdf")

0 comments on commit 911bb39

Please sign in to comment.