From 48fb846a4d63da5135163968df06854a7f5d81b3 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Aug 2024 16:45:19 -0300 Subject: [PATCH] refactor: change typing annotations in tests folder (#3594) * refactor(log_router.py): change variable type annotation from List to list for better consistency refactor(utils.py): change variable type annotation from Dict to dict for better consistency refactor(base.py): change variable type annotation from Optional to Union for better clarity refactor(callback.py): change variable type annotation from Dict to dict for better consistency refactor(chat.py): change variable type annotation from Optional to Union for better clarity refactor(endpoints.py): change variable type annotation from Optional to Union for better clarity refactor(flows.py): change variable type annotation from List to list for better consistency refactor(api): update response_model annotations to use lowercase list for consistency and improve readability refactor(store.py): update type annotations for query parameters in get_components endpoint to improve code readability and maintainability feat(store.py): add support for type hinting Union and list types in query parameters for better data validation and documentation * run make format * refactor(input_mixin.py): update typing annotations for variables to use union types for better clarity and compatibility with Python 3.10 refactor(inputs.py): update typing annotations for variables to use union types and import necessary modules for compatibility with Python 3.10 * refactor(base.py): remove unnecessary imports and update typing for fields in Input and Output classes feat(base.py): add support for specifying field types more explicitly in Input and Output classes feat(frontend_node/base.py): enhance typing and field definitions in FrontendNode class feat(frontend_node/custom_components.py): improve typing and field definitions in CustomComponentFrontendNode and ComponentFrontendNode classes feat(template/base.py): update typing for fields in Template class and remove unnecessary imports * refactor(inputs): remove unnecessary Optional import from typing in input_mixin.py and inputs.py files to improve code readability and maintainability * refactor(schema.py): change 'Type' to 'type' for consistency in type annotations refactor(schema.py): change 'list' to 'List' and 'Literal' to 'literal' for correct type hinting in create_input_schema function * refactor(utils.py): change typing annotations from List and Union to list and type to follow PEP 585 standards refactor(test_schema.py): change typing annotations from List and List to list and list to follow PEP 585 standards refactor(test_graph.py): change typing annotations from Type and Union to type and Vertex | None to follow PEP 585 standards refactor(test_io_schema.py): change typing annotations from List and List to list and list to follow PEP 585 standards refactor(test_custom_component.py): update file reading method to remove unnecessary "r" mode refactor(test_helper_components.py): update file reading method to remove unnecessary "r" mode refactor(test_kubernetes_secrets.py): update b64encode method argument to bytes type refactor(test_template.py): change typing annotations from Optional, List, and Dict to list, dict, and None to follow PEP 585 standards * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/backend/tests/integration/utils.py | 5 ++--- src/backend/tests/unit/graph/test_graph.py | 3 +-- src/backend/tests/unit/test_custom_component.py | 2 +- src/backend/tests/unit/test_helper_components.py | 2 +- src/backend/tests/unit/test_kubernetes_secrets.py | 4 ++-- src/backend/tests/unit/test_template.py | 5 ++--- 6 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/backend/tests/integration/utils.py b/src/backend/tests/integration/utils.py index 1389cd082..1e79ee1db 100644 --- a/src/backend/tests/integration/utils.py +++ b/src/backend/tests/integration/utils.py @@ -1,5 +1,4 @@ import os -from typing import List from astrapy.admin import parse_api_endpoint from langflow.field_typing import Embeddings @@ -43,10 +42,10 @@ class MockEmbeddings(Embeddings): def mock_embedding(text: str): return [len(text) / 2, len(text) / 5, len(text) / 10] - def embed_documents(self, texts: List[str]) -> List[List[float]]: + def embed_documents(self, texts: list[str]) -> list[list[float]]: self.embedded_documents = texts return [self.mock_embedding(text) for text in texts] - def embed_query(self, text: str) -> List[float]: + def embed_query(self, text: str) -> list[float]: self.embedded_query = text return self.mock_embedding(text) diff --git a/src/backend/tests/unit/graph/test_graph.py b/src/backend/tests/unit/graph/test_graph.py index f6b85aa4e..3d26d0744 100644 --- a/src/backend/tests/unit/graph/test_graph.py +++ b/src/backend/tests/unit/graph/test_graph.py @@ -1,7 +1,6 @@ import copy import json import pickle -from typing import Type, Union import pytest @@ -62,7 +61,7 @@ def sample_nodes(): ] -def get_node_by_type(graph, node_type: Type[Vertex]) -> Union[Vertex, None]: +def get_node_by_type(graph, node_type: type[Vertex]) -> Vertex | None: """Get a node by type""" return next((node for node in graph.vertices if isinstance(node, node_type)), None) diff --git a/src/backend/tests/unit/test_custom_component.py b/src/backend/tests/unit/test_custom_component.py index 5d91cc9dc..29030c9c5 100644 --- a/src/backend/tests/unit/test_custom_component.py +++ b/src/backend/tests/unit/test_custom_component.py @@ -19,7 +19,7 @@ def client(): @pytest.fixture def code_component_with_multiple_outputs(): - with open("src/backend/tests/data/component_multiple_outputs.py", "r") as f: + with open("src/backend/tests/data/component_multiple_outputs.py") as f: code = f.read() return Component(_code=code) diff --git a/src/backend/tests/unit/test_helper_components.py b/src/backend/tests/unit/test_helper_components.py index fc343fbaa..83e049d8a 100644 --- a/src/backend/tests/unit/test_helper_components.py +++ b/src/backend/tests/unit/test_helper_components.py @@ -39,7 +39,7 @@ def client(): def test_uuid_generator_component(): # Arrange uuid_generator_component = helpers.IDGeneratorComponent() - uuid_generator_component._code = open(helpers.IDGenerator.__file__, "r").read() + uuid_generator_component._code = open(helpers.IDGenerator.__file__).read() frontend_node, _ = build_custom_component_template(uuid_generator_component) diff --git a/src/backend/tests/unit/test_kubernetes_secrets.py b/src/backend/tests/unit/test_kubernetes_secrets.py index 9de359535..1e48272cf 100644 --- a/src/backend/tests/unit/test_kubernetes_secrets.py +++ b/src/backend/tests/unit/test_kubernetes_secrets.py @@ -33,13 +33,13 @@ def test_create_secret(secret_manager, mocker): kind="Secret", metadata=V1ObjectMeta(name="test-secret"), type="Opaque", - data={"key": b64encode("value".encode()).decode()}, + data={"key": b64encode(b"value").decode()}, ), ) def test_get_secret(secret_manager, mocker): - mock_secret = V1Secret(data={"key": b64encode("value".encode()).decode()}) + mock_secret = V1Secret(data={"key": b64encode(b"value").decode()}) mocker.patch.object(secret_manager.core_api, "read_namespaced_secret", return_value=mock_secret) secret_data = secret_manager.get_secret(name="test-secret") diff --git a/src/backend/tests/unit/test_template.py b/src/backend/tests/unit/test_template.py index c1282b578..b9b38f2fc 100644 --- a/src/backend/tests/unit/test_template.py +++ b/src/backend/tests/unit/test_template.py @@ -1,5 +1,4 @@ import importlib -from typing import Dict, List, Optional import pytest from langflow.utils.util import build_template_from_function, get_base_classes, get_default_factory @@ -27,14 +26,14 @@ class Child(Parent): class ExampleClass1(BaseModel): """Example class 1.""" - def __init__(self, data: Optional[List[int]] = None): + def __init__(self, data: list[int] | None = None): self.data = data or [1, 2, 3] class ExampleClass2(BaseModel): """Example class 2.""" - def __init__(self, data: Optional[Dict[str, int]] = None): + def __init__(self, data: dict[str, int] | None = None): self.data = data or {"a": 1, "b": 2, "c": 3}