python / python/typeshed

[networkx] Some functions don't accept valid Graph types

Open
#16,365 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stubs: false positive
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

I'm using networkx with the Generics introduced by and think I have found a bug.

For some context:
I'm trying to run a type checker on a class Foo that looks like so:

class Bar(Mapping[str, Any]):
    ...

class Foo(Graph[int, Bar]):
    node_attr_dict_factory = Bar
    ...

The key point being that Bar implements Mapping, but is not a dict.

networkx.draw(G: Graph[Node_], ...) doesn't accept this Foo graph, and gives a typing error with the current stubs.

In the transition to generic types for node and edge data, _NodeData and _EdgeData were given an upper bound of Mapping[str, Any] and a default of dict[str, Any]. (https://github.com/python/typeshed/pull/15660/changes)

With this, most functions that took a Graph[_Node] parameter before, were changed to take a Graph[_Node, _NodeData, _EdgeData]

However, some weren't changed, which means they effectively have a parameter with type: Graph[_Node, dict[str, Any], dict[str, Any].
Passing a Graph[_Node, Mapping[str, Any], dict[str, Any]] causes a type error.

Is there a reason that some functions weren't updated to use Graph[_Node, _NodeData, _EdgeData] @ThePiep ?
I see that there are a few other functions in the library that use Graph[_Node] still, but am unsure why.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the networkx stubs by locating functions that still use Graph[_Node] and compare them with signatures updated for generic node and edge data. Check the reported Foo example with a type checker, then update the relevant signatures so Mapping-based data is accepted and verify that the typing error is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.