traittypes icon indicating copy to clipboard operation
traittypes copied to clipboard

Can't use link with Array

Open shaielc opened this issue 3 years ago • 3 comments

Using link with Array for numpy array results in the exception:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

from traitlets import HasTraits, link
from traittypes import Array
import numpy as np

class Test(HasTraits):
    test = Array()
    test2 =Array()

test= Test()
link((test,"test"), (test,"test2"))
test.test =np.array([1,2,3])
traceback NOTE: I have edited the directories
Traceback (most recent call last):
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "userdir\.vscode\extensions\ms-python.python-2022.0.1814523869\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "userdir\.vscode\extensions\ms-python.python-2022.0.1814523869\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "userdir\.vscode\extensions\ms-python.python-2022.0.1814523869\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "projectdir\test.py", line 11, in <module>
    test.test =np.array([1,2,3])
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 606, in __set__
    self.set(obj, value)
  File "projectdir\.test_venv\lib\site-packages\traittypes\traittypes.py", line 112, in set
    obj._notify_trait(self.name, old_value, new_value)
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 1224, in _notify_trait
    type='change',
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 1229, in notify_change
    return self._notify_observers(change)
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 1266, in _notify_observers
    c(event)
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 296, in _update_target
    if getattr(self.source[0], self.source[1]) != change.new:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
pip list
Package             Version
------------------- ---------
anyio               3.4.0
argcomplete         1.12.3
argon2-cffi         21.1.0
attrs               21.2.0
Babel               2.9.1
backcall            0.2.0
bleach              4.1.0
certifi             2021.10.8
cffi                1.15.0
charset-normalizer  2.0.8
colorama            0.4.4
debugpy             1.5.1
decorator           5.1.0
defusedxml          0.7.1
entrypoints         0.3
idna                3.3
importlib-metadata  4.8.2
importlib-resources 5.4.0
ipykernel           6.6.0
ipython             7.30.0
ipython-genutils    0.2.0
ipywidgets          8.0.0a0
jedi                0.18.1
Jinja2              3.0.3
json5               0.9.6
jsonschema          4.2.1
jupyter-client      7.1.0
jupyter-core        4.9.1
jupyter-server      1.12.1
jupyterlab          3.2.4
jupyterlab-pygments 0.1.2
jupyterlab-server   2.8.2
jupyterlab-widgets  1.0.2
MarkupSafe          2.0.1
matplotlib-inline   0.1.3
mistune             0.8.4
nbclassic           0.3.4
nbclient            0.5.9
nbconvert           6.3.0
nbformat            5.1.3
nest-asyncio        1.5.4
notebook            6.4.6
numpy               1.21.5
packaging           21.3
pandocfilters       1.5.0
parso               0.8.3
pickleshare         0.7.5
pip                 21.3.1
prometheus-client   0.12.0
prompt-toolkit      3.0.23
pycparser           2.21
Pygments            2.10.0
pyparsing           3.0.6
pyrsistent          0.18.0
python-dateutil     2.8.2
pytz                2021.3
pywin32             302
pywinpty            1.1.6
pyzmq               22.3.0
requests            2.26.0
Send2Trash          1.8.0
setuptools          59.4.0
six                 1.16.0
sniffio             1.2.0
terminado           0.12.1
testpath            0.5.0
tornado             6.1
traitlets           5.1.1
traittypes          0.2.1
typing_extensions   4.0.1
urllib3             1.26.7
wcwidth             0.2.5
webencodings        0.5.1
websocket-client    1.2.1
widgetsnbextension  3.5.2
zipp                3.6.0

shaielc avatar Nov 18 '21 14:11 shaielc

Hi! Could you please provide the full stack trace for you error as well ?

vidartf avatar Feb 21 '22 18:02 vidartf

Hi! Could you please provide the full stack trace for you error as well ?

added the trace and pip list ( thought it may help )

shaielc avatar Feb 23 '22 09:02 shaielc

You are right. This seems like it broke with traitlets 5, after this PR https://github.com/ipython/traitlets/pull/393. I think you could suggest a fix to traitlets where it uses a similar logic in _update_target as for the set method: https://github.com/ipython/traitlets/blob/0a8333904253a72964dd5b07837f5747f5c7c89b/traitlets/traitlets.py#L695-L700

vidartf avatar Jul 14 '22 16:07 vidartf