Skip to content

Enhancement: Use MultiDiGraph[Any] in type annotations for better generic compatibility #1364

@guibar

Description

@guibar

Contributing guidelines

  • I understand the contributing guidelines

Documentation

  • My proposal is not addressed by the documentation or examples

Existing issues

  • Nothing similar appears in an existing issue

What problem does your feature proposal solve?

Description

When using strict type checkers like pyright/basedpyright, functions in osmnx.distance (and likely elsewhere) that accept nx.MultiDiGraph parameters cause "partially unknown type" warnings because the MultiDiGraph annotation doesn't include a generic type parameter.

Example

from networkx import MultiDiGraph
from osmnx.distance import nearest_nodes

# When the graph is properly typed:
graph: MultiDiGraph[int] = ...

# This triggers a type warning because osmnx expects MultiDiGraph (unparameterized)
# but we're passing MultiDiGraph[int]

nearest_nodes(graph, 1.0, 2.0)

Error Message (from basedpyright)

Type of "nearest_nodes" is partially unknown
  Type of "nearest_nodes" is "Overload[(G: MultiDiGraph[Unknown], X: float, Y: float) -> int, ...]"

What is your proposed solution?

Suggested Fix

Update the type annotations to use MultiDiGraph[Any] instead of bare MultiDiGraph:

# Current (in distance.py)
def nearest_nodes(G: nx.MultiDiGraph, X: float, Y: float) -> int: ...

# Suggested
from typing import Any
def nearest_nodes(G: nx.MultiDiGraph[Any], X: float, Y: float) -> int: ...

This would allow users with typed graphs (MultiDiGraph[int], MultiDiGraph[str], etc.) to pass them without type errors.

What alternatives have you considered?

The alternative is to use ignore directives.

Additional context

Environment

  • osmnx version: 2.0.6
  • Python version: 3.12
  • Type checker: basedpyright

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions