From 124432a67a3e2d32196bf24efa75f4a5fae88b13 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Thu, 3 Jul 2025 13:29:03 +0200 Subject: [PATCH 1/3] Adds code clean up --- configargparse.py | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/configargparse.py b/configargparse.py index fc44aa1a..4e1d783b 100644 --- a/configargparse.py +++ b/configargparse.py @@ -1104,23 +1104,6 @@ def parse_known_args( self.write_config_file(namespace, output_file_paths, exit_after=True) return namespace, unknown_args - def get_source_to_settings_dict(self): - """ - If called after `parse_args()` or `parse_known_args()`, returns a dict that contains up to 4 keys corresponding - to where a given option's value is coming from: - - "command_line" - - "environment_variables" - - "config_file" - - "defaults" - Each such key, will be mapped to another dictionary containing the options set via that method. Here the key - will be the option name, and the value will be a 2-tuple of the form (`argparse.Action` obj, `str` value). - - Returns: - dict[str, dict[str, tuple[argparse.Action, str]]]: source to settings dict - """ - # _source_to_settings is set in parse_know_args(). - return self._source_to_settings # type:ignore[attribute-error] - def write_config_file(self, parsed_namespace, output_file_paths, exit_after=False): """Write the given settings to output files. @@ -1378,7 +1361,7 @@ def _open_config_files(self, command_line_args): # Otherwise it sys.exits(..) if, for example, config file # is_required=True and user doesn't provide it. def error_method(self, message): - pass + del message arg_parser.error = types.MethodType(error_method, arg_parser) @@ -1398,7 +1381,7 @@ def error_method(self, message): stream = self._config_file_open_func(user_config_file) except Exception as e: if len(e.args) == 2: # OSError - errno, msg = e.args + _, msg = e.args else: msg = str(e) # close previously opened config files From 988be54093fb7a9008423edcee02b4fbb2b73400 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Thu, 3 Jul 2025 13:30:25 +0200 Subject: [PATCH 2/3] Slightly cleaner --- configargparse.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/configargparse.py b/configargparse.py index 4e1d783b..5c637b56 100644 --- a/configargparse.py +++ b/configargparse.py @@ -1381,7 +1381,8 @@ def error_method(self, message): stream = self._config_file_open_func(user_config_file) except Exception as e: if len(e.args) == 2: # OSError - _, msg = e.args + errno, msg = e.args + del errno else: msg = str(e) # close previously opened config files @@ -1422,6 +1423,7 @@ def format_values(self): source = source_key_to_display_value_map[source[0]] % tuple(source[1:]) r.write(source) for key, (action, value) in settings.items(): + del action if key: r.write(" {:<19}{}\n".format(key + ":", value)) else: From 4a3ed41baae8ea5a5db6715b9d526e3c50a176dd Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Fri, 1 Aug 2025 10:25:11 -0400 Subject: [PATCH 3/3] wip --- configargparse.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/configargparse.py b/configargparse.py index f900d579..3a3b3bf6 100644 --- a/configargparse.py +++ b/configargparse.py @@ -1097,6 +1097,23 @@ def parse_known_args( self.write_config_file(namespace, output_file_paths, exit_after=True) return namespace, unknown_args + def get_source_to_settings_dict(self): + """ + If called after `parse_args()` or `parse_known_args()`, returns a dict that contains up to 4 keys corresponding + to where a given option's value is coming from: + - "command_line" + - "environment_variables" + - "config_file" + - "defaults" + Each such key, will be mapped to another dictionary containing the options set via that method. Here the key + will be the option name, and the value will be a 2-tuple of the form (`argparse.Action` obj, `str` value). + + Returns: + dict[str, dict[str, tuple[argparse.Action, str]]]: source to settings dict + """ + # _source_to_settings is set in parse_known_args(). + return self._source_to_settings # type:ignore[attribute-error] + def write_config_file(self, parsed_namespace, output_file_paths, exit_after=False): """Write the given settings to output files. @@ -1354,7 +1371,8 @@ def _open_config_files(self, command_line_args): # Otherwise it sys.exits(..) if, for example, config file # is_required=True and user doesn't provide it. def error_method(self, message): - del message + # This line doesn't do anything, and is only here to satisfy linters. + del message # See discussion in PR #330 arg_parser.error = types.MethodType(error_method, arg_parser) @@ -1374,8 +1392,7 @@ def error_method(self, message): stream = self._config_file_open_func(user_config_file) except Exception as e: if len(e.args) == 2: # OSError - errno, msg = e.args - del errno + _, msg = e.args else: msg = str(e) # close previously opened config files @@ -1415,8 +1432,7 @@ def format_values(self): source = source.split("|") source = source_key_to_display_value_map[source[0]] % tuple(source[1:]) r.write(source) - for key, (action, value) in settings.items(): - del action + for key, (_, value) in settings.items(): if key: r.write(" {:<19}{}\n".format(key + ":", value)) else: