Skip to content

Commit

Permalink
style(python): ruff fixes ANN001,ANN201,ANN401
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardHd committed Nov 13, 2023
1 parent d4eb7ac commit 9e9354e
Show file tree
Hide file tree
Showing 40 changed files with 99 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class ActivityNotFoundError(Exception):
def __init__(self, activity_name) -> None:
def __init__(self, activity_name: str) -> None:
super().__init__(f"Activity with name {activity_name} not found")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class FunctionCallInvalidArgumentsCountError(Exception):
def __init__(self, name, evaluated_arguments, expected_argument_names) -> None:
def __init__(self, name: str, evaluated_arguments: list, expected_argument_names: list) -> None:
message = (
f"FunctionCall {name} has invalid arguments count. "
f"Evaluated arguments: \"{', '.join(map(str, evaluated_arguments))}\". "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class PipelineNotFoundException(Exception):
def __init__(self, pipeline_name) -> None:
def __init__(self, pipeline_name: str) -> None:
super().__init__(f"Pipeline with name {pipeline_name} not found")
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class VariableBeingEvaluatedDoesNotExistError(Exception):
def __init__(self, variable_name) -> None:
def __init__(self, variable_name: str) -> None:
super().__init__(f"Variable being evaluated does not exist: {variable_name}")
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class VariableDoesNotExistError(Exception):
def __init__(self, variable_name) -> None:
def __init__(self, variable_name: str) -> None:
super().__init__(f"Variable does not exist: {variable_name}")
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState


def find_and_replace_activity(expression: str, state: PipelineRunState):
def find_and_replace_activity(expression: str, state: PipelineRunState) -> str:
pattern = r"activity\('(?P<activity_name>.*?)'\)(?:\.(\w+))+$"
match = re.match(pattern, expression)
if match:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState


def find_and_replace_dataset(expression: str, state: PipelineRunState):
def find_and_replace_dataset(expression: str, state: PipelineRunState) -> str:
pattern = r"(@?{?dataset\(\'(\w+)\'\)}?)"
matches = re.finditer(pattern, expression, re.MULTILINE)
for match in matches:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState


def find_and_replace_iteration_item(expression: str, state: PipelineRunState):
def find_and_replace_iteration_item(expression: str, state: PipelineRunState) -> str:
pattern = r"(@?{?item\(\)\}?)"
matches = re.finditer(pattern, expression, re.MULTILINE)
for match in matches:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState


def find_and_replace_linked_services(expression: str, state: PipelineRunState):
def find_and_replace_linked_services(expression: str, state: PipelineRunState) -> str:
pattern = r"(@?{?linkedService\(\'(\w+)\'\)}?)"
matches = re.finditer(pattern, expression, re.MULTILINE)
for match in matches:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState


def find_and_replace_parameters(expression: str, parameter_type: RunParameterType, state: PipelineRunState):
def find_and_replace_parameters(expression: str, parameter_type: RunParameterType, state: PipelineRunState) -> str:
pattern = rf"(@?{{?pipeline\(\)\.{_get_parameter_string_template(parameter_type)}\.(\w+)}}?)"
matches = re.finditer(pattern, expression, re.MULTILINE)
for match in matches:
Expand All @@ -24,7 +24,7 @@ def find_and_replace_parameters(expression: str, parameter_type: RunParameterTyp
return expression


def _get_parameter_string_template(parameter_type: RunParameterType):
def _get_parameter_string_template(parameter_type: RunParameterType) -> str:
if parameter_type == RunParameterType.Pipeline:
return "parameters"
elif parameter_type == RunParameterType.Global:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState


def find_and_replace_variables(expression: str, state: PipelineRunState):
def find_and_replace_variables(expression: str, state: PipelineRunState) -> str:
pattern = r"(@?{?variables\(\'(\w+)\'\)}?)"
matches = re.finditer(pattern, expression, re.MULTILINE)
for match in matches:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState


def trim_one_char(text: str, character):
def trim_one_char(text: str, character: str) -> str:
if text.startswith(character):
text = text[1:]
if text.endswith(character):
Expand All @@ -24,7 +24,7 @@ class FunctionArgument:
def __init__(self, expression: str) -> None:
self.expression = expression.strip("\n").strip(" ")

def evaluate(self, state: PipelineRunState):
def evaluate(self, state: PipelineRunState) -> str:
evaluated_expression = find_and_replace_parameters(self.expression, RunParameterType.Pipeline, state)
evaluated_expression = find_and_replace_parameters(evaluated_expression, RunParameterType.Global, state)
evaluated_expression = find_and_replace_variables(evaluated_expression, state)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Any, List

from data_factory_testing_framework.exceptions.function_call_invalid_arguments_count_error import (
FunctionCallInvalidArgumentsCountError,
Expand All @@ -14,7 +14,7 @@ def __init__(self, name: str, arguments: List) -> None:
self.name = name
self.arguments = arguments

def evaluate(self, state):
def evaluate(self, state: Any) -> str: # noqa: ANN401 - what is the type of state?
function = FunctionsRepository.functions.get(self.name)
if not function:
raise UnsupportedFunctionError(self.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extract_func_regex = r"^@?{?([^()]+?)\((.*)\)}?$"


def parse_expression(expression: str):
def parse_expression(expression: str) -> FunctionArgument:
match = re.match(extract_func_regex, expression, re.DOTALL)
if not match:
return FunctionArgument(expression)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from collections.abc import Iterable as IterableType
from datetime import datetime
from typing import Callable, Dict
from typing import Any, Callable, Dict


class FunctionsRepository:
Expand Down Expand Up @@ -35,25 +35,25 @@ class FunctionsRepository:
}

@staticmethod
def _register(function_name: str, function: Callable):
def _register(function_name: str, function: Callable) -> None:
FunctionsRepository.functions[function_name] = function

@staticmethod
def _func_equals(argument0, argument1):
def _func_equals(argument0: Any, argument1: Any) -> bool: # noqa: ANN401
if type(argument0) != type(argument1):
raise ValueError("Equals function requires arguments of the same type.")
return argument0 == argument1

@staticmethod
def _trim(text, trim_argument):
def _trim(text: str, trim_argument: str) -> str:
return text.strip(trim_argument[0])

@staticmethod
def _json(argument):
def _json(argument: Any) -> Any: # noqa: ANN401
return argument

@staticmethod
def _contains(obj, value):
def _contains(obj: Any, value: Any) -> bool: # noqa: ANN401
if isinstance(obj, dict):
return value in obj
elif isinstance(obj, IterableType):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def evaluate(self, state: PipelineRunState) -> Activity:
self.status: DependencyCondition = DependencyCondition.Succeeded
return self

def evaluate_expressions(self, obj: Any, state: PipelineRunState, visited: List[Any] = None):
def evaluate_expressions(self, obj: Any, state: PipelineRunState, visited: List[Any] = None) -> None: # noqa: ANN401
if visited is None:
visited = []

Expand Down Expand Up @@ -46,7 +46,7 @@ def evaluate_expressions(self, obj: Any, state: PipelineRunState, visited: List[
else:
self.evaluate_expressions(attribute, state, visited)

def are_dependency_condition_met(self, state: PipelineRunState):
def are_dependency_condition_met(self, state: PipelineRunState) -> bool:
if not self.depends_on:
return True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ def evaluate_control_activity_iterations(
self,
state: PipelineRunState,
evaluate_activities: Callable[[PipelineRunState], Generator[Activity, None, None]],
):
) -> Generator[Activity, None, None]:
return []
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class ForEachActivity:
def evaluate(self: ForEachActivity, state: PipelineRunState):
def evaluate(self: ForEachActivity, state: PipelineRunState) -> ForEachActivity:
self.items.evaluate(state)

return super(ControlActivity, self).evaluate(state)
Expand All @@ -14,7 +14,7 @@ def evaluate_control_activity_iterations(
self: ForEachActivity,
state: PipelineRunState,
evaluate_activities: Callable[[PipelineRunState], Generator[Activity, None, None]],
):
) -> Generator[Activity, None, None]:
for item in self.items.evaluated:
scoped_state = state.create_iteration_scope(item)
for activity in evaluate_activities(self.activities, scoped_state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class IfConditionActivity:
def evaluate(self: IfConditionActivity, state: PipelineRunState):
def evaluate(self: IfConditionActivity, state: PipelineRunState) -> IfConditionActivity:
self.expression.evaluate(state)

return super(ControlActivity, self).evaluate(state)
Expand All @@ -18,7 +18,7 @@ def evaluate_control_activity_iterations(
self: IfConditionActivity,
state: PipelineRunState,
evaluate_activities: Callable[[PipelineRunState], Generator[Activity, None, None]],
):
) -> Generator[Activity, None, None]:
scoped_state = state.create_iteration_scope(None)
activities = self.if_true_activities if self.expression.evaluated else self.if_false_activities
for activity in evaluate_activities(activities, scoped_state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class UntilActivity:
def evaluate(self: UntilActivity, state: PipelineRunState):
def evaluate(self: UntilActivity, state: PipelineRunState) -> UntilActivity:
self.expression.evaluate(state)

return super(ControlActivity, self).evaluate(state)
Expand All @@ -14,7 +14,7 @@ def evaluate_control_activity_iterations(
self: UntilActivity,
state: PipelineRunState,
evaluate_activities: Callable[[PipelineRunState], Generator[Activity, None, None]],
):
) -> Generator[Activity, None, None]:
while True:
scoped_state = state.create_iteration_scope(None)
for activity in evaluate_activities(self.activities, scoped_state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class SetVariableActivity:
def evaluate(self: SetVariableActivity, state: PipelineRunState):
def evaluate(self: SetVariableActivity, state: PipelineRunState) -> SetVariableActivity:
super(ControlActivity, self).evaluate(state)

if self.variable_name == "pipelineReturnValue":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Generic, TypeVar
from typing import Any, Generic, TypeVar

from data_factory_testing_framework.functions.function_parser import parse_expression
from data_factory_testing_framework.generated.models import DataFactoryElement
Expand All @@ -7,6 +7,6 @@


class DataFactoryElement(Generic[T]):
def evaluate(self: DataFactoryElement, state):
def evaluate(self: DataFactoryElement, state: Any) -> None: # noqa: ANN401
self.value = parse_expression(self.expression).evaluate(state)
return self.value
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class Expression[TResult]:
def __init__(self) -> None:
self.evaluated: TResult = []

def evaluate(self: Expression, state: PipelineRunState):
def evaluate(self: Expression, state: PipelineRunState) -> TResult:
self.evaluated = parse_expression(self.value).evaluate(state)
return self.evaluated
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


# Patch models with our custom classes
def patch_models():
def patch_models() -> None:
patch_model(_models.Activity, Activity)
patch_model(_models.ExecutePipelineActivity, ExecutePipelineActivity)
patch_model(_models.ControlActivity, ControlActivity)
Expand All @@ -29,7 +29,7 @@ def patch_models():
patch_model(_models.DataFactoryElement, DataFactoryElement)


def patch_model(main_class, partial_class):
def patch_model(main_class: type, partial_class: type) -> None:
partial_class_method_list = [
attribute
for attribute in dir(partial_class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_activity_by_name(self: PipelineResource, name: str) -> Activity:

raise ActivityNotFoundError(f"Activity with name {name} not found")

def validate_parameters(self: PipelineResource, parameters: List[RunParameter]):
def validate_parameters(self: PipelineResource, parameters: List[RunParameter]) -> None:
# Check if all parameters are provided
for pipeline_parameter_name, pipeline_parameter_specification in self.parameters.items():
found = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

class DataFactoryRepositoryFactory:
@staticmethod
def parse_from_folder(folder_path: str):
def parse_from_folder(folder_path: str) -> DataFactoryRepository:
pipelines = DataFactoryRepositoryFactory._get_data_factory_entities_by_folder_path(
folder_path,
"PipelineResource",
)
return DataFactoryRepository(pipelines)

@staticmethod
def _get_data_factory_entities_by_folder_path(folder_path: str, target_class: str):
def _get_data_factory_entities_by_folder_path(folder_path: str, target_class: str) -> list:
entities = []
files = os.listdir(folder_path)
for file in files:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(
self.scoped_pipeline_activity_results: Dict[str, Any] = {}
self.iteration_item = iteration_item

def add_activity_result(self, activity_name: str, status: DependencyCondition, output: Any = None):
def add_activity_result(self, activity_name: str, status: DependencyCondition, output: Any = None) -> None: # noqa: ANN401
self.pipeline_activity_results[activity_name] = {
"status": status,
"output": output,
Expand All @@ -46,30 +46,30 @@ def add_activity_result(self, activity_name: str, status: DependencyCondition, o
"output": output,
}

def create_iteration_scope(self, iteration_item: str):
def create_iteration_scope(self, iteration_item: str) -> "PipelineRunState":
return PipelineRunState(
self.parameters,
self._variable_specifications,
self.pipeline_activity_results,
iteration_item,
)

def add_scoped_activity_results_from_scoped_state(self, scoped_state):
def add_scoped_activity_results_from_scoped_state(self, scoped_state: "PipelineRunState") -> None:
for result in scoped_state.pipeline_activity_results:
self.pipeline_activity_results[result] = scoped_state.pipeline_activity_results[result]

def try_get_scoped_activity_result_by_name(self, name: str):
def try_get_scoped_activity_result_by_name(self, name: str) -> Optional[Dict[str, Any]]:
return self.pipeline_activity_results[name] if name in self.pipeline_activity_results else None

def set_variable(self, variable_name: str, value):
def set_variable(self, variable_name: str, value: Any) -> None: # noqa: ANN401
for variable in self.variables:
if variable.name == variable_name:
variable.value = value
return

raise VariableBeingEvaluatedDoesNotExistError(variable_name)

def get_variable_by_name(self, variable_name):
def get_variable_by_name(self, variable_name: str) -> PipelineRunVariable:
for variable in self.variables:
if variable.name == variable_name:
return variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
target_type = "DataFactoryElement"


def get_type_from_description(description):
def get_type_from_description(description: str) -> str:
dfe_type = "str"
if "Expression with resultType string" in description:
dfe_type = "str"
Expand All @@ -16,7 +16,7 @@ def get_type_from_description(description):
return dfe_type


def update_attribute_type(arg, description):
def update_attribute_type(arg: ast.arg, description: str) -> None:
dfe_type = get_type_from_description(description)
if "Expression" in description:
if (
Expand All @@ -39,7 +39,7 @@ def update_attribute_type(arg, description):
)


def transform_ast(node):
def transform_ast(node: ast.ClassDef) -> None:
if isinstance(node, ast.ClassDef):
for body_item in node.body:
if isinstance(body_item, ast.FunctionDef):
Expand Down
Loading

0 comments on commit 9e9354e

Please sign in to comment.