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] More precise annotation for nested tuples#14287

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 merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromAckslD:networkx-nested-tuple

Conversation

AckslD
Copy link
Contributor

Followup to#14218 based on the suggestion by@Avasam.


from networkx.classes.graph import Graph, _Node
from networkx.exception import NetworkXException
from networkx.utils.backends import _dispatchable

__all__ = ["from_nested_tuple", "from_prufer_sequence", "NotATree", "to_nested_tuple", "to_prufer_sequence"]

_NestedTuple: TypeAlias = tuple[_NestedTuple, ...]
Copy link
Member

@brianschubertbrianschubertJun 16, 2025
edited
Loading

Choose a reason for hiding this comment

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

Should the RHS include a union with some other type? This currently only allows tuples with tuple entries (ultimately terminating with empty tuples)

Huh, apparently that's correct:
https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.tree.coding.from_nested_tuple.html#from-nested-tuple

Nevermind then! Sorry for the noise :-)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Unless my understanding is wrong, there should indeed be no other types but only nested tuples:
https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.tree.coding.from_nested_tuple.html

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

No worries, thanks for taking a look :)

brianschubert reacted with thumbs up emoji
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was gonna ask the same question. And made the same assumption in my initial comment. Could you add a comment here pointing to that documentation ?

Suggested change
_NestedTuple:TypeAlias=tuple[_NestedTuple, ...]
# > The tree with one node and no edges is represented by the emptytuple, ()
# https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.tree.coding.from_nested_tuple.html#from-nested-tuple
# So this really only recuses on tuples
_NestedTuple:TypeAlias=tuple[_NestedTuple, ...]

Feel free to reword.

(as a side note, you could write_NestedTuple: TypeAlias = tuple[_NestedTuple | tuple[()], ...], but that quickly becomes unusable/too unwieldy)

Otherwise LGTM.

@github-actionsGitHub Actions

This comment has been minimized.

Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
@github-actionsGitHub Actions
Copy link
Contributor

According tomypy_primer, this change has no effect on the checked open source code. 🤖🎉


from networkx.classes.graph import Graph, _Node
from networkx.exception import NetworkXException
from networkx.utils.backends import _dispatchable

__all__ = ["from_nested_tuple", "from_prufer_sequence", "NotATree", "to_nested_tuple", "to_prufer_sequence"]

_NestedTuple: TypeAlias = tuple[_NestedTuple, ...]

class NotATree(NetworkXException): ...

@_dispatchable
def to_nested_tuple(T: Graph[_Node], root: _Node, canonical_form: bool = False): ...

Choose a reason for hiding this comment

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

While we're here...

Suggested change
defto_nested_tuple(T:Graph[_Node],root:_Node,canonical_form:bool=False): ...
defto_nested_tuple(T:Graph[_Node],root:_Node,canonical_form:bool=False)->_NestedTuple: ...

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Sounds good, I wasn't sure if the return hints were left out for a reason


from networkx.classes.graph import Graph, _Node
from networkx.exception import NetworkXException
from networkx.utils.backends import _dispatchable

__all__ = ["from_nested_tuple", "from_prufer_sequence", "NotATree", "to_nested_tuple", "to_prufer_sequence"]

_NestedTuple: TypeAlias = tuple[_NestedTuple, ...]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was gonna ask the same question. And made the same assumption in my initial comment. Could you add a comment here pointing to that documentation ?

Suggested change
_NestedTuple:TypeAlias=tuple[_NestedTuple, ...]
# > The tree with one node and no edges is represented by the emptytuple, ()
# https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.tree.coding.from_nested_tuple.html#from-nested-tuple
# So this really only recuses on tuples
_NestedTuple:TypeAlias=tuple[_NestedTuple, ...]

Feel free to reword.

(as a side note, you could write_NestedTuple: TypeAlias = tuple[_NestedTuple | tuple[()], ...], but that quickly becomes unusable/too unwieldy)

Otherwise LGTM.

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

@AvasamAvasamAvasam requested changes

@brianschubertbrianschubertbrianschubert approved these 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.

3 participants
@AckslD@Avasam@brianschubert

[8]ページ先頭

©2009-2025 Movatter.jp