Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyCharm debugging leads to TypeError if add_xxx() is called #6789

Open
LucaMarconato opened this issue Mar 29, 2024 · 10 comments
Open

PyCharm debugging leads to TypeError if add_xxx() is called #6789

LucaMarconato opened this issue Mar 29, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@LucaMarconato
Copy link
Contributor

🐛 Bug Report

I used to be able to use the debugger in PyCharm for code using napari. Since around 2 weeks if I try to run a script in debug mode in PyCharm and the script calls one of the add_xxx functions, for instance add_points(), I receive a TypeError exception.

Running the script in Run mode works. Also the debugger works everywhere else. I am using the latest released napari.

The backtrace for the error I get is the following:

/Users/macbook/miniconda3/envs/ome/bin/python /Users/macbook/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 58587 --file /Users/macbook/embl/projects/basel/spatialdata/napari-spatialdata/temp/bug_points_scaling.py 
Connected to pydev debugger (build 231.9225.15)
Traceback (most recent call last):
  File "/Users/macbook/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/macbook/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/macbook/embl/projects/basel/spatialdata/napari-spatialdata/temp/bug_points_scaling.py", line 4, in <module>
    viewer.add_points([[10, 10]], size=[10], name="points_layer")
  File "/Users/macbook/miniconda3/envs/ome/lib/python3.10/site-packages/napari/components/viewer_model.py", line 7, in add_points
    from functools import lru_cache
TypeError: PostInit.__call__() got multiple values for argument 'self'
python-BaseExceptio

💡 Steps to Reproduce

  1. in PyCharm run the following script in Debug Mode
import napari

viewer = napari.Viewer()
viewer.add_points([[10, 10]], size=[10], name="points_layer")
napari.run()

💡 Expected Behavior

The script should execute normally.

🌎 Environment

napari: 0.4.19.post1
Platform: macOS-13.4.1-arm64-arm-64bit
System: MacOS 13.4.1
Python: 3.10.13 | packaged by conda-forge | (main, Oct 26 2023, 18:09:17) [Clang 16.0.6 ]
Qt: 5.15.8
PyQt5: 5.15.9
NumPy: 1.26.3
SciPy: 1.11.4
Dask: 2023.12.1
VisPy: 0.14.1
magicgui: 0.8.1
superqt: 0.6.1
in-n-out: 0.1.9
app-model: 0.2.4
npe2: 0.7.3

OpenGL:

  • GL version: 2.1 Metal - 83.1
  • MAX_TEXTURE_SIZE: 16384

Screens:

  • screen 1: resolution 3840x2160, scale 1.0
  • screen 2: resolution 3840x2160, scale 1.0

Settings path:

  • /Users/macbook/Library/Application Support/napari/ome_e544a94aa34edcbdceb29c82a6079cdcab084ea5/settings.yaml
    Plugins:
  • napari: 0.4.19.post1 (77 contributions)
  • napari-console: 0.0.9 (0 contributions)
  • napari-matplotlib: 1.2.0 (10 contributions)
  • napari-spatialdata: 0.3.2.dev10+g3c8dfd5 (6 contributions)
  • napari-svg: 0.1.10 (2 contributions)

💡 Additional Context

No response

@LucaMarconato LucaMarconato added the bug Something isn't working label Mar 29, 2024
@Czaki
Copy link
Collaborator

Czaki commented Mar 29, 2024

Hi @LucaMarconato. It looks really strange as self is popped form kwargs here:

kwargs.pop('self', None)

And a line referred by this error is

layer = {cls_name}(**kwargs)
(we need to fix this generation).

As I cannot reproduce this on my machine so I could only ask you to try to use debugger with steep by steep and see where is a problem, why self is not popped. With such information we could try to debug this.

@LucaMarconato
Copy link
Contributor Author

Thanks for the answer. I tried using the "Step Over" option in the PyCharm debugger. After 2 steps I get the error:
CleanShot 2024-03-29 at 12 59 41

@LucaMarconato
Copy link
Contributor Author

I tried adding a print(kwargs) before and after the kwargs.pop('self', None) and the self is removed correctly; still I am keeping getting the error.

@LucaMarconato
Copy link
Contributor Author

I haven't tried updating/reinstalling PyCharm or installing napari from main. I could try.

Czaki added a commit that referenced this issue Apr 8, 2024
…6791)

# References and relevant issues

The problem is visible in stacktrace from here
#6789 (comment)


# Description

Instead of using a string template and passing source file name, create
template file and pass the path to it to compile function.

On main:

```python
Traceback (most recent call last):
  File "/home/czaki/Projekty/napari/examples/add_points.py", line 22, in <module>
    viewer.add_points(points, size=size)
  File "/home/czaki/Projekty/napari/napari/components/viewer_model.py", line 7, in add_points
    from collections.abc import Iterator, Sequence
                ^^^^^^^^^^^^^^^^
  File "/home/czaki/Projekty/napari/napari/layers/base/base.py", line 112, in __call__
    raise ValueError("sample exception")
ValueError: sample exception
``` 

with this PR:

```python
Traceback (most recent call last):
  File "/home/czaki/Projekty/napari/examples/add_points.py", line 22, in <module>
    viewer.add_points(points, size=size)
  File "/home/czaki/Projekty/napari/napari/utils/add_layer.py_tmpl", line 7, in add_points
    layer = {cls_name}(**kwargs)
            ^^^^^^^^^^^^^^^^
  File "/home/czaki/Projekty/napari/napari/layers/base/base.py", line 112, in __call__
    raise ValueError("sample exception")
ValueError: sample exception
```

You can see that before this change, `from collections.abc import
Iterator, Sequence` points to an incorrect line of code regarding the
source of the error.
@LucaMarconato
Copy link
Contributor Author

Reinstalling napari from main didn't help, working on a fresh conda env neither. I ended up completely removing conda from my machine and reinstalling it, which worked. So now I am finally not experiencing this bug anymore.

@LucaMarconato
Copy link
Contributor Author

LucaMarconato commented May 6, 2024

I celebrated too early, the problem was not fixed, but now I managed to implement a workaround.

In details:

  • the problem actually still occurred, I just mistakenly didn't call add_xxxx when testing the new conda installation
  • I switched to main (note, I had to do a full install and not an editable install otherwise I was getting a different error upon opening napari)
  • Now that improve stack trace of autogenerated ViewerModel.add_xxxx methods #6791 is merged, I could see a debugger at add_layer.py_tmpl when calling add_xxxx(); I verified and kwargs contained self even after pop() was called.
  • I modified add_layer.py_tmpl to match the code below, and this workaround now the problem disappeared for me. I am reopening the issue.
def {name}{signature}:
    kwargs = locals()
    kwargs.pop('self', None)
    pos_kwargs = dict()
    for name in getattr({cls_name}.__init__, "_deprecated_constructor_args", []):
        pos_kwargs[name] = kwargs.pop(name, None)
    kwargs_workaround = dict()
    for key in kwargs.copy():
        if key not in ['self', 'kwargs_workaround', 'kwargs', 'pos_kwargs']:
            kwargs_workaround[key] = kwargs[key]
    layer = {cls_name}(**kwargs_workaround)
    for name, value in pos_kwargs.items():
        if value is not None:
            setattr(layer, name, value)
    self.layers.append(layer)
    return layer

# This file is created to improve user bug reports when
# user use `viewer.add_xxxx` and some error occurs.
# this allow to have more readable stacktrace.

@LucaMarconato LucaMarconato reopened this May 6, 2024
@Czaki
Copy link
Collaborator

Czaki commented May 6, 2024

What happens if you change kwargs = locals() to kwargs = dict(locals()) without your workaround?

@Czaki
Copy link
Collaborator

Czaki commented May 13, 2024

@LucaMarconato ?

@LucaMarconato
Copy link
Contributor Author

Good point! Yes, now it works, by applying the change you proposed in napari.utils._register.py.

@jni
Copy link
Member

jni commented May 30, 2024

@LucaMarconato would you like to open a PR? 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants