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

PyGraph not typed correctly in VSCode, disallowing autocomplete #832

Closed
thomasaarholt opened this issue Mar 14, 2023 · 7 comments · Fixed by #1010
Closed

PyGraph not typed correctly in VSCode, disallowing autocomplete #832

thomasaarholt opened this issue Mar 14, 2023 · 7 comments · Fixed by #1010
Labels
bug Something isn't working

Comments

@thomasaarholt
Copy link

Information

  • rustworkx version: 0.12.1

  • Python version: 3.10.9

  • Rust version: Not installed, installed via wheel

  • Operating system:
    Debian Buster

What is the current behavior?

VSCode is unable to parse the type of PyGraph, see below.

Screenshot 2023-03-14 at 07 49 48

image

This makes it very difficult to write code, especially while getting used to a new API.

Autocompletion works in iPython, and assumedly, the Jupyter Notebook (both use Jedi).

What is the expected behavior?

VSCode should identify the type of the graph

Steps to reproduce the problem

In VSCode:

import rustworkx

graph = rustworkx.PyGraph()

a = graph.add_node("A")
@thomasaarholt thomasaarholt added the bug Something isn't working label Mar 14, 2023
@IvanIsCoding
Copy link
Collaborator

We just merged #401 which should fix that. It has not been released yet.

Would you mind installing from the main branch and sharing what you see?

@thomasaarholt
Copy link
Author

Wahey! It works great!

The typing here is very cool. Took me a moment to figure out, but its quite useful.
image

Two suggestions:

  • Rename the TypeVars S and T to NodeType and PayloadType
  • Rename the edge argument to payload.

@thomasaarholt
Copy link
Author

Hilariously, the above code doesn't actually run 😅

g: rx.PyGraph[tuple[int, int], float] = rx.PyGraph() # this typing is very cool

n1 = g.add_node((1,2))
n2 = g.add_node((3,4))

g.add_edge(n1, n2, 10)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[52], line 1
----> 1 g: rx.PyGraph[tuple[int, int], float] = rx.PyGraph() # this typing is very cool
      3 n1 = g.add_node((1,2))
      4 n2 = g.add_node((3,4))

TypeError: type 'rustworkx.PyGraph' is not subscriptable

Is this technically a bug? Without it, the node and payload types are Unknown:
image

image

@IvanIsCoding
Copy link
Collaborator

Hilariously, the above code doesn't actually run 😅

g: rx.PyGraph[tuple[int, int], float] = rx.PyGraph() # this typing is very cool

n1 = g.add_node((1,2))
n2 = g.add_node((3,4))

g.add_edge(n1, n2, 10)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[52], line 1
----> 1 g: rx.PyGraph[tuple[int, int], float] = rx.PyGraph() # this typing is very cool
      3 n1 = g.add_node((1,2))
      4 n2 = g.add_node((3,4))

TypeError: type 'rustworkx.PyGraph' is not subscriptable

Is this technically a bug? Without it, the node and payload types are Unknown: image

image

I think that is expected because you are using PyGraph with Any.

If you declare the variable specifying the PyGraph[int, int] type it will define the node and edge types instead of Any

@thomasaarholt
Copy link
Author

thomasaarholt commented Mar 16, 2023

See my previous comment - I can't index into PyGraph (not subscriptable). That's the error i get when I'm adding the type hint.

@IvanIsCoding
Copy link
Collaborator

See my previous comment - I can't index into PyGraph (not subscriptable). That's the error i get when I'm adding the type hint.

This is related to PEP 563, the following code works:

from __future__ import annotations

import rustworkx as rx

g: rx.PyGraph[tuple[int, int], float] = rx.PyGraph() # this typing is very cool

n1 = g.add_node((1,2))
n2 = g.add_node((3,4))

g.add_edge(n1, n2, 10)

@thomasaarholt
Copy link
Author

thomasaarholt commented May 17, 2023

Came back to this this week, and I now understand the error better! After installing from master, PyGraph is now correctly typed via being imported in [rustworkx.pyi](https://github.com/Qiskit/rustworkx/blob/main/rustworkx/rustworkx.pyi). Thank you for the helpful comments - the import from __future__ was very interesting to read about!

I've installed the master branch, but now I don't understand why, e.g. distance_matrix is not being imported into the namespace. It was my understanding that since it is defined in [__init__.py](https://github.com/Qiskit/rustworkx/blob/main/rustworkx/__init__.py), it should be found by the type checker. I even tried adding it to __all__ = ["distance_matrix"] (and reinstalling), without luck. I considered making a new issue, but this is strongly related to the original issue, so commented here.

Screenshot 2023-05-17 at 13 28 23

EDIT: Classic rubber ducky moment. The existence of __init__.pyi in the same directory overrides __init__.py completely for the type checker. Temporarily renaming that exposed distance_matrix, removing the screenshotted error.

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

Successfully merging a pull request may close this issue.

2 participants