From 577b43c4e8f0c08f9d9b76819847a847f04909ca Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Tue, 31 Jan 2023 19:35:10 -0800 Subject: [PATCH] Fix regression with settings with a '-' --- settngs.py | 2 +- testing/settngs.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/settngs.py b/settngs.py index 4f4243d..0d2c3b1 100644 --- a/settngs.py +++ b/settngs.py @@ -201,7 +201,7 @@ if TYPE_CHECKING: def sanitize_name(name: str) -> str: - return re.sub('[' + re.escape(' -_,.!@#$%^&*(){}[]\',."<>;:') + ']+', '-', name).strip('-') + return re.sub('[' + re.escape(' -_,.!@#$%^&*(){}[]\',."<>;:') + ']+', '_', name).strip('_') def get_option(options: Values | Namespace, setting: Setting) -> tuple[Any, bool]: diff --git a/testing/settngs.py b/testing/settngs.py index 3327309..5f56fa9 100644 --- a/testing/settngs.py +++ b/testing/settngs.py @@ -64,6 +64,44 @@ example: list[tuple[list[str], str, str]] = [ ), ] success = [ + ( + ( + ('--test-setting',), + dict( + group='tst', + ), + ), # Equivalent to Setting("--test-setting", group="tst") + { + 'action': None, + 'choices': None, + 'cmdline': True, + 'const': None, + 'default': None, + 'dest': 'test_setting', # dest is calculated by Setting and is not used by argparse + 'exclusive': False, + 'file': True, + 'group': 'tst', + 'help': None, + 'internal_name': 'tst_test_setting', # Should almost always be "{group}_{dest}" + 'metavar': 'TEST_SETTING', # Set manually so argparse doesn't use TST_TEST + 'nargs': None, + 'required': None, + 'type': None, + 'argparse_args': ('--test-setting',), # *args actually sent to argparse + 'argparse_kwargs': { + 'action': None, + 'choices': None, + 'const': None, + 'default': None, + 'dest': 'tst_test_setting', + 'help': None, + 'metavar': 'TEST_SETTING', + 'nargs': None, + 'required': None, + 'type': None, + }, # Non-None **kwargs sent to argparse + }, + ), ( ( ('--test',), @@ -71,7 +109,7 @@ success = [ group='tst', dest='testing', ), - ), # Equivalent to Setting("--test", group="tst") + ), # Equivalent to Setting("--test", group="tst", dest="testing") { 'action': None, 'choices': None,