Skip to content

NetworkX stubs for AtlasView are incorrect #13513

Closed
@kveretennicov

Description

@kveretennicov

Repro:

from __future__ import annotations

from networkx import Graph
from typing import NewType, reveal_type

Node = NewType("Node", str)


g: Graph[Node] = Graph()
for s in ["spam", "bacon", "eggs"]:
    g.add_node(Node(s))

g.add_edge(Node("spam"), Node("bacon"), weight=1)
g.add_edge(Node("spam"), Node("eggs"), weight=10)

atlas = g[Node("spam")]

print(f"{atlas=}")
print(f"{dict(atlas.items())=}")

reveal_type(atlas)
reveal_type(dict(atlas.items()))

Running this produces

atlas=AtlasView({'bacon': {'weight': 1}, 'eggs': {'weight': 10}})
dict(atlas.items())={'bacon': {'weight': 1}, 'eggs': {'weight': 10}}

Atlas view for a node is basically a mapping of connected nodes to their attribute dicts.

Running mypy 1.15 over this produces

spam.py:21: note: Revealed type is "networkx.classes.coreviews.AtlasView[spam.Node, spam.Node, builtins.dict[builtins.str, Any]]"
spam.py:22: note: Revealed type is "builtins.dict[spam.Node, builtins.dict[spam.Node, builtins.dict[builtins.str, Any]]]"

Note how the static type for node's atlas view has an extra Node-keyed dict around the correct type.

The bold bits should not be there and contradict the actual runtime type: "builtins.dict[spam.Node, builtins.dict[spam.Node, builtins.dict[builtins.str, Any]]]"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions