Skip to content

complete networkx/...lowest_common_ancestors.pyi #14582

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

hunterhogan
Copy link
Contributor

@hunterhogan hunterhogan commented Aug 16, 2025

addresses #14499 follows #14509, #14569

added TypeVar for parameter default to class Graph: mirroring class MultiGraph


Deferred: Waiting for a stubtest fix: python/mypy#19689

… follows python#14509, python#14569

added TypeVar for parameter `default ` to class Graph: mirroring class MultiGraph

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thanks, a few remarks below.

This comment has been minimized.

Copy link
Contributor Author

@hunterhogan hunterhogan left a comment

Choose a reason for hiding this comment

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

Veni, vidi, submiti.

This comment has been minimized.

I tried a few things, nothing helped. I thought adding `networkx\.(algorithms\.)?(lowest_common_ancestors\.)?lowest_common_ancestor` to

https://github.com/python/typeshed/blob/d270bb0dc1df3fcfae2808c9a5a0f7ec802e1fb2/stubs/networkx/%40tests/stubtest_allowlist.txt#L1-L11

would resolve the problem, but it didn't.
Copy link
Contributor

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

discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
+ discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
- discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
+ discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
- discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/bot.py:291: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/bot.py:291: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/bot.py:315: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/bot.py:315: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]

@srittau
Copy link
Collaborator

srittau commented Aug 19, 2025

The changes LGTM. I'm a bit baffled why stubtest crashed. This is definitely a bug in stubtest that needs to be investigated. (Even if there was something wrong with the changes, it shouldn't crash, but print a proper error message.)

@brianschubert
Copy link
Member

Opened python/mypy#19689 to track the stubtest crash

@srittau srittau added the status: deferred Issue or PR deferred until some precondition is fixed label Aug 20, 2025
@srittau
Copy link
Collaborator

srittau commented Aug 20, 2025

I've marked this PR as deferred for now until we get a stubtest fix (or a workaround).

Comment on lines +17 to +22
@overload
@_dispatchable
def lowest_common_ancestor(G: DiGraph[_Node], node1: _Node, node2: _Node, default: None = None) -> _Node | None: ...
@overload
@_dispatchable
def lowest_common_ancestor(G: DiGraph[_Node], node1, node2, default=None): ...
def lowest_common_ancestor(G: DiGraph[_Node], node1: _Node, node2: _Node, default: _DefaultT) -> _Node | _DefaultT: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that @overload does not work with @_dispatchable (see #14462 for more information). TLDR, you have to remove the dispatchable decorator if you want to use overloads. Here is a patch that passes stubtest:

diff --git a/stubs/networkx/networkx/algorithms/lowest_common_ancestors.pyi b/stubs/networkx/networkx/algorithms/lowest_common_ancestors.pyi
index 7de4949cc..3244759f3 100644
--- a/stubs/networkx/networkx/algorithms/lowest_common_ancestors.pyi
+++ b/stubs/networkx/networkx/algorithms/lowest_common_ancestors.pyi
@@ -15,11 +15,13 @@ def all_pairs_lowest_common_ancestor(
     G: DiGraph[_Node], pairs: Iterable[tuple[_Node, _Node]] | None = None
 ) -> Generator[tuple[tuple[_Node, _Node], _Node | None]]: ...
 @overload
-@_dispatchable
-def lowest_common_ancestor(G: DiGraph[_Node], node1: _Node, node2: _Node, default: None = None) -> _Node | None: ...
+def lowest_common_ancestor(
+    G: DiGraph[_Node], node1: _Node, node2: _Node, default: None = None, *, backend: str | None = None, **backend_kwargs
+) -> _Node | None: ...
 @overload
-@_dispatchable
-def lowest_common_ancestor(G: DiGraph[_Node], node1: _Node, node2: _Node, default: _DefaultT) -> _Node | _DefaultT: ...
+def lowest_common_ancestor(
+    G: DiGraph[_Node], node1: _Node, node2: _Node, default: _DefaultT, *, backend: str | None = None, **backend_kwargs
+) -> _Node | _DefaultT: ...
 @_dispatchable
 def tree_all_pairs_lowest_common_ancestor(
     G: DiGraph[_Node], root: _Node | None = None, pairs: Iterable[tuple[_Node, _Node]] | None = None

@hunterhogan
Copy link
Contributor Author

1. Should the stub have @_dispatchable?

I am both ignorant of the answer and apathetic about the question.

2. @srittau wrote, "I'm a bit baffled why stubtest crashed."

I didn't realize stubtest was crashing, even though I saw the error screen at least 20 times. I merely thought stubtest was telling me to fix the stub.

3. @hamdanal wrote, "Note that @overload does not work with @_dispatchable (see #14462 for more information)."

I think that issue is described more generally in

mypy: python/mypy#16840
pyright: microsoft/pyright#7167

# overloaded class-decorators are not properly supported in type-checkers:
# - mypy: https://github.com/python/mypy/issues/16840
# - pyright: https://github.com/microsoft/pyright/issues/7167
networkx\.(algorithms\.)?(boundary\.)?edge_boundary
networkx\.(algorithms\.)?(bridges\.)?local_bridges
networkx\.(algorithms\.)?(clique\.)?node_clique_number
networkx\.(convert_matrix\.)?from_numpy_array
networkx\.(convert_matrix\.)?from_pandas_adjacency
networkx\.(convert_matrix\.)?from_pandas_edgelist
networkx\.(generators\.)?(random_clustered\.)?random_clustered_graph
networkx\.(relabel\.)?relabel_nodes

I think _dispatchable is a "class-dispatcher", but I've never seen that term before, so I could be wrong.

class _dispatchable:
    _is_testing = False


    def __new__(
...

Therefore, I tried adding

networkx\.(algorithms\.)?(lowest_common_ancestors\.)?lowest_common_ancestor

to https://github.com/python/typeshed/blob/d270bb0dc1df3fcfae2808c9a5a0f7ec802e1fb2/stubs/networkx/%40tests/stubtest_allowlist.txt, but either I didn't do it correctly or it didn't help.

4. Note that "stubtest_allowlist.txt" has a special section just for _dispatchable.

But I couldn't figure out if it was related or how to use the information to resolve the problem.

# Stubtest says: "runtime argument "backend" has a default value of type None, which is
# incompatible with stub argument type builtins.str. This is often caused by overloads
# failing to account for explicitly passing in the default value."
# Which is true, but would require some way of concatenating `backend` to ParamSpec.kwargs
networkx\.(utils\.)?(backends\.)?_dispatchable\.__call__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: deferred Issue or PR deferred until some precondition is fixed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stubtest crashes on class decorator in overloaded function
4 participants