From 15aa272ad2a36eff9891491480b58052c8be9f97 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Thu, 12 Sep 2024 15:26:27 +0800 Subject: [PATCH 1/5] appy flake8-pytest-style rules --- api/pyproject.toml | 3 +++ .../model_runtime/xinference/test_llm.py | 12 ++++++------ .../utils/test_module_import_helper.py | 6 ++++-- .../vdb/opensearch/test_opensearch.py | 2 +- api/tests/unit_tests/conftest.py | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/api/pyproject.toml b/api/pyproject.toml index 7d64d07678b37b..92dacf7d2ef4c8 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -23,6 +23,7 @@ select = [ "RUF101", # redirected-noqa "S506", # unsafe-yaml-load "SIM", # flake8-simplify rules + "PT", "UP", # pyupgrade rules "W191", # tab-indentation "W605", # invalid-escape-sequence @@ -59,6 +60,8 @@ ignore = [ "SIM117", # multiple-with-statements "SIM210", # if-expr-with-true-false "SIM300", # yoda-conditions + "PT011", + "PT017", ] [tool.ruff.lint.per-file-ignores] diff --git a/api/tests/integration_tests/model_runtime/xinference/test_llm.py b/api/tests/integration_tests/model_runtime/xinference/test_llm.py index 7db59fddeff783..fb5e03855d2120 100644 --- a/api/tests/integration_tests/model_runtime/xinference/test_llm.py +++ b/api/tests/integration_tests/model_runtime/xinference/test_llm.py @@ -20,7 +20,7 @@ from tests.integration_tests.model_runtime.__mock.xinference import setup_xinference_mock -@pytest.mark.parametrize("setup_openai_mock, setup_xinference_mock", [["chat", "none"]], indirect=True) +@pytest.mark.parametrize(("setup_openai_mock", "setup_xinference_mock"), [("chat", "none")], indirect=True) def test_validate_credentials_for_chat_model(setup_openai_mock, setup_xinference_mock): model = XinferenceAILargeLanguageModel() @@ -45,7 +45,7 @@ def test_validate_credentials_for_chat_model(setup_openai_mock, setup_xinference ) -@pytest.mark.parametrize("setup_openai_mock, setup_xinference_mock", [["chat", "none"]], indirect=True) +@pytest.mark.parametrize(("setup_openai_mock", "setup_xinference_mock"), [("chat", "none")], indirect=True) def test_invoke_chat_model(setup_openai_mock, setup_xinference_mock): model = XinferenceAILargeLanguageModel() @@ -75,7 +75,7 @@ def test_invoke_chat_model(setup_openai_mock, setup_xinference_mock): assert response.usage.total_tokens > 0 -@pytest.mark.parametrize("setup_openai_mock, setup_xinference_mock", [["chat", "none"]], indirect=True) +@pytest.mark.parametrize(("setup_openai_mock", "setup_xinference_mock"), [("chat", "none")], indirect=True) def test_invoke_stream_chat_model(setup_openai_mock, setup_xinference_mock): model = XinferenceAILargeLanguageModel() @@ -236,7 +236,7 @@ def test_invoke_stream_chat_model(setup_openai_mock, setup_xinference_mock): # assert response.message.tool_calls[0].function.name == 'get_current_weather' -@pytest.mark.parametrize("setup_openai_mock, setup_xinference_mock", [["completion", "none"]], indirect=True) +@pytest.mark.parametrize(("setup_openai_mock", "setup_xinference_mock"), [("completion", "none")], indirect=True) def test_validate_credentials_for_generation_model(setup_openai_mock, setup_xinference_mock): model = XinferenceAILargeLanguageModel() @@ -261,7 +261,7 @@ def test_validate_credentials_for_generation_model(setup_openai_mock, setup_xinf ) -@pytest.mark.parametrize("setup_openai_mock, setup_xinference_mock", [["completion", "none"]], indirect=True) +@pytest.mark.parametrize(("setup_openai_mock", "setup_xinference_mock"), [("completion", "none")], indirect=True) def test_invoke_generation_model(setup_openai_mock, setup_xinference_mock): model = XinferenceAILargeLanguageModel() @@ -286,7 +286,7 @@ def test_invoke_generation_model(setup_openai_mock, setup_xinference_mock): assert response.usage.total_tokens > 0 -@pytest.mark.parametrize("setup_openai_mock, setup_xinference_mock", [["completion", "none"]], indirect=True) +@pytest.mark.parametrize(("setup_openai_mock", "setup_xinference_mock"), [("completion", "none")], indirect=True) def test_invoke_stream_generation_model(setup_openai_mock, setup_xinference_mock): model = XinferenceAILargeLanguageModel() diff --git a/api/tests/integration_tests/utils/test_module_import_helper.py b/api/tests/integration_tests/utils/test_module_import_helper.py index 7d32f5ae66f5df..50725415e4174e 100644 --- a/api/tests/integration_tests/utils/test_module_import_helper.py +++ b/api/tests/integration_tests/utils/test_module_import_helper.py @@ -9,7 +9,8 @@ def test_loading_subclass_from_source(): module = load_single_subclass_from_source( module_name="ChildClass", script_path=os.path.join(current_path, "child_class.py"), parent_type=ParentClass ) - assert module and module.__name__ == "ChildClass" + assert module + assert module.__name__ == "ChildClass" def test_load_import_module_from_source(): @@ -17,7 +18,8 @@ def test_load_import_module_from_source(): module = import_module_from_source( module_name="ChildClass", py_file_path=os.path.join(current_path, "child_class.py") ) - assert module and module.__name__ == "ChildClass" + assert module + assert module.__name__ == "ChildClass" def test_lazy_loading_subclass_from_source(): diff --git a/api/tests/integration_tests/vdb/opensearch/test_opensearch.py b/api/tests/integration_tests/vdb/opensearch/test_opensearch.py index a99b81d41eba78..2666ce2e1ebe56 100644 --- a/api/tests/integration_tests/vdb/opensearch/test_opensearch.py +++ b/api/tests/integration_tests/vdb/opensearch/test_opensearch.py @@ -34,7 +34,7 @@ def setup_method(self): self.vector._client = MagicMock() @pytest.mark.parametrize( - "search_response, expected_length, expected_doc_id", + ("search_response", "expected_length", "expected_doc_id"), [ ( { diff --git a/api/tests/unit_tests/conftest.py b/api/tests/unit_tests/conftest.py index ca3082953aef5c..621c995a4bd642 100644 --- a/api/tests/unit_tests/conftest.py +++ b/api/tests/unit_tests/conftest.py @@ -13,7 +13,7 @@ CACHED_APP.config.update({"TESTING": True}) -@pytest.fixture() +@pytest.fixture def app() -> Flask: return CACHED_APP From 6d131c9a22050d193383526b0c72346662fb1217 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Thu, 12 Sep 2024 16:21:57 +0800 Subject: [PATCH 2/5] + PT017 --- api/pyproject.toml | 1 - .../unit_tests/core/helper/test_ssrf_proxy.py | 8 ++++---- api/tests/unit_tests/libs/test_email.py | 14 +++++--------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/api/pyproject.toml b/api/pyproject.toml index 92dacf7d2ef4c8..426f450e843e7a 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -61,7 +61,6 @@ ignore = [ "SIM210", # if-expr-with-true-false "SIM300", # yoda-conditions "PT011", - "PT017", ] [tool.ruff.lint.per-file-ignores] diff --git a/api/tests/unit_tests/core/helper/test_ssrf_proxy.py b/api/tests/unit_tests/core/helper/test_ssrf_proxy.py index 7a0bc70c63eeab..d6e6b0b79ca813 100644 --- a/api/tests/unit_tests/core/helper/test_ssrf_proxy.py +++ b/api/tests/unit_tests/core/helper/test_ssrf_proxy.py @@ -1,6 +1,8 @@ import random from unittest.mock import MagicMock, patch +import pytest + from core.helper.ssrf_proxy import SSRF_DEFAULT_MAX_RETRIES, STATUS_FORCELIST, make_request @@ -22,11 +24,9 @@ def test_retry_exceed_max_retries(mock_request): side_effects = [mock_response] * SSRF_DEFAULT_MAX_RETRIES mock_request.side_effect = side_effects - try: + with pytest.raises(Exception) as e: make_request("GET", "http://example.com", max_retries=SSRF_DEFAULT_MAX_RETRIES - 1) - raise AssertionError("Expected Exception not raised") - except Exception as e: - assert str(e) == f"Reached maximum retries ({SSRF_DEFAULT_MAX_RETRIES - 1}) for URL http://example.com" + assert str(e.value) == f"Reached maximum retries ({SSRF_DEFAULT_MAX_RETRIES - 1}) for URL http://example.com" @patch("httpx.request") diff --git a/api/tests/unit_tests/libs/test_email.py b/api/tests/unit_tests/libs/test_email.py index f8234f3f3be3c3..ae0177791b5986 100644 --- a/api/tests/unit_tests/libs/test_email.py +++ b/api/tests/unit_tests/libs/test_email.py @@ -1,3 +1,5 @@ +import pytest + from libs.helper import email @@ -9,17 +11,11 @@ def test_email_with_valid_email(): def test_email_with_invalid_email(): - try: + with pytest.raises(ValueError, match="invalid_email is not a valid email."): email("invalid_email") - except ValueError as e: - assert str(e) == "invalid_email is not a valid email." - try: + with pytest.raises(ValueError, match="@example.com is not a valid email."): email("@example.com") - except ValueError as e: - assert str(e) == "@example.com is not a valid email." - try: + with pytest.raises(ValueError, match="()@example.com is not a valid email."): email("()@example.com") - except ValueError as e: - assert str(e) == "()@example.com is not a valid email." From 3e06a875828678fa6c6f7f7bde6fc1a2942e7afa Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Thu, 12 Sep 2024 16:23:27 +0800 Subject: [PATCH 3/5] - PT011 --- api/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/pyproject.toml b/api/pyproject.toml index 426f450e843e7a..f590624ab79def 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -51,6 +51,7 @@ ignore = [ "B905", # zip-without-explicit-strict "N806", # non-lowercase-variable-in-function "N815", # mixed-case-variable-in-class-scope + "PT011", # pytest-raises-too-broad "SIM102", # collapsible-if "SIM103", # needless-bool "SIM105", # suppressible-exception @@ -60,7 +61,6 @@ ignore = [ "SIM117", # multiple-with-statements "SIM210", # if-expr-with-true-false "SIM300", # yoda-conditions - "PT011", ] [tool.ruff.lint.per-file-ignores] From 9aaa1e0353aae9d5a8355407b3cff47d25c84ab8 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Thu, 12 Sep 2024 16:26:47 +0800 Subject: [PATCH 4/5] comment --- api/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/pyproject.toml b/api/pyproject.toml index f590624ab79def..10a7d8f0c36244 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -23,7 +23,7 @@ select = [ "RUF101", # redirected-noqa "S506", # unsafe-yaml-load "SIM", # flake8-simplify rules - "PT", + "PT", # flake8-pytest-style rules "UP", # pyupgrade rules "W191", # tab-indentation "W605", # invalid-escape-sequence From 8a8e6506de44376aac7a06b49e70b06b09613b6a Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Thu, 12 Sep 2024 16:26:58 +0800 Subject: [PATCH 5/5] comment --- api/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/pyproject.toml b/api/pyproject.toml index 10a7d8f0c36244..1822f594199847 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -18,12 +18,12 @@ select = [ "FURB", # refurb rules "I", # isort rules "N", # pep8-naming + "PT", # flake8-pytest-style rules "RUF019", # unnecessary-key-check "RUF100", # unused-noqa "RUF101", # redirected-noqa "S506", # unsafe-yaml-load "SIM", # flake8-simplify rules - "PT", # flake8-pytest-style rules "UP", # pyupgrade rules "W191", # tab-indentation "W605", # invalid-escape-sequence