Skip to content

Commit 51de23b

Browse files
authored
Fix the bug where utilsd tries to add NoneType arguments to argparse (#10)
1 parent d7389bd commit 51de23b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

tests/test_config_cli.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Optional
2+
from typing import Optional, Union
33

44
from utilsd.config import PythonConfig, Registry, RegistryConfig, SubclassConfig, configclass
55
from unittest.mock import patch
@@ -38,9 +38,10 @@ class TEST(metaclass=Registry, name="test"):
3838

3939
@TEST.register_module()
4040
class Module1:
41-
def __init__(self, a: int = 1, b: Optional[str] = None):
41+
def __init__(self, a: int = 1, b: Optional[str] = None, c: Union[str, BaseFoo, None] = None):
4242
self.a = a
4343
self.b = b
44+
self.c = c
4445

4546

4647
@configclass
@@ -100,4 +101,5 @@ def test_registry_config_command_line():
100101

101102

102103
if __name__ == '__main__':
103-
test_cli_with_bool()
104+
# test_cli_with_bool()
105+
test_registry_config_command_line()

utilsd/config/cli_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ def build_parser(self, parser: ArgumentParser,
6060
if not isinstance(shortcut, list):
6161
raise TypeError(f'Shortcut of {name} is not found to be a list: {shortcut}')
6262

63-
if issubclass(type_, Enum):
63+
if type_ is type(None):
64+
# ignore None type when building parser
65+
pass
66+
elif issubclass(type_, Enum):
6467
parser.add_argument('--' + name, *shortcut, dest=name, type=str, metavar='STRING',
6568
default=SUPPRESS, choices=[e.value for e in type_])
6669
elif type_ in (int, str, float, bool, list, dict):

0 commit comments

Comments
 (0)