From 0edc7fad89908f6c911333cb4607cfab068226f5 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Tue, 5 Mar 2024 17:27:26 +0100 Subject: [PATCH 1/2] Notification: fix `choices` rendering for `INVALID_CHOICE` While taking a look at #11189 I found that the rendering was broken. I found the `with` + `yield` pattern pretty complex (I have a note about this in the code) and it seems that complexity is confusing enough to make this hard to dealt with. Anyways, I fixed the problem for now and I added a test case. We can come back to refactoring this in the future with more time. --- readthedocs/config/config.py | 17 +++++++++++++---- readthedocs/config/tests/test_config.py | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/readthedocs/config/config.py b/readthedocs/config/config.py index bc672e0fd46..4b3ffbf92fd 100644 --- a/readthedocs/config/config.py +++ b/readthedocs/config/config.py @@ -112,13 +112,22 @@ def catch_validation_error(self, key): try: yield except ConfigValidationError as error: - raise ConfigError( - message_id=error.message_id, - format_values={ + # Expand the format values defined when the exception is risen + # with extra ones we have here + format_values = ( + error.format_values if hasattr(error, "format_values") else {} + ) + format_values.update( + { "key": key, "value": error.format_values.get("value"), "source_file": os.path.relpath(self.source_file, self.base_path), - }, + } + ) + + raise ConfigError( + message_id=error.message_id, + format_values=format_values, ) from error def pop(self, name, container, default, raise_ex): diff --git a/readthedocs/config/tests/test_config.py b/readthedocs/config/tests/test_config.py index 021a56b5f7f..c72e2c256b6 100644 --- a/readthedocs/config/tests/test_config.py +++ b/readthedocs/config/tests/test_config.py @@ -424,6 +424,9 @@ def test_new_build_config_invalid_tools_version(self): build.validate() assert excinfo.value.message_id == ConfigValidationError.INVALID_CHOICE assert excinfo.value.format_values.get("key") == "build.tools.python" + assert excinfo.value.format_values.get("choices") == ", ".join( + settings.RTD_DOCKER_BUILD_SETTINGS["tools"]["python"].keys() + ) def test_new_build_config(self): build = get_build_config( From f8ca8e3ffe8fa6913936e53a964c25d43de7d12d Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Tue, 5 Mar 2024 21:40:18 +0100 Subject: [PATCH 2/2] Update readthedocs/config/config.py Co-authored-by: Santos Gallegos --- readthedocs/config/config.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/readthedocs/config/config.py b/readthedocs/config/config.py index 4b3ffbf92fd..2452621e647 100644 --- a/readthedocs/config/config.py +++ b/readthedocs/config/config.py @@ -114,9 +114,7 @@ def catch_validation_error(self, key): except ConfigValidationError as error: # Expand the format values defined when the exception is risen # with extra ones we have here - format_values = ( - error.format_values if hasattr(error, "format_values") else {} - ) + format_values = getattr(error, "format_values", {}) format_values.update( { "key": key,