Skip to content

Commit

Permalink
Fix mypy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
robertisele committed Sep 24, 2024
1 parent ee83f2a commit 1e9a6e7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmem_plugin_base/dataintegration/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ def __call__(self, func):
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__"):
if not hasattr(plugin_class.__init__, "__code__"): # type: ignore[misc]
return []
# Collect parameters from init method
params = []
sig = inspect.signature(plugin_class.__init__)
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)
Expand Down
9 changes: 9 additions & 0 deletions tests/parameter_types/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]:
MyTransformPlugin()

plugin = Plugin.plugins[0]
assert plugin.parameters[0].param_type is not None
assert plugin.parameters[1].param_type is not None
assert plugin.parameters[2].param_type is not None
assert plugin.parameters[3].param_type is not None
assert plugin.parameters[4].param_type is not None
assert plugin.parameters[5].param_type is not None
assert plugin.parameters[6].param_type is not None
assert plugin.parameters[7].param_type is not None

assert plugin.parameters[0].param_type.name == "code-xml"
assert plugin.parameters[1].param_type.name == "code-json"
assert plugin.parameters[2].param_type.name == "code-jinja2"
Expand Down
3 changes: 1 addition & 2 deletions tests/parameter_types/test_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: # noqa:
"""Test transform"""
return []

Plugin.plugins.append(MyTransformPlugin)

plugin = Plugin.plugins[0]
password_param = plugin.parameters[0]
assert password_param.param_type is not None
assert password_param.param_type.name == PasswordParameterType.name


Expand Down
4 changes: 4 additions & 0 deletions tests/test_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]:
plugin = Plugin.plugins[0]

no_default_par = plugin.parameters[0]
assert no_default_par.param_type is not None
assert no_default_par.param_type.name == StringParameterType.name
assert no_default_par.default_value is None

string_par = plugin.parameters[1]
assert string_par.param_type is not None
assert string_par.param_type.name == StringParameterType.name
assert string_par.default_value == "value"

float_par = plugin.parameters[2]
assert float_par.param_type is not None
assert float_par.param_type.name == FloatParameterType.name
assert float_par.default_value == 1.5

bool_par = plugin.parameters[3]
assert bool_par.param_type is not None
assert bool_par.param_type.name == BoolParameterType.name
assert bool_par.default_value is True

Expand Down
27 changes: 16 additions & 11 deletions tests/test_utils_build_entities_from_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

import json

from cmem_plugin_base.dataintegration.entity import EntityPath, EntitySchema
from cmem_plugin_base.dataintegration.entity import EntityPath, EntitySchema, Entities
from cmem_plugin_base.dataintegration.utils.entity_builder import build_entities_from_data


def build_entities_from_json(json_data: str) -> Entities:
data = json.loads(json_data)
entities = build_entities_from_data(data)
assert entities is not None
return entities


def test_single_object() -> None:
"""Test generation of entities and schema for a simple JSON object."""
test_data = """
{
"name": "sai",
"email": "[email protected]"
}"""
data = json.loads(test_data)
entities = build_entities_from_data(data)
entities = build_entities_from_json(test_data)
assert len(list(entities.entities)) == 1
for _ in entities.entities:
assert len(_.values) == 2
Expand Down Expand Up @@ -42,8 +48,7 @@ def test_single_object_one_level() -> None:
"country": "United States"
}
}"""
data = json.loads(test_data)
entities = build_entities_from_data(data)
entities = build_entities_from_json(test_data)
assert len(list(entities.entities)) == 1
for _ in entities.entities:
assert len(_.values) == 3
Expand All @@ -59,6 +64,7 @@ def test_single_object_one_level() -> None:
],
)
# Validate sub entities
assert entities.sub_entities is not None
for _ in entities.sub_entities:
for _entity in _.entities:
assert len(_entity.values) == 2
Expand Down Expand Up @@ -89,8 +95,7 @@ def test_single_object_one_level_array() -> None:
"country": "United States"
}]
}"""
data = json.loads(test_data)
entities = build_entities_from_data(data)
entities = build_entities_from_json(test_data)
assert len(list(entities.entities)) == 1
for _ in entities.entities:
assert len(_.values) == 3
Expand All @@ -106,6 +111,7 @@ def test_single_object_one_level_array() -> None:
],
)
# Validate sub entities
assert entities.sub_entities is not None
for _ in entities.sub_entities:
assert len(list(_.entities)) == 2
for _entity in _.entities:
Expand Down Expand Up @@ -146,8 +152,7 @@ def test_single_object_two_level_array() -> None:
}
]
}"""
data = json.loads(test_data)
entities = build_entities_from_data(data)
entities = build_entities_from_json(test_data)
assert len(list(entities.entities)) == 1
for _ in entities.entities:
assert len(_.values) == 3
Expand All @@ -163,6 +168,7 @@ def test_single_object_two_level_array() -> None:
],
)
# Validate sub entities
assert entities.sub_entities is not None
location_entities = entities.sub_entities[0]
city_entities = entities.sub_entities[1]
assert len(list(city_entities.entities)) == 2
Expand Down Expand Up @@ -196,8 +202,7 @@ def test_array_object() -> None:
"name": "sai",
"email": "[email protected]"
}]"""
data = json.loads(test_data)
entities = build_entities_from_data(data)
entities = build_entities_from_json(test_data)
_ = next(entities.entities)
assert len(_.values) == 2
assert _.values == [["seebi"], [""]]
Expand Down

0 comments on commit 1e9a6e7

Please sign in to comment.