@@ -2116,9 +2116,11 @@ class Settings(BaseSettings):
2116
2116
def test_env_parse_enums (env ):
2117
2117
class Settings (BaseSettings ):
2118
2118
fruit : FruitsEnum
2119
+ union_fruit : Optional [Union [int , FruitsEnum ]] = None
2119
2120
2120
2121
with pytest .raises (ValidationError ) as exc_info :
2121
2122
env .set ('FRUIT' , 'kiwi' )
2123
+ env .set ('UNION_FRUIT' , 'kiwi' )
2122
2124
s = Settings ()
2123
2125
assert exc_info .value .errors (include_url = False ) == [
2124
2126
{
@@ -2127,18 +2129,42 @@ class Settings(BaseSettings):
2127
2129
'msg' : 'Input should be 0, 1 or 2' ,
2128
2130
'input' : 'kiwi' ,
2129
2131
'ctx' : {'expected' : '0, 1 or 2' },
2130
- }
2132
+ },
2133
+ {
2134
+ 'input' : 'kiwi' ,
2135
+ 'loc' : (
2136
+ 'union_fruit' ,
2137
+ 'int' ,
2138
+ ),
2139
+ 'msg' : 'Input should be a valid integer, unable to parse string as an integer' ,
2140
+ 'type' : 'int_parsing' ,
2141
+ },
2142
+ {
2143
+ 'ctx' : {
2144
+ 'expected' : '0, 1 or 2' ,
2145
+ },
2146
+ 'input' : 'kiwi' ,
2147
+ 'loc' : (
2148
+ 'union_fruit' ,
2149
+ 'int-enum[FruitsEnum]' ,
2150
+ ),
2151
+ 'msg' : 'Input should be 0, 1 or 2' ,
2152
+ 'type' : 'enum' ,
2153
+ },
2131
2154
]
2132
2155
2133
2156
env .set ('FRUIT' , str (FruitsEnum .lime .value ))
2157
+ env .set ('UNION_FRUIT' , str (FruitsEnum .lime .value ))
2134
2158
s = Settings ()
2135
2159
assert s .fruit == FruitsEnum .lime
2136
2160
2137
2161
env .set ('FRUIT' , 'kiwi' )
2162
+ env .set ('UNION_FRUIT' , 'kiwi' )
2138
2163
s = Settings (_env_parse_enums = True )
2139
2164
assert s .fruit == FruitsEnum .kiwi
2140
2165
2141
2166
env .set ('FRUIT' , str (FruitsEnum .lime .value ))
2167
+ env .set ('UNION_FRUIT' , str (FruitsEnum .lime .value ))
2142
2168
s = Settings (_env_parse_enums = True )
2143
2169
assert s .fruit == FruitsEnum .lime
2144
2170
@@ -3129,17 +3155,18 @@ class Cfg(BaseSettings):
3129
3155
assert cfg .model_dump () == {'child' : {'name' : 'new name a' , 'diff_a' : 'new diff a' }}
3130
3156
3131
3157
3132
- def test_cli_enums ():
3158
+ def test_cli_enums (capsys , monkeypatch ):
3133
3159
class Pet (IntEnum ):
3134
3160
dog = 0
3135
3161
cat = 1
3136
3162
bird = 2
3137
3163
3138
3164
class Cfg (BaseSettings ):
3139
- pet : Pet
3165
+ pet : Pet = Pet .dog
3166
+ union_pet : Union [Pet , int ] = 43
3140
3167
3141
- cfg = Cfg (_cli_parse_args = ['--pet' , 'cat' ])
3142
- assert cfg .model_dump () == {'pet' : Pet .cat }
3168
+ cfg = Cfg (_cli_parse_args = ['--pet' , 'cat' , '--union_pet' , 'dog' ])
3169
+ assert cfg .model_dump () == {'pet' : Pet .cat , 'union_pet' : Pet . dog }
3143
3170
3144
3171
with pytest .raises (ValidationError ) as exc_info :
3145
3172
Cfg (_cli_parse_args = ['--pet' , 'rock' ])
@@ -3153,6 +3180,24 @@ class Cfg(BaseSettings):
3153
3180
}
3154
3181
]
3155
3182
3183
+ with monkeypatch .context () as m :
3184
+ m .setattr (sys , 'argv' , ['example.py' , '--help' ])
3185
+
3186
+ with pytest .raises (SystemExit ):
3187
+ Cfg (_cli_parse_args = True )
3188
+ assert (
3189
+ capsys .readouterr ().out
3190
+ == f"""usage: example.py [-h] [--pet {{dog,cat,bird}}]
3191
+ [--union_pet {{{{dog,cat,bird}},int}}]
3192
+
3193
+ { ARGPARSE_OPTIONS_TEXT } :
3194
+ -h, --help show this help message and exit
3195
+ --pet {{dog,cat,bird}} (default: dog)
3196
+ --union_pet {{{{dog,cat,bird}},int}}
3197
+ (default: 43)
3198
+ """
3199
+ )
3200
+
3156
3201
3157
3202
def test_cli_literals ():
3158
3203
class Cfg (BaseSettings ):
0 commit comments