From 316cd23747ef23f3ebd2358dcfee0df2f284de72 Mon Sep 17 00:00:00 2001
From: saipraneeth <2506664+msaipraneeth@users.noreply.github.com>
Date: Wed, 25 Sep 2024 14:33:49 +0100
Subject: [PATCH] fix ruff and mypy issues
---
.../dataintegration/description.py | 4 ++--
.../dataintegration/parameter/choice.py | 7 +++++--
.../dataintegration/utils/__init__.py | 5 ++++-
.../dataintegration/utils/entity_builder.py | 4 ++--
tests/conftest.py | 19 ++++++++++---------
tests/parameter_types/test_code.py | 16 ++++++++--------
tests/parameter_types/test_resource.py | 3 ++-
tests/test_output_only_plugin.py | 1 +
tests/utils.py | 5 +++--
9 files changed, 37 insertions(+), 27 deletions(-)
diff --git a/cmem_plugin_base/dataintegration/description.py b/cmem_plugin_base/dataintegration/description.py
index 8654493..f121e89 100644
--- a/cmem_plugin_base/dataintegration/description.py
+++ b/cmem_plugin_base/dataintegration/description.py
@@ -275,11 +275,11 @@ def __call__(self, func: type):
def retrieve_parameters(self, plugin_class: type) -> list[PluginParameter]:
"""Retrieve parameters from a plugin class and matches them with the user parameter defs"""
# Only return parameters for user-defined init methods.
- if not hasattr(plugin_class.__init__, "__code__"): # type: ignore[misc]
+ if not hasattr(plugin_class.__init__, "__code__"): # type: ignore[misc]
return []
# Collect parameters from init method
params = []
- sig = inspect.signature(plugin_class.__init__) # type: ignore[misc]
+ sig = inspect.signature(plugin_class.__init__) # type: ignore[misc]
for name in sig.parameters:
if name != "self":
param = next((p for p in self.parameters if p.name == name), None)
diff --git a/cmem_plugin_base/dataintegration/parameter/choice.py b/cmem_plugin_base/dataintegration/parameter/choice.py
index fa41f49..8fe4638 100644
--- a/cmem_plugin_base/dataintegration/parameter/choice.py
+++ b/cmem_plugin_base/dataintegration/parameter/choice.py
@@ -26,7 +26,10 @@ def label(
return self.choice_list[value]
def autocomplete(
- self, query_terms: list[str], depend_on_parameter_values: list[Any], context: PluginContext,
+ self,
+ query_terms: list[str],
+ depend_on_parameter_values: list[Any],
+ context: PluginContext,
) -> list[Autocompletion]:
"""Autocompletion request - Returns all results that match ALL provided query terms."""
result = []
@@ -38,5 +41,5 @@ def autocomplete(
for term in query_terms:
if term.lower() in label.lower():
result.append(Autocompletion(value=identifier, label=label)) # noqa: PERF401
- result.sort(key=lambda x: x.label) # type: ignore[return-value, arg-type]
+ result.sort(key=lambda x: x.label) # type: ignore[return-value, arg-type]
return list(set(result))
diff --git a/cmem_plugin_base/dataintegration/utils/__init__.py b/cmem_plugin_base/dataintegration/utils/__init__.py
index 665f616..86fdff9 100644
--- a/cmem_plugin_base/dataintegration/utils/__init__.py
+++ b/cmem_plugin_base/dataintegration/utils/__init__.py
@@ -2,6 +2,7 @@
import os
import re
+from typing import IO
from cmem.cmempy.workspace.projects.datasets.dataset import post_resource
@@ -70,7 +71,9 @@ def split_task_id(task_id: str) -> tuple:
return project_part, task_part
-def write_to_dataset(dataset_id: str, file_resource=None, context: UserContext | None = None):
+def write_to_dataset( # noqa: ANN201
+ dataset_id: str, file_resource: IO | None = None, context: UserContext | None = None
+):
"""Write to a dataset.
Args:
diff --git a/cmem_plugin_base/dataintegration/utils/entity_builder.py b/cmem_plugin_base/dataintegration/utils/entity_builder.py
index 950b032..cd896ec 100644
--- a/cmem_plugin_base/dataintegration/utils/entity_builder.py
+++ b/cmem_plugin_base/dataintegration/utils/entity_builder.py
@@ -133,10 +133,10 @@ def _get_entity(
values.append(
[f"{data.get(_.path)}"]
if _.is_single_value
- else [f"{_v}" for _v in data.get(_.path)]
+ else [f"{_v}" for _v in data.get(_.path)] # type: ignore[union-attr]
)
else:
- _data = [data.get(_.path)] if _.is_single_value else data.get(_.path)
+ _data: list[dict] = [data.get(_.path)] if _.is_single_value else data.get(_.path) # type: ignore[assignment,list-item]
sub_entities_uri = []
for _v in _data:
sub_entity_path = f"{path_from_root}/{_.path}"
diff --git a/tests/conftest.py b/tests/conftest.py
index 89df83c..a1e5b75 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -33,8 +33,16 @@ def _json_dataset() -> Generator[dict, None, None]:
delete_project(PROJECT_NAME)
+@dataclass
+class JSONResourceFixtureDate:
+ """fixture dataclass"""
+
+ project_name: str
+ resource_name: str
+
+
@pytest.fixture(name="json_resource", scope="module")
-def _json_resource() -> object:
+def _json_resource() -> Generator[JSONResourceFixtureDate, None, None]:
"""Set up json resource"""
_project_name = "resource_test_project"
_resource_name = "sample_test.json"
@@ -46,13 +54,6 @@ def _json_resource() -> object:
replace=True,
)
- @dataclass
- class FixtureDate:
- """fixture dataclass"""
-
- project_name = _project_name
- resource_name = _resource_name
-
- _ = FixtureDate()
+ _ = JSONResourceFixtureDate(project_name=_project_name, resource_name=_resource_name)
yield _
delete_project(_project_name)
diff --git a/tests/parameter_types/test_code.py b/tests/parameter_types/test_code.py
index e46211c..accc748 100644
--- a/tests/parameter_types/test_code.py
+++ b/tests/parameter_types/test_code.py
@@ -32,14 +32,14 @@ class MyTransformPlugin(TransformPlugin):
def __init__( # pylint: disable=too-many-arguments # noqa: PLR0913
self,
- xml: XmlCode = XmlCode(""), # noqa: B008
- json: JsonCode = JsonCode("{}"), # noqa: B008
- jinja: JinjaCode = JinjaCode(""), # noqa: B008
- sql: SqlCode = SqlCode(""), # noqa: B008
- yaml: YamlCode = YamlCode(""), # noqa: B008
- sparql: SparqlCode = SparqlCode(""), # noqa: B008
- turtle: TurtleCode = TurtleCode(""), # noqa: B008
- python: PythonCode = PythonCode(""), # noqa: B008
+ xml: XmlCode = XmlCode(""), # noqa: B008
+ json: JsonCode = JsonCode("{}"), # noqa: B008
+ jinja: JinjaCode = JinjaCode(""), # noqa: B008
+ sql: SqlCode = SqlCode(""), # noqa: B008
+ yaml: YamlCode = YamlCode(""), # noqa: B008
+ sparql: SparqlCode = SparqlCode(""), # noqa: B008
+ turtle: TurtleCode = TurtleCode(""), # noqa: B008
+ python: PythonCode = PythonCode(""), # noqa: B008
) -> None:
self.xml = xml
self.json = json
diff --git a/tests/parameter_types/test_resource.py b/tests/parameter_types/test_resource.py
index 277da1a..5f317c6 100644
--- a/tests/parameter_types/test_resource.py
+++ b/tests/parameter_types/test_resource.py
@@ -1,11 +1,12 @@
"""resource parameter type tests"""
from cmem_plugin_base.dataintegration.parameter.resource import ResourceParameterType
+from tests.conftest import JSONResourceFixtureDate
from tests.utils import TestPluginContext, get_autocomplete_values, needs_cmem
@needs_cmem
-def test_resource_parameter_type_completion(json_resource) -> None:
+def test_resource_parameter_type_completion(json_resource: JSONResourceFixtureDate) -> None:
"""Test resource parameter type completion"""
project_name = json_resource.project_name
resource_name = json_resource.resource_name
diff --git a/tests/test_output_only_plugin.py b/tests/test_output_only_plugin.py
index db776d8..7ab09c6 100644
--- a/tests/test_output_only_plugin.py
+++ b/tests/test_output_only_plugin.py
@@ -1,4 +1,5 @@
"""test file."""
+
from collections.abc import Sequence
from cmem_plugin_base.dataintegration.context import ExecutionContext
diff --git a/tests/utils.py b/tests/utils.py
index 3740ea8..c087d1c 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -43,8 +43,9 @@ def __init__(
self.user = user
-def get_autocomplete_values(parameter: ParameterType, query_terms: list[str],
- context: PluginContext) -> list[str]:
+def get_autocomplete_values(
+ parameter: ParameterType, query_terms: list[str], context: PluginContext
+) -> list[str]:
"""Get autocomplete values"""
return [
x.value