Skip to content

Make types-networkx NumPy dependency optional - #16297

Merged
srittau merged 2 commits into
python:mainfrom
fly1d:codex/networkx-optional-numpy
Sep 3, 2026
Merged

Make types-networkx NumPy dependency optional#16297
srittau merged 2 commits into
python:mainfrom
fly1d:codex/networkx-optional-numpy

Conversation

@fly1d

@fly1d fly1d commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #14760

Move NumPy from the required stub dependency list to optional-dependencies. This keeps pure graph users from installing NumPy transitively while retaining a discoverable numpy extra for users of NumPy-backed APIs.

Use NumPy 1.21 as the optional dependency floor because these stubs import numpy.typing.NDArray, which was introduced in 1.21. The numpy>=2 requirement discussed in review is currently on unreleased NetworkX main; this PR targets NetworkX 3.6.1.

Validation:

  • python tests/check_typeshed_structure.py
  • python tests/get_external_stub_requirements.py networkx
  • pre-commit run --files stubs/networkx/METADATA.toml

Agent used: OpenAI Codex.

OpenAI Codex assisted with issue review, implementation, and verification. The change was reviewed before submission.
Comment thread stubs/networkx/METADATA.toml Outdated
Comment on lines +3 to +4
# requires a version of numpy with a `py.typed` file
dependencies = ["numpy>=1.20"]
optional-dependencies = ["numpy>=1.20"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

networkx requires numpy>=2 nowadays: https://github.com/networkx/networkx/blob/8e0b38e1a11335f07db13a13a6a5955eecf33408/pyproject.toml#L71

So maybe it's an idea to also update that here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. I checked both the released dependency and the typing API used by these stubs. This PR targets NetworkX 3.6.1, whose released default extra uses NumPy >=1.25; >=2 is currently only on unreleased main. For the typeshed metadata floor, the stubs import numpy.typing.NDArray, which was introduced in NumPy 1.21, matching the existing approach in #13538. I have therefore raised the optional dependency to >=1.21 in 142ce78. I am happy to adjust further if the maintainers prefer this extra to mirror the NetworkX runtime floor instead.

The stubs import numpy.typing.NDArray, which was introduced in NumPy 1.21. Keep the optional dependency aligned with the minimum typing API the stubs use rather than an unreleased NetworkX requirement.

OpenAI Codex assisted with review, implementation, and verification. The change was reviewed before submission.
@srittau
srittau merged commit 93a4ebd into python:main Sep 3, 2026
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make numpy dependency from types-networkx optional

3 participants