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
28 changes: 28 additions & 0 deletions simple_parsing/wrappers/field_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
is_forward_ref,
is_homogeneous_tuple_type,
is_list,
is_literal,
is_tuple,
is_typevar,
is_union,
Expand Down Expand Up @@ -127,6 +128,10 @@ def get_parsing_fn(t: type[T]) -> Callable[[Any], T]:
args = get_type_arguments(t)
return parse_union(*args)

elif is_literal(t):
logger.debug(f"Parsing a Literal field of type {t}")
return parse_literal(t)

elif is_enum(t):
logger.debug(f"Parsing an Enum field of type {t}")
return parse_enum(t)
Expand Down Expand Up @@ -205,6 +210,29 @@ def _parse_optional(val: Optional[Any]) -> Optional[T]:
return _parse_optional


def parse_literal(literal_type: type[T]) -> Callable[[str], T]:
"""Returns a parsing function for a Literal type.

The function maps the string representation of each literal value back to the actual value
(e.g. "1" -> 1, "BLUE" -> Color.BLUE).
"""
literal_values = get_type_arguments(literal_type)
# Build a mapping from the string representation to the actual value.
choice_dict: dict[str, Any] = {
(v.name if isinstance(v, enum.Enum) else str(v)): v for v in literal_values
}

def _parse_literal(val: str) -> T:
if val in choice_dict:
return choice_dict[val]
raise ValueError(
f"Invalid value {val!r} for {literal_type}. Expected one of: {list(choice_dict)}"
)

_parse_literal.__name__ = str(literal_type)
return _parse_literal


def parse_tuple(tuple_item_types: tuple[type[T], ...]) -> Callable[[list[T]], tuple[T, ...]]:
"""Makes a parsing function for creating tuples from the command-line args.

Expand Down
20 changes: 19 additions & 1 deletion test/test_literal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import enum
import sys
from dataclasses import dataclass
from typing import Any, NamedTuple, Optional
from typing import Any, NamedTuple, Optional, Union

import pytest
from typing_extensions import Literal
Expand Down Expand Up @@ -118,3 +118,21 @@ def test_reproduce_issue_259_parsing_literal_py39():
"argument --param: invalid typing.Literal['bar', 'biz'] value: 'biz'"
):
assert SomeFoo.setup("").param == "biz"


@dataclass
class Foo:
bar: Union[Literal["a"], int] = "a"


def test_issue_322():
"""Test for https://github.com/lebrice/SimpleParsing/issues/322."""
from simple_parsing import parse

assert parse(Foo, args="") == Foo()
assert parse(Foo, args="--bar=a") == Foo(bar="a")
# 'b' is neither in Literal["a"] nor a valid int, so it should be rejected.
with exits_and_writes_to_stderr("invalid"):
assert parse(Foo, args="--bar=b")

assert parse(Foo, args="--bar=123") == Foo(bar=123)
Loading