Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

networkx: fix subgraph and degree#14390

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
AckslD wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromAckslD:networkx-fixes

Conversation

AckslD
Copy link
Contributor

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

  1. Graph.subgraph can also take a single node.
  2. If egdegree takes a single node, the return type is different so addoverloads.

@AckslD
Copy link
ContributorAuthor

Hmm,degree works differently if the first argument isNone vs any otherHashable, not sure how to type that.

@github-actionsGitHub Actions

This comment has been minimized.

Copy link
Collaborator

@srittausrittau left a comment

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
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 aDiDegreeView, but could also return a sub-class if called on an instance of a sub-class. SoSelf 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
ContributorAuthor

Choose a reason for hiding this comment

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

good point, fixed

@@ -55,7 +55,10 @@ class NodeDataView(AbstractSet[_Node]):

class DiDegreeView(Generic[_Node]):
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
Collaborator

Choose a reason for hiding this comment

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

__getitem__ returnsfloat, so this should also returnfloat:

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

Copy link
ContributorAuthor

@AckslDAckslDJul 10, 2025
edited
Loading

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:

importnetworkxasnxg=nx.Graph()g.add_node('a')asserttype(g.degree('a'))isint

Copy link
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__ returnfloat (which is equivalent toint | float, so I was going by that. If you say that this will always beint, I'll believe you.

@AckslD
Copy link
ContributorAuthor

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

@github-actionsGitHub Actions
Copy link
Contributor

Diff frommypy_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
Copy link
Collaborator

@srittau I tried something whennbunch = 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@srittausrittausrittau requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@AckslD@srittau

[8]ページ先頭

©2009-2025 Movatter.jp