Skip to content

networkx: fix subgraph and degree#14390

Open
AckslD wants to merge 1 commit into
python:mainfrom
AckslD:networkx-fixes
Open

networkx: fix subgraph and degree#14390
AckslD wants to merge 1 commit into
python:mainfrom
AckslD:networkx-fixes

Conversation

@AckslD

@AckslD AckslD commented Jul 9, 2025

Copy link
Copy Markdown
Contributor

Two small fixes to networkx, let me know if it's better with separate PRs:

  1. Graph.subgraph can also take a single node.
  2. If eg degree takes a single node, the return type is different so add overloads.

@AckslD

AckslD commented Jul 9, 2025

Copy link
Copy Markdown
Contributor Author

Hmm, degree works differently if the first argument is None vs any other Hashable, not sure how to type that.

@github-actions

This comment has been minimized.

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, remarks below.

@overload
def __call__(self, nbunch: _Node, weight: None | bool | str = None) -> int: ...
@overload
def __call__(self, nbunch: Iterable[_Node] | None = None, weight: None | bool | str = None) -> DiDegreeView[_Node]: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Probably overly pedantic, but this doesn't necessarily return a DiDegreeView, but could also return a sub-class if called on an instance of a sub-class. So Self is more appropriate:

Suggested change
def __call__(self, nbunch: Iterable[_Node] | None = None, weight: None | bool | str = None) -> DiDegreeView[_Node]: ...
def __call__(self, nbunch: Iterable[_Node] | None = None, weight: None | bool | str = None) -> Self: ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good point, fixed

def __init__(self, G: Graph[_Node], nbunch: _NBunch[_Node] = None, weight: None | bool | str = None) -> None: ...
def __call__(self, nbunch: _NBunch[_Node] = None, weight: None | bool | str = None) -> int | DiDegreeView[_Node]: ...
@overload
def __call__(self, nbunch: _Node, weight: None | bool | str = None) -> int: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

__getitem__ returns float, so this should also return float:

Suggested change
def __call__(self, nbunch: _Node, weight: None | bool | str = None) -> int: ...
def __call__(self, nbunch: _Node, weight: None | bool | str = None) -> float: ...

@AckslD AckslD Jul 10, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm shouldn't the degree always be an integer, at least this passes:

import networkx as nx
g = nx.Graph()
g.add_node('a')
assert type(g.degree('a')) is int

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have no experience with networkx, so I can't say either way, I just noticed that __getitem__ return float (which is equivalent to int | float, so I was going by that. If you say that this will always be int, I'll believe you.

@AckslD

AckslD commented Jul 10, 2025

Copy link
Copy Markdown
Contributor Author

@srittau I tried something when nbunch = None but that also doesn't work, any suggestions how to distinguish that from other hashables?

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

bokeh (https://github.com/bokeh/bokeh)
+ src/bokeh/models/util/structure.py:165: note: ... from here:

@srittau

srittau commented Jul 11, 2025

Copy link
Copy Markdown
Collaborator

@srittau I tried something when nbunch = None but that also doesn't work, any suggestions how to distinguish that from other hashables?

We'll need a # type: ignore[overload-overlap] on the offending line. Unfortunately, mypy's overlap check is sometimes a bit over eager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants