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

PyDiGraph Type Hinting not working, TypeError: 'type' object is not subscriptable #1271

Closed
drauh opened this issue Aug 24, 2024 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@drauh
Copy link

drauh commented Aug 24, 2024

Information

  • rustworkx version: 0.15.1
  • Python version: 3.9, 3.11 and 3.10 (Colab)
  • Rust version: None
  • Operating system: Ubuntu 22.04 and Google Colab

What is the current behavior?

import rustworkx as rx

graph: rx.PyDiGraph[str, str] = rx.PyDiGraph()
import rustworkx as rx
----> graph: rx.PyDiGraph[str, str] = rx.PyDiGraph()

TypeError: 'type' object is not subscriptable

What is the expected behavior?

It seems that other users manage to use type hinting as shown in #1242. I can't understand why it doesn't work on both systems I tested, and I can't find anything in the documentation about it.

Steps to reproduce the problem

Go to Google Colab and run:

!pip install rustworkx[all]
import sys
print(sys.version)
!ldd --version
import rustworkx as rx

graph: rx.PyDiGraph[str, str] = rx.PyDiGraph()
@drauh drauh added the bug Something isn't working label Aug 24, 2024
@IvanIsCoding
Copy link
Collaborator

Try using from __future__ import annotations. This works fine:

from __future__ import annotations

import rustworkx as rx

graph: rx.PyDiGraph[str, str] = rx.PyDiGraph()

Notice that it works out of the box on newer Python versions.

@drauh
Copy link
Author

drauh commented Aug 24, 2024

Thank you very much for your answer. I apologize for not trying the from __future__ import annotations earlier. I can confirm that using this approach works on Python versions 3.9 through 3.12. However, without using the __future__ import, the code still doesn't work, even on Python 3.12.4 (I just created a brand new environment for testing this).
image
That said, this solution does indeed solve the problem for me. Thank you!

@IvanIsCoding
Copy link
Collaborator

IvanIsCoding commented Aug 25, 2024

I guess PEP 563 is not yet mandatory in any version so you are correct. All versions need the __future__ mechanism

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

2 participants