From 6501d327bf9987c9960bfd3afd44944a568bae4a Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Thu, 8 May 2025 16:43:44 +0800 Subject: [PATCH 1/8] enhance graphviz_draw type hint --- rustworkx/visualization/graphviz.pyi | 59 +++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/rustworkx/visualization/graphviz.pyi b/rustworkx/visualization/graphviz.pyi index f37bd5bcf4..76f4058d11 100644 --- a/rustworkx/visualization/graphviz.pyi +++ b/rustworkx/visualization/graphviz.pyi @@ -10,17 +10,66 @@ import typing from rustworkx.rustworkx import PyGraph, PyDiGraph if typing.TYPE_CHECKING: - from PIL import Image # type: ignore + from PIL.Image import Image + Method: typing.TypeAlias = typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] + ImageType: typing.TypeAlias = typing.Literal[ + "canon", + "cmap", + "cmapx", + "cmapx_np", + "dia", + "dot", + "fig", + "gd", + "gd2", + "gif", + "hpgl", + "imap", + "imap_np", + "ismap", + "jpe", + "jpeg", + "jpg", + "mif", + "mp", + "pcl", + "pdf", + "pic", + "plain", + "plain-ext", + "png", + "ps", + "ps2", + "svg", + "svgz", + "vml", + "vmlzvrml", + "vtx", + "wbmp", + "xdor", + "xlib", + ] _S = typing.TypeVar("_S") _T = typing.TypeVar("_T") +@typing.overload def graphviz_draw( graph: PyDiGraph[_S, _T] | PyGraph[_S, _T], node_attr_fn: typing.Callable[[_S], dict[str, str]] | None = ..., edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., graph_attr: dict[str, str] | None = ..., - filename: str | None = ..., - image_type: str | None = ..., - method: str | None = ..., -) -> Image | None: ... + filename: None = ..., + image_type: ImageType | None = ..., + method: Method | None = ..., +) -> Image: ... +@typing.overload +def graphviz_draw( + graph: PyDiGraph[_S, _T] | PyGraph[_S, _T], + node_attr_fn: typing.Callable[[_S], dict[str, str]] | None = ..., + edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., + graph_attr: dict[str, str] | None = ..., + filename: str = ..., + image_type: ImageType | None = ..., + method: Method | None = ..., +) -> None: ... From 11d4346703807d97d10f2fc0e68c78c2d74879ab Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Thu, 8 May 2025 16:48:49 +0800 Subject: [PATCH 2/8] enhance mpl_draw type hint --- rustworkx/visualization/matplotlib.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rustworkx/visualization/matplotlib.pyi b/rustworkx/visualization/matplotlib.pyi index 3f8da1fd17..38759fb6a1 100644 --- a/rustworkx/visualization/matplotlib.pyi +++ b/rustworkx/visualization/matplotlib.pyi @@ -20,7 +20,7 @@ if typing.TYPE_CHECKING: _S = typing.TypeVar("_S") _T = typing.TypeVar("_T") -class _DrawKwargs(typing.TypedDict, total=False): +class _DrawKwargs(typing.TypedDict, typing.Generic[_S, _T], total=False): arrowstyle: str arrow_size: int node_list: list[int] @@ -67,5 +67,5 @@ def mpl_draw( ax: Axes | None = ..., arrows: bool = ..., with_labels: bool = ..., - **kwds: typing_extensions.Unpack[_DrawKwargs], + **kwds: typing_extensions.Unpack[_DrawKwargs[_S, _T]], ) -> Figure | None: ... From d65797884e428e4f7e593cdf97f497e859f83254 Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Thu, 8 May 2025 17:14:41 +0800 Subject: [PATCH 3/8] fix black lint --- rustworkx/visualization/graphviz.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/rustworkx/visualization/graphviz.pyi b/rustworkx/visualization/graphviz.pyi index 76f4058d11..cf043bd3bf 100644 --- a/rustworkx/visualization/graphviz.pyi +++ b/rustworkx/visualization/graphviz.pyi @@ -11,6 +11,7 @@ from rustworkx.rustworkx import PyGraph, PyDiGraph if typing.TYPE_CHECKING: from PIL.Image import Image + Method: typing.TypeAlias = typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] ImageType: typing.TypeAlias = typing.Literal[ "canon", From 598c9d4b0dfbd94f95f0870e13a8f1e7168d728d Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Fri, 9 May 2025 10:22:23 +0800 Subject: [PATCH 4/8] fix black lint Signed-off-by: ZhengYu, Xu --- rustworkx/visualization/graphviz.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rustworkx/visualization/graphviz.pyi b/rustworkx/visualization/graphviz.pyi index cf043bd3bf..6b17e3c252 100644 --- a/rustworkx/visualization/graphviz.pyi +++ b/rustworkx/visualization/graphviz.pyi @@ -11,7 +11,7 @@ from rustworkx.rustworkx import PyGraph, PyDiGraph if typing.TYPE_CHECKING: from PIL.Image import Image - + Method: typing.TypeAlias = typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] ImageType: typing.TypeAlias = typing.Literal[ "canon", From 0fe0781a865ed97dff638fbcf16f15696e1f2293 Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Fri, 9 May 2025 13:53:04 +0800 Subject: [PATCH 5/8] make Method and ImageType private to satisfy mypy.stubtest --- rustworkx/visualization/graphviz.pyi | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/rustworkx/visualization/graphviz.pyi b/rustworkx/visualization/graphviz.pyi index 6b17e3c252..846f86b016 100644 --- a/rustworkx/visualization/graphviz.pyi +++ b/rustworkx/visualization/graphviz.pyi @@ -10,10 +10,11 @@ import typing from rustworkx.rustworkx import PyGraph, PyDiGraph if typing.TYPE_CHECKING: + from typing_extensions import TypeAlias from PIL.Image import Image - Method: typing.TypeAlias = typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] - ImageType: typing.TypeAlias = typing.Literal[ + _Method: TypeAlias = typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] + _ImageType: TypeAlias = typing.Literal[ "canon", "cmap", "cmapx", @@ -61,8 +62,8 @@ def graphviz_draw( edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., graph_attr: dict[str, str] | None = ..., filename: None = ..., - image_type: ImageType | None = ..., - method: Method | None = ..., + image_type: _ImageType | None = ..., + method: _Method | None = ..., ) -> Image: ... @typing.overload def graphviz_draw( @@ -71,6 +72,6 @@ def graphviz_draw( edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., graph_attr: dict[str, str] | None = ..., filename: str = ..., - image_type: ImageType | None = ..., - method: Method | None = ..., + image_type: _ImageType | None = ..., + method: _Method | None = ..., ) -> None: ... From 6076d8d97e4fb8e9808ceefc0ddbb1a62f7c5e19 Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Sat, 10 May 2025 11:37:10 +0800 Subject: [PATCH 6/8] allow image_type and method to accept str --- rustworkx/visualization/graphviz.pyi | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rustworkx/visualization/graphviz.pyi b/rustworkx/visualization/graphviz.pyi index 846f86b016..2f93c09c40 100644 --- a/rustworkx/visualization/graphviz.pyi +++ b/rustworkx/visualization/graphviz.pyi @@ -66,6 +66,16 @@ def graphviz_draw( method: _Method | None = ..., ) -> Image: ... @typing.overload +def graphviz_draw( + graph: PyDiGraph[_S, _T] | PyGraph[_S, _T], + node_attr_fn: typing.Callable[[_S], dict[str, str]] | None = ..., + edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., + graph_attr: dict[str, str] | None = ..., + filename: None = ..., + image_type: str | None = ..., + method: str | None = ..., +) -> Image: ... +@typing.overload def graphviz_draw( graph: PyDiGraph[_S, _T] | PyGraph[_S, _T], node_attr_fn: typing.Callable[[_S], dict[str, str]] | None = ..., @@ -75,3 +85,13 @@ def graphviz_draw( image_type: _ImageType | None = ..., method: _Method | None = ..., ) -> None: ... +@typing.overload +def graphviz_draw( + graph: PyDiGraph[_S, _T] | PyGraph[_S, _T], + node_attr_fn: typing.Callable[[_S], dict[str, str]] | None = ..., + edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., + graph_attr: dict[str, str] | None = ..., + filename: str = ..., + image_type: str | None = ..., + method: str | None = ..., +) -> None: ... From c71626e852d2010a9a90f1ef74fffcf73159b672 Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Sat, 10 May 2025 11:54:55 +0800 Subject: [PATCH 7/8] remove type alias --- rustworkx/visualization/graphviz.pyi | 70 +++++++++++++++++++++------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/rustworkx/visualization/graphviz.pyi b/rustworkx/visualization/graphviz.pyi index 2f93c09c40..66a6d4867f 100644 --- a/rustworkx/visualization/graphviz.pyi +++ b/rustworkx/visualization/graphviz.pyi @@ -10,11 +10,19 @@ import typing from rustworkx.rustworkx import PyGraph, PyDiGraph if typing.TYPE_CHECKING: - from typing_extensions import TypeAlias from PIL.Image import Image - _Method: TypeAlias = typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] - _ImageType: TypeAlias = typing.Literal[ +_S = typing.TypeVar("_S") +_T = typing.TypeVar("_T") + +@typing.overload +def graphviz_draw( + graph: PyDiGraph[_S, _T] | PyGraph[_S, _T], + node_attr_fn: typing.Callable[[_S], dict[str, str]] | None = ..., + edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., + graph_attr: dict[str, str] | None = ..., + filename: None = ..., + image_type: typing.Literal[ "canon", "cmap", "cmapx", @@ -51,19 +59,8 @@ if typing.TYPE_CHECKING: "xdor", "xlib", ] - -_S = typing.TypeVar("_S") -_T = typing.TypeVar("_T") - -@typing.overload -def graphviz_draw( - graph: PyDiGraph[_S, _T] | PyGraph[_S, _T], - node_attr_fn: typing.Callable[[_S], dict[str, str]] | None = ..., - edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., - graph_attr: dict[str, str] | None = ..., - filename: None = ..., - image_type: _ImageType | None = ..., - method: _Method | None = ..., + | None = ..., + method: typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] | None = ..., ) -> Image: ... @typing.overload def graphviz_draw( @@ -82,8 +79,45 @@ def graphviz_draw( edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., graph_attr: dict[str, str] | None = ..., filename: str = ..., - image_type: _ImageType | None = ..., - method: _Method | None = ..., + image_type: typing.Literal[ + "canon", + "cmap", + "cmapx", + "cmapx_np", + "dia", + "dot", + "fig", + "gd", + "gd2", + "gif", + "hpgl", + "imap", + "imap_np", + "ismap", + "jpe", + "jpeg", + "jpg", + "mif", + "mp", + "pcl", + "pdf", + "pic", + "plain", + "plain-ext", + "png", + "ps", + "ps2", + "svg", + "svgz", + "vml", + "vmlzvrml", + "vtx", + "wbmp", + "xdor", + "xlib", + ] + | None = ..., + method: typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] | None = ..., ) -> None: ... @typing.overload def graphviz_draw( From d0e45b1248b863da5c8eab936ef2e20950a1c0dc Mon Sep 17 00:00:00 2001 From: "ZhengYu, Xu" Date: Sun, 11 May 2025 15:27:34 +0800 Subject: [PATCH 8/8] format --- rustworkx/visualization/graphviz.pyi | 156 ++++++++++++++------------- 1 file changed, 80 insertions(+), 76 deletions(-) diff --git a/rustworkx/visualization/graphviz.pyi b/rustworkx/visualization/graphviz.pyi index 66a6d4867f..c5902b132d 100644 --- a/rustworkx/visualization/graphviz.pyi +++ b/rustworkx/visualization/graphviz.pyi @@ -22,44 +22,46 @@ def graphviz_draw( edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., graph_attr: dict[str, str] | None = ..., filename: None = ..., - image_type: typing.Literal[ - "canon", - "cmap", - "cmapx", - "cmapx_np", - "dia", - "dot", - "fig", - "gd", - "gd2", - "gif", - "hpgl", - "imap", - "imap_np", - "ismap", - "jpe", - "jpeg", - "jpg", - "mif", - "mp", - "pcl", - "pdf", - "pic", - "plain", - "plain-ext", - "png", - "ps", - "ps2", - "svg", - "svgz", - "vml", - "vmlzvrml", - "vtx", - "wbmp", - "xdor", - "xlib", - ] - | None = ..., + image_type: ( + typing.Literal[ + "canon", + "cmap", + "cmapx", + "cmapx_np", + "dia", + "dot", + "fig", + "gd", + "gd2", + "gif", + "hpgl", + "imap", + "imap_np", + "ismap", + "jpe", + "jpeg", + "jpg", + "mif", + "mp", + "pcl", + "pdf", + "pic", + "plain", + "plain-ext", + "png", + "ps", + "ps2", + "svg", + "svgz", + "vml", + "vmlzvrml", + "vtx", + "wbmp", + "xdor", + "xlib", + ] + | None + ) = ..., method: typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] | None = ..., ) -> Image: ... @typing.overload @@ -79,44 +81,46 @@ def graphviz_draw( edge_attr_fn: typing.Callable[[_T], dict[str, str]] | None = ..., graph_attr: dict[str, str] | None = ..., filename: str = ..., - image_type: typing.Literal[ - "canon", - "cmap", - "cmapx", - "cmapx_np", - "dia", - "dot", - "fig", - "gd", - "gd2", - "gif", - "hpgl", - "imap", - "imap_np", - "ismap", - "jpe", - "jpeg", - "jpg", - "mif", - "mp", - "pcl", - "pdf", - "pic", - "plain", - "plain-ext", - "png", - "ps", - "ps2", - "svg", - "svgz", - "vml", - "vmlzvrml", - "vtx", - "wbmp", - "xdor", - "xlib", - ] - | None = ..., + image_type: ( + typing.Literal[ + "canon", + "cmap", + "cmapx", + "cmapx_np", + "dia", + "dot", + "fig", + "gd", + "gd2", + "gif", + "hpgl", + "imap", + "imap_np", + "ismap", + "jpe", + "jpeg", + "jpg", + "mif", + "mp", + "pcl", + "pdf", + "pic", + "plain", + "plain-ext", + "png", + "ps", + "ps2", + "svg", + "svgz", + "vml", + "vmlzvrml", + "vtx", + "wbmp", + "xdor", + "xlib", + ] + | None + ) = ..., method: typing.Literal["twopi", "neato", "circo", "fdp", "sfdp", "dot"] | None = ..., ) -> None: ... @typing.overload