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

Allow PyGraph and PyDiGraph to be annotated as generic classes at runtime #1348

Merged
merged 6 commits into from
Jan 14, 2025

Conversation

IvanIsCoding
Copy link
Collaborator

@IvanIsCoding IvanIsCoding commented Dec 30, 2024

Fixes #1345. It should address the core aspects for PyGraph and PyDiGraph. However, custom return types will come in a follow up PR

This makes the following work without requiring from __future__ import annotations:

import rustworkx as rx

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

Also, this is worth highlighting: I refrained from adding __class_getitem__ to the type stubs as in the stubs that method is inherited from Generic[_S, _T] I ended up adding the method to the stubs as mypy stub tests disagreed with my reasoning

@IvanIsCoding IvanIsCoding changed the title Allow PyGraph and PyDiGraph to be annotated as generic classes at run… Allow PyGraph and PyDiGraph to be annotated as generic classes at runtime Dec 30, 2024
@coveralls
Copy link

coveralls commented Dec 30, 2024

Pull Request Test Coverage Report for Build 12770141739

Details

  • 24 of 24 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.005%) to 95.83%

Totals Coverage Status
Change from base Build 12770043851: 0.005%
Covered Lines: 18384
Relevant Lines: 19184

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One quick question about the release note. But otherwise this lgtm it's a pretty straightforward feature and one I could see people using in typing contexts.

`PEP 560 <https://peps.python.org/pep-0560/>`__. Building off of the previous
releases which introduced type annotations, the following code snippet is now valid:

.. jupyter-execute::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be executed? It seems like a static code-block since we just need syntax highlighting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no preference either is fine. But I will say the advantage of executing is that it tests the code path.

If you execute that with 0.15.1 you’ll get a runtime error!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine leaving it. We're not super worried about our docs build time right now. If we were though this is the kind of thing we can change.

@mtreinish mtreinish added this pull request to the merge queue Jan 14, 2025
Merged via the queue into Qiskit:main with commit 830668b Jan 14, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add __class_getitem__ to generic classes
3 participants