EventedModel is incompatible with dask delayed objects
See original GitHub issue🐛 Bug
napari.utils.events.EventedModel
instantiation fails if one of the attributes is a dask.Delayed
object. I’m not aware of this particular usage anywhere inside napari but it could feasibly come up so I thought it best to make an issue.
I know this is appearing as a pydantic issue here (not napari or EventedModel
specific) but in my use case the error appeared when equality checking two EventedModel
objects which had delayed attributes, not on instantiation.
I’m looking at a few ways to solve this now, for my use case simply removing the __eq__
override in EventedModel
works but this isn’t a good solution
To Reproduce
from dask import delayed
from dask.delayed import DelayedLeaf
from napari.utils.events import EventedModel
@delayed
def my_function():
pass
class MyObject(EventedModel):
attribute: DelayedLeaf = my_function
Traceback (most recent call last):
File "/Users/aburt/ccpem-projects/pp2d-relion/bug_report_eventedmodel_equality.py", line 10, in <module>
class MyObject(EventedModel):
File "/opt/homebrew/Caskroom/miniforge/base/envs/pp2d-relion/lib/python3.9/site-packages/napari/utils/events/evented_model.py", line 79, in __new__
cls = super().__new__(mcs, name, bases, namespace, **kwargs)
File "pydantic/main.py", line 299, in pydantic.main.ModelMetaclass.__new__
File "pydantic/fields.py", line 411, in pydantic.fields.ModelField.infer
File "pydantic/fields.py", line 342, in pydantic.fields.ModelField.__init__
File "pydantic/fields.py", line 445, in pydantic.fields.ModelField.prepare
File "pydantic/fields.py", line 473, in pydantic.fields.ModelField._set_default_and_type
File "pydantic/fields.py", line 345, in pydantic.fields.ModelField.get_default
File "pydantic/utils.py", line 627, in pydantic.utils.smart_deepcopy
File "/opt/homebrew/Caskroom/miniforge/base/envs/pp2d-relion/lib/python3.9/site-packages/dask/delayed.py", line 580, in __bool__
raise TypeError("Truth of Delayed objects is not supported")
TypeError: Truth of Delayed objects is not supported
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Make `EventedModel` compatible with `dask.Delayed` objects ...
napari: a fast, interactive, multi-dimensional image viewer for python - Make `EventedModel` compatible with `dask.Delayed` objects (#2879) ...
Read more >Dask Delayed - Dask documentation
The Dask delayed function decorates your functions so that they operate lazily. Rather than executing your function immediately, it will defer execution, ...
Read more >Dask inconsistent behavior when column order differs ...
It happened that the column order returned by the delayed objects didn't match the one provided in the meta . In that case,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think the key here is the
pick_equality_operator
functionThat function takes an object (which in this case would be a dask.Delayed object) and returns an appropriate function for checking equality. In “hard” cases like this, we simply return
operator.is_
, which just checks for object identity (probably the best we can do). The evented model then populates the equality checker for each type in the model on this line here:https://github.com/napari/napari/blob/609fd9f4cc54832c752d3691d7f16e15fbf86517/napari/utils/events/evented_model.py#L81-L82
So, to fix this, maybe just try adding
'dask.delayed.Delayed': operator.is_
to the_known_arrays
dict inpick_equality_operator
?@tlambert03 thanks a bunch for the detailed explanation, really appreciate it! 🙂