napari: Unsettable attributes and magicgui LayerDataTuple are incompatible

πŸ› Bug

To update inplace a result layer with a @magicgui decorated function, one should return a LayerDataTuple. However, this breaks if any of the attributes passed in the dictionary are not settable, such as ndim for Points.

To Reproduce

import napari
from napari.types import LayerDataTuple
from napari.layers import Points
from magicgui import magicgui

@magicgui()
def func(points: Points) -> LayerDataTuple:
    return (), dict(name='result', ndim=3), 'points'

v = napari.Viewer()
v.add_points()
v.window.add_dock_widget(func)
func()
func()

This fails with

~/git/napari/napari/utils/_magicgui.py in add_layer_data_tuples_to_viewer(gui, result, return_type)
    148                     layer.data = layer_datum[0]
    149                     for k, v in layer_datum[1].items():
--> 150                         setattr(layer, k, v)
    151                     continue
    152                 except KeyError:  # layer not in the viewer

AttributeError: can't set attribute

Expected behavior

I’m not sure about the best approach, but I feel like there should be no failure when the attribute (even if not settable) is already correct. In this case, ndim was 3, so setting it to 3 should just be a noop.

Environment

napari: 0.4.9rc1.dev12+gbf03a0d4
Platform: Linux-5.12.14-arch1-1-x86_64-with-glibc2.33
System: Arch Linux
Python: 3.9.6 (default, Jun 30 2021, 10:22:16) [GCC 11.1.0]
Qt: 5.15.2
PyQt5: 5.15.4
NumPy: 1.20.3
SciPy: 1.7.0
Dask: 2020.12.0
VisPy: 0.6.5.dev716+g91430ea4.d20210629

OpenGL:
- GL version: 4.6.0 NVIDIA 465.31
- MAX_TEXTURE_SIZE: 16384

Screens:
- screen 1: resolution 1920x1080, scale 1.0
- screen 2: resolution 1920x1200, scale 1.0

Plugins:
- animation: 0.0.2
- console: 0.0.3
- mrcfile-reader: 0.1.2
- napari-em-reader: 0.1.0
- napari-properties-plotter: 0.2.0
- ndtiffs: 0.1.1
- scikit-image: 0.4.9
- svg: 0.1.5

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 15 (14 by maintainers)

Most upvoted comments

That’s reasonable! Might be worth considering in #2995 then, which was prompted by similar issues.