From 1a8f5c6701e9de82895ad85e4b98449e0892f472 Mon Sep 17 00:00:00 2001 From: Fabrice Normandin Date: Thu, 5 Sep 2024 16:15:49 -0400 Subject: [PATCH] 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)