question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

https://github.com/napari/napari/blob/255822fb17f25c79133ec40dba0e626b2bf66b20/napari/utils/events/evented_model.py#L202-L218

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:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tlambert03commented, Jun 11, 2021

I think the key here is the pick_equality_operator function

That 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 in pick_equality_operator?

0reactions
alisterburtcommented, Jun 13, 2021

@tlambert03 thanks a bunch for the detailed explanation, really appreciate it! 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found