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

Finalize type signatures and enforce for all future functions #1061

Merged
merged 21 commits into from
Jan 21, 2024

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Jan 19, 2024

This commit adds the last few missing type hints (primarily the generators module) and switches the CI configuration over to enforce that the library is completely type hinted. This makes it a CI error if a function or method is added that doesn't include type hints. This is all built on the hard work of @IvanIsCoding who built up all the type hinting infrastructure and stub files for the majority of rustworkx. This is just the last piece to enforce the library is fully typed moving forward.

TODO:

  • Figure out how to teach mypy to discover the rustworkx.generators stubs correctly (and validate they're correct)
  • Fix .to_dot() signature so it doesn't error and that graphviz_draw()'s use of it passes mypy

This commit adds the last few missing type hints (primarily the
generators module) and switches the CI configuration over to enforce
that the library is completely type hinted. This makes it a CI error if
a function or method is added that doesn't include type hints. This is
all built on the hard work of @IvanIsCoding who built up all the type
hinting infrastructure and stub files for the majority of rustworkx.
This is just the last piece to enforce the library is fully typed moving
forward.

Co-authored-by: Ivan Carvalho <[email protected]>
@mtreinish mtreinish added this to the 0.14.0 milestone Jan 19, 2024
@coveralls
Copy link

coveralls commented Jan 19, 2024

Pull Request Test Coverage Report for Build 7601942772

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 95.942%

Totals Coverage Status
Change from base Build 7590700476: 0.0%
Covered Lines: 16100
Relevant Lines: 16781

💛 - Coveralls

@mtreinish mtreinish changed the title [WIP] Finalize type signatures and enforce for all future functions Finalize type signatures and enforce for all future functions Jan 19, 2024
This commit fixes most of the package for stubtest to work. This
primarily involves having the stubfiles match the package layout.
Unfortunately this means we need to put the majority of the type hints
in the stubfile for `rustworkx.rustworkx`. Having the stubfiles split up
as before mypy complains that the module doesn't exist. For example,
having the coloring functions in `rustworkx/coloring.pyi` causes a mypy
error that `rustworkx.coloring` couldn't be imported. This causes the
rustworkx.pyi file to be quite large, but I couldn't find a pattern
to workaround this limitation. Aside from that a bunch of stubs needed
to be updated as now that they're getting included in the stubtest run
issues were caught. The one exception is the generators module where the
custom `text_signature` set in the function definitions were incorrect
and tripping up mypy, these were fixed by removing the `text_signature`
fields.
Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

Thanks for wrapping this up! Just some thoughts before merging:

  • The re-exports are awful but mypy cannot find type-specific functions #960 was a real reported issue and mypy does not re-export symbols. None of our users import from rustworkx.rustworkx so I guess we need the duplication at the root
  • Maybe from __future__ import annotations fixes the errors in 3.8?
  • We should add a release note to celebrate 😃 🍾

label: str
connectionstyle: str

def mpl_draw(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is really awesome! I personally would have annotate just with Any because even matplotlib still lacks annotation suport nowadays

@mtreinish
Copy link
Member Author

The re-exports are awful but #960 was a real reported issue and mypy does not re-export symbols. None of our users import from rustworkx.rustworkx so I guess we need the duplication at the root

Do you think we can get away from a from .rustworkx import * in the __init__.pyi stub? Or will I have to list every single function with an as symbol?

@IvanIsCoding
Copy link
Collaborator

The re-exports are awful but #960 was a real reported issue and mypy does not re-export symbols. None of our users import from rustworkx.rustworkx so I guess we need the duplication at the root

Do you think we can get away from a from .rustworkx import * in the __init__.pyi stub? Or will I have to list every single function with an as symbol?

We need to re-export with the as symbol. If you import with the as it does not count unfortunately because mypy thinks it is not the intent

This commit adds back the re-exports to the root `__init__.pyi` stub file.
As was reported in Qiskit#960 the manual re-exports are necessary to ensure
that mypy can find symbols in the root of the package, where most people
use them, instead of off the inner `rustworkx.rustworkx` module.
@mtreinish
Copy link
Member Author

Maybe from future import annotations fixes the errors in 3.8?

ruff didn't like this saying it was unnecessary because it's implicit in a stub file. The only way I found to get this working was to add a # type: ignore comment to each of the imports (like I did for the pillow import).

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@mtreinish mtreinish added the automerge Queue a approved PR for merging label Jan 21, 2024
@mergify mergify bot merged commit 5344f68 into Qiskit:main Jan 21, 2024
26 checks passed
mtreinish added a commit to mtreinish/retworkx that referenced this pull request Jan 21, 2024
Since Qiskit#1061 has merged rustworkx is now fully typed with mypy. This
means that adding type annotations is no longer optional and is now
required for all new function. This commit updates the contribution
guidelines to indicate type annotations are required now. The details
are also updated a bit to provide a bit more detail on what's required
when adding type annotations to rustworkx.
@mtreinish mtreinish deleted the fully-typed branch January 21, 2024 15:33
mergify bot pushed a commit that referenced this pull request Jan 21, 2024
Since #1061 has merged rustworkx is now fully typed with mypy. This
means that adding type annotations is no longer optional and is now
required for all new function. This commit updates the contribution
guidelines to indicate type annotations are required now. The details
are also updated a bit to provide a bit more detail on what's required
when adding type annotations to rustworkx.

Co-authored-by: Ivan Carvalho <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Queue a approved PR for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants