From 6ed3e6f70dda3c6d1840058d96a5fe3fa55a3955 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Sun, 3 Sep 2023 23:38:00 +0000 Subject: [PATCH] Remove warnings in unit tests --- .github/workflows/publish.yml | 2 +- aiidalab_widgets_base/data/__init__.py | 5 ++++- aiidalab_widgets_base/nodes.py | 2 +- aiidalab_widgets_base/process.py | 11 +++++------ aiidalab_widgets_base/structures.py | 12 +++++++----- aiidalab_widgets_base/viewers.py | 25 +++++++------------------ tests/test_databases.py | 5 ++++- tests/test_process.py | 4 ++-- tests/test_viewers.py | 2 +- 9 files changed, 32 insertions(+), 36 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index e3fd2ca50..baf9db738 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -19,7 +19,7 @@ jobs: steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python 3.10 uses: actions/setup-python@v2 diff --git a/aiidalab_widgets_base/data/__init__.py b/aiidalab_widgets_base/data/__init__.py index c0dfa4334..a933950b4 100644 --- a/aiidalab_widgets_base/data/__init__.py +++ b/aiidalab_widgets_base/data/__init__.py @@ -34,7 +34,10 @@ def __init__(self, value=0, description="Select functional group", **kwargs): self.style = {"description_width": "initial"} self.layout = {"width": "initial"} super().__init__( - value=value, description=description, options=FUNCTIONAL_GROUPS, **kwargs + value=value, + description=description, + options=[(key, value) for key, value in FUNCTIONAL_GROUPS.items()], + **kwargs, ) def rotate(self, align_to=(0, 0, 1), remove_anchor=False): diff --git a/aiidalab_widgets_base/nodes.py b/aiidalab_widgets_base/nodes.py index c2056aab1..770d7caf1 100644 --- a/aiidalab_widgets_base/nodes.py +++ b/aiidalab_widgets_base/nodes.py @@ -320,7 +320,7 @@ class OpenAiidaNodeInAppWidget(ipw.VBox): def __init__(self, path_to_root="../", **kwargs): self.path_to_root = path_to_root - self.tab = ipw.Tab(style={"description_width": "initial"}) + self.tab = ipw.Tab() self.tab_selection = ipw.RadioButtons( options=[], description="", diff --git a/aiidalab_widgets_base/process.py b/aiidalab_widgets_base/process.py index 4501b7884..06a8412b7 100644 --- a/aiidalab_widgets_base/process.py +++ b/aiidalab_widgets_base/process.py @@ -302,7 +302,7 @@ def follow(self, detach=False): if self._monitor is None: self._monitor = ProcessMonitor( - process=self.process, + value=self.process.uuid, callbacks=[self.update], on_sealed=self._run_after_completed, timeout=self.update_interval, @@ -415,7 +415,6 @@ def __init__(self, title="Progress Bar", **kwargs): value=0, min=0, max=2, - step=1, description="Progress:", bar_style="warning", # 'success', 'info', 'warning', 'danger' or '' orientation="horizontal", @@ -538,7 +537,7 @@ def __init__(self, title="Running Job Output", **kwargs): self.title = title self.selection = ipw.Dropdown( description="Select calculation:", - options={p.id: p for p in get_running_calcs(self.process)}, + options=[(p.id, p) for p in get_running_calcs(self.process)], style={"description_width": "initial"}, ) self.output = CalcJobOutputWidget() @@ -551,9 +550,9 @@ def update(self): return with self.hold_trait_notifications(): old_label = self.selection.label - self.selection.options = { - str(p.id): p for p in get_running_calcs(self.process) - } + self.selection.options = [ + (str(p.id), p) for p in get_running_calcs(self.process) + ] # If the selection remains the same. if old_label in self.selection.options: self.label = old_label # After changing options trait, the label and value traits might change as well. diff --git a/aiidalab_widgets_base/structures.py b/aiidalab_widgets_base/structures.py index 055215b63..711f1a437 100644 --- a/aiidalab_widgets_base/structures.py +++ b/aiidalab_widgets_base/structures.py @@ -5,7 +5,6 @@ import io import pathlib import tempfile -from collections import OrderedDict import ase import ipywidgets as ipw @@ -98,7 +97,10 @@ def __init__( # Store format selector. data_format = ipw.RadioButtons( - options=self.SUPPORTED_DATA_FORMATS, description="Data type:" + options=[ + (key, value) for key, value in self.SUPPORTED_DATA_FORMATS.items() + ], + description="Data type:", ) tl.link((data_format, "label"), (self, "node_class")) @@ -658,8 +660,8 @@ def search(self, _=None): matches = {n[0] for n in qbuild.iterall()} matches = sorted(matches, reverse=True, key=lambda n: n.ctime) - options = OrderedDict() - options[f"Select a Structure ({len(matches)} found)"] = False + options = [] + options.append((f"Select a Structure ({len(matches)} found)", False)) for mch in matches: label = f"PK: {mch.pk}" @@ -668,7 +670,7 @@ def search(self, _=None): label += " | " + mch.node_type.split(".")[-2] label += " | " + mch.label label += " | " + mch.description - options[label] = mch + options.append((label, mch)) self.results.options = options diff --git a/aiidalab_widgets_base/viewers.py b/aiidalab_widgets_base/viewers.py index d1ed774f9..77de9f98e 100644 --- a/aiidalab_widgets_base/viewers.py +++ b/aiidalab_widgets_base/viewers.py @@ -34,12 +34,9 @@ def registration_decorator(widget): return registration_decorator -def viewer(obj, downloadable=True, **kwargs): +def viewer(obj, **kwargs): """Display AiiDA data types in Jupyter notebooks. - :param downloadable: If True, add link/button to download the content of displayed AiiDA object. - :type downloadable: bool - Returns the object itself if the viewer wasn't found.""" if not isinstance(obj, orm.Node): # only working with AiiDA nodes warnings.warn( @@ -47,19 +44,12 @@ def viewer(obj, downloadable=True, **kwargs): ) return obj - try: + if obj.node_type in AIIDA_VIEWER_MAPPING: _viewer = AIIDA_VIEWER_MAPPING[obj.node_type] - except KeyError as exc: - if obj.node_type in str(exc): - warnings.warn( - f"Did not find an appropriate viewer for the {type(obj)} object. Returning the object " - "itself.", - stacklevel=2, - ) - return obj - raise + return _viewer(obj, **kwargs) else: - return _viewer(obj, downloadable=downloadable, **kwargs) + # No viewer registered for this type, return object itself + return obj class AiidaNodeViewWidget(ipw.VBox): @@ -294,12 +284,11 @@ def change_supercell(_=None): # 3. Camera switcher camera_type = ipw.ToggleButtons( - options={"Orthographic": "orthographic", "Perspective": "perspective"}, + options=[("Orthographic", "orthographic"), ("Perspective", "perspective")], description="Camera type:", value=self._viewer.camera, layout={"align_self": "flex-start"}, style={"button_width": "115.5px"}, - orientation="vertical", ) def change_camera(change): @@ -482,7 +471,7 @@ def _download_tab(self): ) # 4. Render a high quality image - self.render_btn = ipw.Button(description="Render", icon="fa-paint-brush") + self.render_btn = ipw.Button(description="Render", icon="paint-brush") self.render_btn.on_click(self._render_structure) self.render_box = ipw.VBox( children=[ipw.Label("Render an image with POVRAY:"), self.render_btn] diff --git a/tests/test_databases.py b/tests/test_databases.py index b0f80fe13..594a46b6b 100644 --- a/tests/test_databases.py +++ b/tests/test_databases.py @@ -13,7 +13,10 @@ def test_cod_query_widget(): # Run the query. widget._on_click_query() - # Select on of the results. + # Select one of the results. + # TODO: Select a different structure to get rid of the ASE warning: + # "ase/io/cif.py:401: UserWarning: crystal system 'cubic' is not interpreted + # for space group 'Pm-3m'. This may result in wrong setting!" widget.drop_structure.label = "NiTi (id: 1100132)" # Check that the structure was loaded. diff --git a/tests/test_process.py b/tests/test_process.py index 0ae69de9d..3c0261df1 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -218,8 +218,8 @@ def test_running_calcjob_output_widget(generate_calc_job_node): } ) - # Test the widget can be instantiated with a process - RunningCalcJobOutputWidget(calculation=process) + widget = RunningCalcJobOutputWidget() + widget.process = process @pytest.mark.usefixtures("aiida_profile_clean") diff --git a/tests/test_viewers.py b/tests/test_viewers.py index b5c9287db..c876073e2 100644 --- a/tests/test_viewers.py +++ b/tests/test_viewers.py @@ -59,7 +59,7 @@ def test_several_data_viewers( @pytest.mark.usefixtures("aiida_profile_clean") -def test_structure_data_viwer(structure_data_object): +def test_structure_data_viewer(structure_data_object): v = viewers.viewer(structure_data_object) assert isinstance(v, viewers.StructureDataViewer)