Skip to content

Commit 7b870ea

Browse files
committed
Use new networkx type stubs from typeshed
Type stubs are not [officially available][1], so we use them instead of instruct `mypy` to ignore type hints for the `networkx` module. [1]: networkx/networkx#3988 (comment) Signed-off-by: Leandro Lucarella <[email protected]>
1 parent d81fdf1 commit 7b870ea

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

pyproject.toml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dev-mypy = [
7272
"types-Markdown == 3.9.0.20250906",
7373
"types-protobuf == 6.32.1.20250918",
7474
"types-setuptools == 80.9.0.20250822",
75+
"types-networkx == 3.5.0.20251106",
7576
# For checking the noxfile, docs/ script, and tests
7677
"frequenz-sdk[dev-mkdocs,dev-noxfile,dev-pytest]",
7778
]
@@ -205,13 +206,7 @@ files = ["src", "tests", "examples", "benchmarks", "docs", "noxfile.py"]
205206
strict = true
206207

207208
[[tool.mypy.overrides]]
208-
module = [
209-
"async_solipsism",
210-
"mkdocs_macros.*",
211-
# The available stubs packages are outdated or incomplete (WIP/experimental):
212-
# https://github.com/frequenz-floss/frequenz-sdk-python/issues/430
213-
"networkx",
214-
]
209+
module = ["async_solipsism", "mkdocs_macros.*"]
215210
ignore_missing_imports = true
216211

217212
[tool.setuptools_scm]

src/frequenz/sdk/microgrid/component_graph.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def __init__(
369369
InvalidGraphError: If `components` and `connections` are not both `None`
370370
and either of them is either `None` or empty.
371371
"""
372-
self._graph: nx.DiGraph = nx.DiGraph()
372+
self._graph: nx.DiGraph[ComponentId] = nx.DiGraph()
373373

374374
if components is None and connections is None:
375375
return
@@ -437,6 +437,7 @@ def connections(
437437
"""
438438
matching_sources = _comp_ids_to_iter(matching_sources)
439439
matching_destinations = _comp_ids_to_iter(matching_destinations)
440+
selection: Iterable[tuple[ComponentId, ComponentId]]
440441

441442
match (matching_sources, matching_destinations):
442443
case (None, None):
@@ -536,7 +537,7 @@ def refresh_from(
536537
if issues:
537538
raise InvalidGraphError(f"Invalid component data: {', '.join(issues)}")
538539

539-
new_graph = nx.DiGraph()
540+
new_graph: nx.DiGraph[ComponentId] = nx.DiGraph()
540541
new_graph.add_nodes_from(
541542
(component.id, {_DATA_KEY: component}) for component in components
542543
)

0 commit comments

Comments
 (0)