ref: Auto-fix ruff rules in tests (#4154)

This commit is contained in:
Christophe Bornet 2024-10-16 17:42:36 +02:00 • committed by GitHub
commit 45c8f98692
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 359 additions and 456 deletions

View file

@ -5,7 +5,6 @@ from textwrap import dedent
import pytest
from langchain_core.documents import Document
from langflow.custom import Component, CustomComponent
from langflow.custom.code_parser.code_parser import CodeParser, CodeSyntaxError
from langflow.custom.custom_component.base_component import BaseComponent, ComponentCodeNullError
@ -14,7 +13,7 @@ from langflow.custom.utils import build_custom_component_template
@pytest.fixture
def code_component_with_multiple_outputs():
code = Path("src/backend/tests/data/component_multiple_outputs.py").read_text()
code = Path("src/backend/tests/data/component_multiple_outputs.py").read_text(encoding="utf-8")
return Component(_code=code)
@ -39,25 +38,20 @@ class YourComponent(CustomComponent):
def test_code_parser_init():
"""
Test the initialization of the CodeParser class.
"""
"""Test the initialization of the CodeParser class."""
parser = CodeParser(code_default)
assert parser.code == code_default
def test_code_parser_get_tree():
"""
Test the __get_tree method of the CodeParser class.
"""
"""Test the __get_tree method of the CodeParser class."""
parser = CodeParser(code_default)
tree = parser.get_tree()
assert isinstance(tree, ast.AST)
def test_code_parser_syntax_error():
"""
Test the __get_tree method raises the
"""Test the __get_tree method raises the
CodeSyntaxError when given incorrect syntax.
"""
code_syntax_error = "zzz import os"
@ -68,26 +62,21 @@ def test_code_parser_syntax_error():
def test_component_init():
"""
Test the initialization of the Component class.
"""
"""Test the initialization of the Component class."""
component = BaseComponent(_code=code_default, _function_entrypoint_name="build")
assert component._code == code_default
assert component._function_entrypoint_name == "build"
def test_component_get_code_tree():
"""
Test the get_code_tree method of the Component class.
"""
"""Test the get_code_tree method of the Component class."""
component = BaseComponent(_code=code_default, _function_entrypoint_name="build")
tree = component.get_code_tree(component._code)
assert "imports" in tree
def test_component_code_null_error():
"""
Test the get_function method raises the
"""Test the get_function method raises the
ComponentCodeNullError when the code is empty.
"""
component = BaseComponent(_code="", _function_entrypoint_name="")
@ -96,9 +85,7 @@ def test_component_code_null_error():
def test_custom_component_init():
"""
Test the initialization of the CustomComponent class.
"""
"""Test the initialization of the CustomComponent class."""
function_entrypoint_name = "build"
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name=function_entrypoint_name)
@ -107,26 +94,21 @@ def test_custom_component_init():
def test_custom_component_build_template_config():
"""
Test the build_template_config property of the CustomComponent class.
"""
"""Test the build_template_config property of the CustomComponent class."""
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
config = custom_component.build_template_config()
assert isinstance(config, dict)
def test_custom_component_get_function():
"""
Test the get_function property of the CustomComponent class.
"""
"""Test the get_function property of the CustomComponent class."""
custom_component = CustomComponent(_code="def build(): pass", _function_entrypoint_name="build")
my_function = custom_component.get_function()
assert isinstance(my_function, types.FunctionType)
def test_code_parser_parse_imports_import():
"""
Test the parse_imports method of the CodeParser
"""Test the parse_imports method of the CodeParser
class with an import statement.
"""
parser = CodeParser(code_default)
@ -138,8 +120,7 @@ def test_code_parser_parse_imports_import():
def test_code_parser_parse_imports_importfrom():
"""
Test the parse_imports method of the CodeParser
"""Test the parse_imports method of the CodeParser
class with an import from statement.
"""
parser = CodeParser("from os import path")
@ -151,9 +132,7 @@ def test_code_parser_parse_imports_importfrom():
def test_code_parser_parse_functions():
"""
Test the parse_functions method of the CodeParser class.
"""
"""Test the parse_functions method of the CodeParser class."""
parser = CodeParser("def test(): pass")
tree = parser.get_tree()
for node in ast.walk(tree):
@ -164,9 +143,7 @@ def test_code_parser_parse_functions():
def test_code_parser_parse_classes():
"""
Test the parse_classes method of the CodeParser class.
"""
"""Test the parse_classes method of the CodeParser class."""
parser = CodeParser("from langflow.custom import Component\n\nclass Test(Component): pass")
tree = parser.get_tree()
for node in ast.walk(tree):
@ -177,9 +154,7 @@ def test_code_parser_parse_classes():
def test_code_parser_parse_classes_raises():
"""
Test the parse_classes method of the CodeParser class.
"""
"""Test the parse_classes method of the CodeParser class."""
parser = CodeParser("class Test: pass")
tree = parser.get_tree()
with pytest.raises(TypeError):
@ -189,9 +164,7 @@ def test_code_parser_parse_classes_raises():
def test_code_parser_parse_global_vars():
"""
Test the parse_global_vars method of the CodeParser class.
"""
"""Test the parse_global_vars method of the CodeParser class."""
parser = CodeParser("x = 1")
tree = parser.get_tree()
for node in ast.walk(tree):
@ -202,8 +175,7 @@ def test_code_parser_parse_global_vars():
def test_component_get_function_valid():
"""
Test the get_function method of the Component
"""Test the get_function method of the Component
class with valid code and function_entrypoint_name.
"""
component = BaseComponent(_code="def build(): pass", _function_entrypoint_name="build")
@ -212,8 +184,7 @@ def test_component_get_function_valid():
def test_custom_component_get_function_entrypoint_args():
"""
Test the get_function_entrypoint_args
"""Test the get_function_entrypoint_args
property of the CustomComponent class.
"""
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
@ -225,28 +196,23 @@ def test_custom_component_get_function_entrypoint_args():
def test_custom_component_get_function_entrypoint_return_type():
"""
Test the get_function_entrypoint_return_type
"""Test the get_function_entrypoint_return_type
property of the CustomComponent class.
"""
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
return_type = custom_component.get_function_entrypoint_return_type
assert return_type == [Document]
def test_custom_component_get_main_class_name():
"""
Test the get_main_class_name property of the CustomComponent class.
"""
"""Test the get_main_class_name property of the CustomComponent class."""
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
class_name = custom_component.get_main_class_name
assert class_name == "YourComponent"
def test_custom_component_get_function_valid():
"""
Test the get_function property of the CustomComponent
"""Test the get_function property of the CustomComponent
class with valid code and function_entrypoint_name.
"""
custom_component = CustomComponent(_code="def build(): pass", _function_entrypoint_name="build")
@ -255,9 +221,7 @@ def test_custom_component_get_function_valid():
def test_code_parser_parse_arg_no_annotation():
"""
Test the parse_arg method of the CodeParser class without an annotation.
"""
"""Test the parse_arg method of the CodeParser class without an annotation."""
parser = CodeParser("")
arg = ast.arg(arg="x", annotation=None)
result = parser.parse_arg(arg, None)
@ -266,9 +230,7 @@ def test_code_parser_parse_arg_no_annotation():
def test_code_parser_parse_arg_with_annotation():
"""
Test the parse_arg method of the CodeParser class with an annotation.
"""
"""Test the parse_arg method of the CodeParser class with an annotation."""
parser = CodeParser("")
arg = ast.arg(arg="x", annotation=ast.Name(id="int", ctx=ast.Load()))
result = parser.parse_arg(arg, None)
@ -277,8 +239,7 @@ def test_code_parser_parse_arg_with_annotation():
def test_code_parser_parse_callable_details_no_args():
"""
Test the parse_callable_details method of the
"""Test the parse_callable_details method of the
CodeParser class with a function with no arguments.
"""
parser = CodeParser("")
@ -295,9 +256,7 @@ def test_code_parser_parse_callable_details_no_args():
def test_code_parser_parse_assign():
"""
Test the parse_assign method of the CodeParser class.
"""
"""Test the parse_assign method of the CodeParser class."""
parser = CodeParser("")
stmt = ast.Assign(targets=[ast.Name(id="x", ctx=ast.Store())], value=ast.Num(n=1))
result = parser.parse_assign(stmt)
@ -306,9 +265,7 @@ def test_code_parser_parse_assign():
def test_code_parser_parse_ann_assign():
"""
Test the parse_ann_assign method of the CodeParser class.
"""
"""Test the parse_ann_assign method of the CodeParser class."""
parser = CodeParser("")
stmt = ast.AnnAssign(
target=ast.Name(id="x", ctx=ast.Store()),
@ -323,8 +280,7 @@ def test_code_parser_parse_ann_assign():
def test_code_parser_parse_function_def_not_init():
"""
Test the parse_function_def method of the
"""Test the parse_function_def method of the
CodeParser class with a function that is not __init__.
"""
parser = CodeParser("")
@ -341,8 +297,7 @@ def test_code_parser_parse_function_def_not_init():
def test_code_parser_parse_function_def_init():
"""
Test the parse_function_def method of the
"""Test the parse_function_def method of the
CodeParser class with an __init__ function.
"""
parser = CodeParser("")
@ -359,8 +314,7 @@ def test_code_parser_parse_function_def_init():
def test_component_get_code_tree_syntax_error():
"""
Test the get_code_tree method of the Component class
"""Test the get_code_tree method of the Component class
raises the CodeSyntaxError when given incorrect syntax.
"""
component = BaseComponent(_code="import os as", _function_entrypoint_name="build")
@ -369,8 +323,7 @@ def test_component_get_code_tree_syntax_error():
def test_custom_component_class_template_validation_no_code():
"""
Test the _class_template_validation method of the CustomComponent class
"""Test the _class_template_validation method of the CustomComponent class
raises the HTTPException when the code is None.
"""
custom_component = CustomComponent(_code=None, _function_entrypoint_name="build")
@ -379,8 +332,7 @@ def test_custom_component_class_template_validation_no_code():
def test_custom_component_get_code_tree_syntax_error():
"""
Test the get_code_tree method of the CustomComponent class
"""Test the get_code_tree method of the CustomComponent class
raises the CodeSyntaxError when given incorrect syntax.
"""
custom_component = CustomComponent(_code="import os as", _function_entrypoint_name="build")
@ -389,8 +341,7 @@ def test_custom_component_get_code_tree_syntax_error():
def test_custom_component_get_function_entrypoint_args_no_args():
"""
Test the get_function_entrypoint_args property of
"""Test the get_function_entrypoint_args property of
the CustomComponent class with a build method with no arguments.
"""
my_code = """
@ -405,8 +356,7 @@ class MyMainClass(CustomComponent):
def test_custom_component_get_function_entrypoint_return_type_no_return_type():
"""
Test the get_function_entrypoint_return_type property of the
"""Test the get_function_entrypoint_return_type property of the
CustomComponent class with a build method with no return type.
"""
my_code = """
@ -421,8 +371,7 @@ class MyClass(CustomComponent):
def test_custom_component_get_main_class_name_no_main_class():
"""
Test the get_main_class_name property of the
"""Test the get_main_class_name property of the
CustomComponent class when there is no main class.
"""
my_code = """
@ -435,8 +384,7 @@ def build():
def test_custom_component_build_not_implemented():
"""
Test the build method of the CustomComponent
"""Test the build method of the CustomComponent
class raises the NotImplementedError.
"""
custom_component = CustomComponent(_code="def build(): pass", _function_entrypoint_name="build")
@ -453,7 +401,7 @@ def test_build_config_no_code():
@pytest.fixture
def component():
yield CustomComponent(
return CustomComponent(
field_config={
"fields": {
"llm": {"type": "str"},