Improve type coverage for networkx#16028
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| def sets(G: Graph[_Node], top_nodes: Iterable[Incomplete] | None = None) -> tuple[set[Incomplete], set[Incomplete]]: ... | ||
| @_dispatchable | ||
| def density(B: Graph[_Node], nodes) -> float: ... | ||
| def density(B: Graph[_Node], nodes: Collection[Incomplete]) -> float: ... |
There was a problem hiding this comment.
A representative example:
the docs say "list or container", typed as Collection because the implementation calls len()
https://networkx.org/documentation/stable/_modules/networkx/algorithms/bipartite/basic.html#density
in most cases the "list" params were widened to Iterable unless the implementation calls len(), in which case I widen to Collection
| def predecessor( | ||
| G: Graph[_Node], source: _Node, target: _Node | None = None, cutoff: int | None = None, return_seen: bool | None = None | ||
| ): ... | ||
| ) -> dict[_Node, list[_Node]] | list[_Node] | tuple[dict[_Node, list[_Node]], dict[_Node, int]] | tuple[list[_Node], int]: ... |
There was a problem hiding this comment.
this is pretty ugly, based on the docs we might want to split it into overloads but I think the dispatchable decorator won't work on those
| @_dispatchable | ||
| def k_core(G: Graph[_Node], k: int | None = None, core_number: Mapping[Incomplete, Incomplete] | None = None): ... | ||
| def k_core( | ||
| G: Graph[_Node], k: int | None = None, core_number: Mapping[Incomplete, Incomplete] | None = None |
There was a problem hiding this comment.
most inputs written in the docs as "dictionary" were widened to Mapping
| ) -> Graph[Incomplete]: ... | ||
| @_dispatchable | ||
| def chordal_cycle_graph(p, create_using=None): ... | ||
| def chordal_cycle_graph(p: int, create_using: Graph[Incomplete] | type[Graph[Incomplete]] | None = None) -> Graph[Incomplete]: ... |
There was a problem hiding this comment.
for where the docs say "NetworkX graph constructor" I mostly used type[Graph] | Graph, since the docstring says passing an instance is allowed too
if we wanted to widen even more we could allow Callable[..., Graph], but I'm not sure if we should
|
I did another pass on the changes. Would splitting it up into multiple PRs make this easier to review? |
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
The types in this PR were added using https://github.com/yangdanny97/docs2types, which extracts descriptions of arguments/returns from packages with numpy-style docs & uses AI to figure out what types those text descriptions correspond to.
Sometimes it's straightforward, like if the doc just says
booleanwe just putbool, but sometimes the doc will say something likea function, optionalwhich a LLM can understand. The added types are fairly conservative - I don't try to guess type arguments for containers, for the most part.I've made some manual tweaks on top of those changes. This technique was used for pandas-stubs last year and seemed to work fairly well.
Ignore the commit history on this branch, I forgot to pull when I started working on this PR.