From 1a8f5c6701e9de82895ad85e4b98449e0892f472 Mon Sep 17 00:00:00 2001 From: Fabrice Normandin Date: Thu, 5 Sep 2024 16:15:49 -0400 Subject: [PATCH 1/2] Add test to repro #322 Signed-off-by: Fabrice Normandin --- test/test_literal.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/test_literal.py b/test/test_literal.py index caac3be4..796953b3 100644 --- a/test/test_literal.py +++ b/test/test_literal.py @@ -1,10 +1,9 @@ import enum import sys from dataclasses import dataclass -from typing import Any, List, NamedTuple, Optional +from typing import Any, List, Literal, NamedTuple, Optional, Union import pytest -from typing_extensions import Literal from .testutils import ( TestSetup, @@ -118,3 +117,22 @@ 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") + with raises_invalid_choice(): + assert parse(Foo, args="--bar=b") + + assert parse(Foo, args="--bar=123") == Foo(bar=123) + # TODO: What about this again? + assert parse(Foo, args="--bar=1.23") == Foo(bar=1) From 3179647e1f697a9def0a9190d67b45709bd834e2 Mon Sep 17 00:00:00 2001 From: Fabrice Normandin Date: Mon, 27 Jul 2026 16:08:31 -0400 Subject: [PATCH 2/2] Fix pre-commit issues Signed-off-by: Fabrice Normandin --- simple_parsing/wrappers/field_parsing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simple_parsing/wrappers/field_parsing.py b/simple_parsing/wrappers/field_parsing.py index ebd47b14..493c4b3c 100644 --- a/simple_parsing/wrappers/field_parsing.py +++ b/simple_parsing/wrappers/field_parsing.py @@ -213,8 +213,8 @@ def _parse_optional(val: Optional[Any]) -> Optional[T]: 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). + 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.