Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1037 from ess-dmsc/Pylance-error-fixes
Browse files Browse the repository at this point in the history
Pylance error fixes
  • Loading branch information
ggoneiESS authored Sep 15, 2023
2 parents cf1a56e + b5db3b3 commit 7c37604
Show file tree
Hide file tree
Showing 168 changed files with 310 additions and 296 deletions.
12 changes: 6 additions & 6 deletions nexus_constructor/json/json_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@

@attr.s
class InvalidJson:
message = attr.ib(type=str)
message: str = attr.ib()


@attr.s
class InvalidShape:
message = attr.ib(type=str)
message: str = attr.ib()


@attr.s
class InvalidTransformation:
message = attr.ib(type=str)
message: str = attr.ib()


@attr.s
class TransformDependencyMissing:
message = attr.ib(type=str)
message: str = attr.ib()


@attr.s
class NameFieldMissing:
message = attr.ib(type=str)
message: str = attr.ib()


@attr.s
class NXClassAttributeMissing:
message = attr.ib(type=str)
message: str = attr.ib()


JsonWarning = Union[
Expand Down
137 changes: 74 additions & 63 deletions nexus_constructor/json/load_from_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@

class JSONReader:
def __init__(self):
self.entry_node: Group = None
self.model = Model()
self.sample_name: str = ""
self.warnings = JsonWarningsContainer()
Expand Down Expand Up @@ -199,14 +198,15 @@ def load_model_from_json(self, filename: str) -> bool:
return self._load_from_json_dict(json_dict)

def _load_from_json_dict(self, json_dict: Dict) -> bool:
self.entry_node = self._read_json_object(json_dict[CommonKeys.CHILDREN][0])
self.model.entry.attributes = self.entry_node.attributes
for child in self.entry_node.children:
if isinstance(child, (Dataset, Link, FileWriter, Group)):
self.model.entry[child.name] = child
else:
self.model.entry.children.append(child)
child.parent_node = self.model.entry
entry_node = self._read_json_object(json_dict[CommonKeys.CHILDREN][0])
if entry_node:
self.model.entry.attributes = entry_node.attributes
for child in entry_node.children:
if isinstance(child, (Dataset, Link, FileWriter, Group)):
self.model.entry[child.name] = child
else:
self.model.entry.children.append(child)
child.parent_node = self.model.entry
self._set_transforms_depends_on()
self._set_components_depends_on()
self._append_transformations_to_nx_group()
Expand All @@ -231,76 +231,87 @@ def _replace_placeholder(self, placeholder: str):
}
return None

def _read_json_object(self, json_object: Dict, parent_node: Group = None):
def _read_json_object(
self, json_object: Optional[Dict], parent_node: Optional[Group] = None
):
"""
Tries to create a component based on the contents of the JSON file.
:param json_object: A component from the JSON dictionary.
:param parent_name: The name of the parent object. Used for warning messages if something goes wrong.
"""
nexus_object: Union[Group, FileWriterModule] = None
nexus_object: Union[Group, FileWriterModule, None] = None
use_placeholder = False
if isinstance(json_object, str) and json_object in PLACEHOLDER_WITH_NX_CLASSES:
json_object = self._replace_placeholder(json_object)
if not json_object:
return
return None
use_placeholder = True
if (
CommonKeys.TYPE in json_object
and json_object[CommonKeys.TYPE] == NodeType.GROUP
):
try:
name = json_object[CommonKeys.NAME]
except KeyError:
self._add_object_warning(CommonKeys.NAME, parent_node)
if json_object:
if (
CommonKeys.TYPE in json_object
and json_object[CommonKeys.TYPE] == NodeType.GROUP
):
try:
name = json_object[CommonKeys.NAME]
except KeyError:
self._add_object_warning(CommonKeys.NAME, parent_node)
return None
nx_class = _find_nx_class(json_object.get(CommonKeys.ATTRIBUTES))
if nx_class == SAMPLE_CLASS_NAME:
self.sample_name = name
if not self._validate_nx_class(name, nx_class):
self._add_object_warning(
f"valid Nexus class {nx_class}", parent_node
)
if nx_class in COMPONENT_TYPES:
nexus_object = Component(name=name, parent_node=parent_node)
children_dict = json_object[CommonKeys.CHILDREN]
self._add_transform_and_shape_to_component(
nexus_object, children_dict
)
self.model.append_component(nexus_object)
else:
nexus_object = Group(name=name, parent_node=parent_node)
if nexus_object:
nexus_object.nx_class = nx_class
if CommonKeys.CHILDREN in json_object:
for child in json_object[CommonKeys.CHILDREN]:
node = self._read_json_object(child, nexus_object)
if node and isinstance(node, StreamModule):
nexus_object.children.append(node)
nexus_object.remove_stream_module(node.writer_module)
elif node and node.name not in nexus_object:
nexus_object[node.name] = node
elif CommonKeys.MODULE in json_object and NodeType.CONFIG in json_object:
module_type = json_object[CommonKeys.MODULE]
if (
module_type == WriterModules.DATASET.value
or module_type == WriterModules.FILEWRITER.value
) and json_object[NodeType.CONFIG][
CommonKeys.NAME
] == CommonAttrs.DEPENDS_ON:
nexus_object = None
elif module_type in [x.value for x in WriterModules]:
nexus_object = create_fw_module_object(
module_type, json_object[NodeType.CONFIG], parent_node
)
if nexus_object:
nexus_object.parent_node = parent_node
else:
self._add_object_warning("valid module type", parent_node)
return None
elif json_object == USERS_PLACEHOLDER:
self.model.entry.users_placeholder = True
return None
nx_class = _find_nx_class(json_object.get(CommonKeys.ATTRIBUTES))
if nx_class == SAMPLE_CLASS_NAME:
self.sample_name = name
if not self._validate_nx_class(name, nx_class):
self._add_object_warning(f"valid Nexus class {nx_class}", parent_node)
if nx_class in COMPONENT_TYPES:
nexus_object = Component(name=name, parent_node=parent_node)
children_dict = json_object[CommonKeys.CHILDREN]
self._add_transform_and_shape_to_component(nexus_object, children_dict)
self.model.append_component(nexus_object)
else:
nexus_object = Group(name=name, parent_node=parent_node)
nexus_object.nx_class = nx_class
if CommonKeys.CHILDREN in json_object:
for child in json_object[CommonKeys.CHILDREN]:
node = self._read_json_object(child, nexus_object)
if node and isinstance(node, StreamModule):
nexus_object.children.append(node)
nexus_object.remove_stream_module(node.writer_module)
elif node and node.name not in nexus_object:
nexus_object[node.name] = node
elif CommonKeys.MODULE in json_object and NodeType.CONFIG in json_object:
module_type = json_object[CommonKeys.MODULE]
if (
module_type == WriterModules.DATASET.value
or module_type == WriterModules.FILEWRITER.value
) and json_object[NodeType.CONFIG][
CommonKeys.NAME
] == CommonAttrs.DEPENDS_ON:
nexus_object = None
elif module_type in [x.value for x in WriterModules]:
nexus_object = create_fw_module_object(
module_type, json_object[NodeType.CONFIG], parent_node
self._add_object_warning(
f"valid {CommonKeys.TYPE} or {CommonKeys.MODULE}", parent_node
)
nexus_object.parent_node = parent_node
else:
self._add_object_warning("valid module type", parent_node)
return None
elif json_object == USERS_PLACEHOLDER:
self.model.entry.users_placeholder = True
return None
else:
self._add_object_warning(
f"valid {CommonKeys.TYPE} or {CommonKeys.MODULE}", parent_node
)
self._add_object_warning("!!No json_object!!", parent_node)

# Add attributes to nexus_object.
if nexus_object:
if nexus_object and json_object:
json_attrs = json_object.get(CommonKeys.ATTRIBUTES)
if json_attrs:
attributes = Attributes()
Expand Down
4 changes: 2 additions & 2 deletions nexus_constructor/json/load_from_json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _find_attribute_from_dict(attribute_name: str, entry: dict) -> Any:

def _find_attribute_from_list_or_dict(
attribute_name: str,
entry: Union[list, dict],
entry: Optional[Union[list, dict, str]],
) -> Any:
"""
Attempts to determine the value of an attribute in a dictionary or a list of dictionaries.
Expand All @@ -81,7 +81,7 @@ def _find_attribute_from_list_or_dict(
return _find_attribute_from_dict(attribute_name, entry)


def _find_nx_class(entry: Union[list, dict]) -> str:
def _find_nx_class(entry: Union[list, dict, str, None]) -> str:
"""
Tries to find the NX class value from a dictionary or a list of dictionaries.
:param entry: A dictionary or list of dictionaries.
Expand Down
4 changes: 2 additions & 2 deletions nexus_constructor/json/transform_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class TransformId:
Uniquely identifies a Transformation
"""

component_name = attr.ib(type=str)
transform_name = attr.ib(type=str)
component_name: str = attr.ib()
transform_name: str = attr.ib()
12 changes: 7 additions & 5 deletions nexus_constructor/model/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ class Component(Group):
Base class for a component object. In the NeXus file this would translate to the component group.
"""

_depends_on = attr.ib(type=Transformation, default=None)
has_link = attr.ib(type=bool, default=None)
component_info: "ComponentInfo" = None
stored_transforms: list = None
_depends_on: Transformation = attr.ib(default=None)
has_link: bool = attr.ib(default=None)
component_info: Optional["ComponentInfo"] = None
stored_transforms: list = []
name: str = ""
parent_node: Optional[Group] = None

@property
def stored_items(self) -> List:
Expand Down Expand Up @@ -271,7 +273,7 @@ def _create_and_add_transform(
units: str,
vector: QVector3D,
depends_on: Transformation,
values: Union[Dataset, Group],
values: Union[Dataset, Group, StreamModule],
target_pos: int = -1,
) -> Transformation:
if name is None:
Expand Down
16 changes: 8 additions & 8 deletions nexus_constructor/model/group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union

import attr

Expand All @@ -19,7 +19,7 @@
from nexus_constructor.model.value_type import ValueTypes

if TYPE_CHECKING:
from nexus_constructor.model.module import FileWriterModule # noqa: F401
from nexus_constructor.model.module import FileWriterModule

TRANSFORMS_GROUP_NAME = "transformations"

Expand All @@ -37,15 +37,15 @@ class Group:
Base class for any group which has a set of children and an nx_class attribute.
"""

name = attr.ib(type=str)
parent_node = attr.ib(type="Group", default=None)
children: List[Union["FileWriterModule", "Group"]] = attr.ib( # noqa: F821
name: str = attr.ib()
parent_node: Optional["Group"] = attr.ib(default=None)
children: List[Union["FileWriterModule", "Group"]] = attr.ib(
factory=list, init=False
)
attributes = attr.ib(type=Attributes, factory=Attributes, init=False)
attributes: Attributes = attr.ib(factory=Attributes, init=False)
values = None
possible_stream_modules = attr.ib(
type=List[str], default=attr.Factory(create_list_of_possible_streams)
possible_stream_modules: List[str] = attr.ib(
default=attr.Factory(create_list_of_possible_streams)
)
_group_placeholder: bool = False

Expand Down
Loading

0 comments on commit 7c37604

Please sign in to comment.