@@ -12,6 +12,7 @@ class Env:
1212 t2 : Blank [int ] = None
1313 t3 : Annotated [Blank [int ], Literal [3 ]] = None
1414 t4 : Annotated [Blank [int | bool ], Literal [3 ]] = 2
15+ t5 : Blank [int | Literal ["foo" ]] = None
1516
1617
1718class TestFlag (TestAbstract ):
@@ -20,10 +21,11 @@ def test_flag(self):
2021 (
2122 {
2223 "" : {
23- "t1" : Tag (val = MISSING , description = "" , annotation = int | None , label = "t1" ),
24- "t2" : Tag (val = None , description = "" , annotation = int | None , label = "t2" ),
25- "t3" : Tag (val = None , description = "" , annotation = int | None , label = "t3" ),
24+ "t1" : Tag (val = MISSING , description = "" , annotation = int | bool | None , label = "t1" ),
25+ "t2" : Tag (val = None , description = "" , annotation = int | bool | None , label = "t2" ),
26+ "t3" : Tag (val = None , description = "" , annotation = int | bool | None , label = "t3" ),
2627 "t4" : Tag (val = 2 , description = "" , annotation = int | bool | None , label = "t4" ),
28+ "t5" : Tag (val = None , description = "" , annotation = int | bool | Literal ["foo" ] | None , label = "t5" ),
2729 }
2830 },
2931 {"" : {"t1" : None }},
@@ -34,20 +36,40 @@ def test_flag(self):
3436 def r (* args ):
3537 return asdict (runm (Env , args = args ).env )
3638
37- self .assertDictEqual ({"t1" : 1 , "t2" : None , "t3" : None , "t4" : 2 }, r ("--t1" , "1" ))
38- self .assertDictEqual ({"t1" : 1 , "t2" : 3 , "t3" : None , "t4" : 2 }, r ("--t1" , "1" , "--t2" , "3" ))
39- self .assertDictEqual ({"t1" : 1 , "t2" : True , "t3" : 3 , "t4" : 3 }, r ("--t1" , "1" , "--t2" , "--t3" , "--t4" ))
40- self .assertDictEqual ({"t1" : 10 , "t2" : None , "t3" : None , "t4" : False }, r ("--t1" , "10" , "--t4" , "False" ))
41- self .assertDictEqual ({"t1" : 10 , "t2" : None , "t3" : None , "t4" : True }, r ("--t1" , "10" , "--t4" , "True" ))
42- self .assertDictEqual ({"t1" : 10 , "t2" : None , "t3" : None , "t4" : 44 }, r ("--t1" , "10" , "--t4" , "44" ))
39+ self .assertDictEqual ({"t1" : 1 , "t2" : None , "t3" : None , "t4" : 2 , "t5" : None }, r ("--t1" , "1" ))
40+ self .assertDictEqual ({"t1" : 1 , "t2" : 3 , "t3" : None , "t4" : 2 , "t5" : None }, r ("--t1" , "1" , "--t2" , "3" ))
41+ self .assertDictEqual (
42+ {"t1" : 1 , "t2" : True , "t3" : 3 , "t4" : 3 , "t5" : True }, r ("--t1" , "1" , "--t2" , "--t3" , "--t4" ,"--t5" )
43+ )
44+ self .assertDictEqual (
45+ {"t1" : 10 , "t2" : None , "t3" : None , "t4" : False , "t5" : None }, r ("--t1" , "10" , "--t4" , "False" )
46+ )
47+ self .assertDictEqual (
48+ {"t1" : 10 , "t2" : None , "t3" : None , "t4" : True , "t5" : 100 }, r ("--t1" , "10" , "--t5" , "100" , "--t4" , "True" )
49+ )
50+ self .assertDictEqual ({"t1" : 10 , "t2" : None , "t3" : None , "t4" : 44 , "t5" : None }, r ("--t1" , "10" , "--t4" , "44" ))
4351
4452
53+ # NOTE this should work too, Literals now canot be constructed
54+ # self.assertDictEqual(
55+ # {"t1": 10, "t2": None, "t3": None, "t4": False, "t5": "foo"}, r("--t1", "10", "--t4", "False", "--t5", "foo")
56+ # )
57+
58+ with self .assertStderr (contains = "Error parsing --t5" ), self .assertRaises (SystemExit ):
59+ # NOTE this might rather raise a wrong field dialog
60+ r ("--t1" , "10" , "--t5" , "invalid" )
61+
4562 with (
46- self .assertOutputs (contains = ["--t1 [int] (required)" ,
47- "--t2 [int] (default: 'None / or if left blank: True') " ,
48- "--t3 [int] (default: 'None / or if left blank: 3')" ,
49- "--t4 [int|bool] (default: '2 / or if left blank: 3')"
50- ]),
51- self .assertRaises (SystemExit ),
52- ):
63+ self .assertOutputs (
64+ contains = [
65+ "--t1 [int] (required)" ,
66+ "--t2 [int] (default: 'None / or if left blank: True') " ,
67+ "--t3 [int] (default: 'None / or if left blank: 3')" ,
68+ "--t4 [int] (default: '2 / or if left blank: 3')" ,
69+ "--t5 [int|Literal] (default: 'None / or if left blank: True')" ,
70+ ]
71+ ),
72+ # NOTE t5 should display the mere Literal value
73+ self .assertRaises (SystemExit ),
74+ ):
5375 runm (Env , args = ["--help" ])
0 commit comments