Fix graphviz_draw return type annotation and docstring (PIL.Image -> PIL.Image.Image)#1637
Open
juan52878911 wants to merge 1 commit into
Open
Fix graphviz_draw return type annotation and docstring (PIL.Image -> PIL.Image.Image)#1637juan52878911 wants to merge 1 commit into
juan52878911 wants to merge 1 commit into
Conversation
graphviz_draw was annotated and documented as returning the PIL.Image module instead of the PIL.Image.Image class. Because graphviz.py does `from PIL import Image`, `Image` is the module, so `-> Image | None` annotated the return with a module rather than the image class. Correct the runtime annotation to `Image.Image | None` and the `:returns:`/`:rtype:` docstring to `PIL.Image.Image`. The stub (graphviz.pyi) already imported the class and was correct, so it is left unchanged. Add tests/visualization/test_graphviz_typing.py with two guards: - a runtime check (get_type_hints + docstring) for the implementation, which is what actually regressed here since the stub shadows the implementation for static type checkers; and - a static assert_type check for every stub overload, wired into the `stubs` nox session via mypy. Fixes Qiskit#1357
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
graphviz_drawwas annotated and documented as returning thePIL.Imagemodule instead of thePIL.Image.Imageclass. This fixes the runtime annotation and the docstring, and adds regression tests.Fixes #1357.
Root cause
rustworkx/visualization/graphviz.pydoesfrom PIL import Image, so within that moduleImageis the modulePIL.Image, not the image class. The return annotation was therefore:which annotates the return value with a module. The docstring had the matching mistake (
:rtype: PIL.Image). This PR changes them toImage.Image | Noneand:rtype: PIL.Image.Image.Why the stub (
graphviz.pyi) is intentionally left untouchedThis is the non-obvious part, and the reason the fix touches
graphviz.pyand not the stub:from PIL.Image import Image(the class) and returnsImage, so it was already correct —git logshows it has imported the class since before enhance visualization func type hint #1443..pyistub exists, static type checkers use the stub and ignore the.pyimplementation. So the wrong annotation ingraphviz.pyis invisible to mypy/pyright at call sites, andpyright --verifytypes(the tool cited in graphviz typing error #1357) reads the already-correct stub.In other words, the specific
--verifytypessymptom from the original report (filed against 0.15.1) is no longer reproducible onmainat the stub level — but the implementation annotation and the rendered docstring were still wrong, which is what this PR fixes.How this was found
While adding a regression test, a negative control (reverting the fix and re-running the checks) surfaced the stub-vs-implementation split above: a
typing.assert_typetest kept passing even with the bug reintroduced, because it validates the stub. The bug only manifests when something reads the implementation's own annotation:After the fix,
get_type_hints(graphviz_draw)["return"]correctly resolves toPIL.Image.Image | None.Tests
tests/visualization/test_graphviz_typing.pyguards both fronts so neither can regress:get_type_hints+ docstring) over the implementation — this is what actually catches graphviz typing error #1357. Verified it fails without the fix (TypeErroron the annotation;AssertionErroron the:rtype:docstring) and passes with it.assert_typeunderTYPE_CHECKING) over every stub overload —filename=None → Image,filename=str → None, withimage_typeas both aLiteraland an arbitrarystr. Wired into the existingstubsnox session viamypyso CI enforces it.Verified locally against a fresh build (rustworkx
main, Pillow 12.3, mypy 1.17.1, pyright 1.1.411): runtime tests pass,mypy/pyrightclean on the typing test, andstubteststill passes.How this helps the project
.pydocstring, so the API reference forgraphviz_drawshowedPIL.Image(a module) as the return type. Users now see the correctPIL.Image.Image.typing.get_type_hints/inspect.signatureongraphviz_drawno longer crashes withTypeErrorand gets the right type.🤖 Generated with Claude Code