From ca6e9e5e3476a7cd1a1a2b727b4dacc6129cc884 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 30 Nov 2023 17:20:39 +0100 Subject: [PATCH] lint: Address linting issues from #5458 - Fixed linting issues in `json_utils/utilities.py` and `test_json_utils.py` --- autogpts/autogpt/autogpt/json_utils/utilities.py | 6 +++--- autogpts/autogpt/tests/unit/test_utils.py | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py index 028eb67c..6f968397 100644 --- a/autogpts/autogpt/autogpt/json_utils/utilities.py +++ b/autogpts/autogpt/autogpt/json_utils/utilities.py @@ -1,7 +1,7 @@ """Utilities for the json_fixes package.""" -import re import ast import logging +import re from typing import Any logger = logging.getLogger(__name__) @@ -9,7 +9,7 @@ logger = logging.getLogger(__name__) def extract_dict_from_response(response_content: str) -> dict[str, Any]: # Sometimes the response includes the JSON in a code block with ``` - pattern = r'```([\s\S]*?)```' + pattern = r"```([\s\S]*?)```" match = re.search(pattern, response_content) if match: @@ -18,7 +18,7 @@ def extract_dict_from_response(response_content: str) -> dict[str, Any]: response_content = response_content.lstrip("json") else: # The string may contain JSON. - json_pattern = r'{.*}' + json_pattern = r"{.*}" match = re.search(json_pattern, response_content) if match: diff --git a/autogpts/autogpt/tests/unit/test_utils.py b/autogpts/autogpt/tests/unit/test_utils.py index f56e2b97..9a783ce4 100644 --- a/autogpts/autogpt/tests/unit/test_utils.py +++ b/autogpts/autogpt/tests/unit/test_utils.py @@ -194,14 +194,18 @@ def test_extract_json_from_response_wrapped_in_code_block(valid_json_response: d extract_dict_from_response(emulated_response_from_openai) == valid_json_response ) -def test_extract_json_from_response_wrapped_in_code_block_with_language(valid_json_response: dict): + +def test_extract_json_from_response_wrapped_in_code_block_with_language( + valid_json_response: dict, +): emulated_response_from_openai = "```json" + str(valid_json_response) + "```" assert ( extract_dict_from_response(emulated_response_from_openai) == valid_json_response ) + def test_extract_json_from_response_json_contained_in_string(valid_json_response: dict): emulated_response_from_openai = "sentence1" + str(valid_json_response) + "sentence2" assert ( extract_dict_from_response(emulated_response_from_openai) == valid_json_response - ) \ No newline at end of file + )