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

Improvements for rustworkx.visit annotations #1362

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions rustworkx/rustworkx.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ from collections.abc import (
from abc import ABC
from rustworkx import generators # noqa

# from collections.abc import Sequence as SequenceCollection
from typing_extensions import Self

import numpy as np
import sys

Expand All @@ -42,6 +39,11 @@ if sys.version_info >= (3, 13):
else:
from typing_extensions import TypeVar

if sys.version_info >= (3, 11):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is not strictly necessary but I noticed we could refactor this to avoid depending on typing_extension for 3.13

from typing import Self
else:
from typing_extensions import Self

_S = TypeVar("_S", default=Any)
_T = TypeVar("_T", default=Any)

Expand Down Expand Up @@ -974,41 +976,41 @@ def graph_transitivity(graph: PyGraph, /) -> float: ...

# Traversal

_BFSVisitor = TypeVar("_BFSVisitor", bound=BFSVisitor)
_DFSVisitor = TypeVar("_DFSVisitor", bound=DFSVisitor)
_DijkstraVisitor = TypeVar("_DijkstraVisitor", bound=DijkstraVisitor)
_BFSVisitor = TypeVar("_BFSVisitor", bound=BFSVisitor, default=BFSVisitor)
_DFSVisitor = TypeVar("_DFSVisitor", bound=DFSVisitor, default=DFSVisitor)
_DijkstraVisitor = TypeVar("_DijkstraVisitor", bound=DijkstraVisitor, default=DijkstraVisitor)

def digraph_bfs_search(
graph: PyDiGraph,
source: Sequence[int] | None = ...,
visitor: _BFSVisitor | None = ...,
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
visitor: _BFSVisitor = ...,
) -> None: ...
def graph_bfs_search(
graph: PyGraph,
source: Sequence[int] | None = ...,
visitor: _BFSVisitor | None = ...,
visitor: _BFSVisitor = ...,
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
) -> None: ...
def digraph_dfs_search(
graph: PyDiGraph,
source: Sequence[int] | None = ...,
visitor: _DFSVisitor | None = ...,
visitor: _DFSVisitor = ...,
) -> None: ...
def graph_dfs_search(
graph: PyGraph,
source: Sequence[int] | None = ...,
visitor: _DFSVisitor | None = ...,
visitor: _DFSVisitor = ...,
) -> None: ...
def digraph_dijkstra_search(
graph: PyDiGraph,
source: Sequence[int] | None = ...,
weight_fn: Callable[[Any], float] | None = ...,
visitor: _DijkstraVisitor | None = ...,
visitor: _DijkstraVisitor = ...,
) -> None: ...
def graph_dijkstra_search(
graph: PyGraph,
source: Sequence[int] | None = ...,
weight_fn: Callable[[Any], float] | None = ...,
visitor: _DijkstraVisitor | None = ...,
visitor: _DijkstraVisitor = ...,
) -> None: ...
def digraph_dfs_edges(graph: PyDiGraph[_S, _T], /, source: int | None = ...) -> EdgeList: ...
def graph_dfs_edges(graph: PyGraph[_S, _T], /, source: int | None = ...) -> EdgeList: ...
Expand Down
9 changes: 8 additions & 1 deletion rustworkx/visit.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@

from typing import Any, Generic, TypeVar

import sys

if sys.version_info >= (3, 13):
from typing import TypeVar
else:
from typing_extensions import TypeVar

class StopSearch(Exception): ...
class PruneSearch(Exception): ...

_T = TypeVar("_T")
_T = TypeVar("_T", default=Any)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This silences pyright in strict mode like reported in #1352


class BFSVisitor(Generic[_T]):
def discover_vertex(self, v: int) -> Any: ...
Expand Down
Loading