diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2022-09-08 10:49:36 -0700 |
---|---|---|
committer | Jarrod Millman <jarrod.millman@gmail.com> | 2022-09-30 09:55:02 -0700 |
commit | deda4c3f08f821888cb3b0ebc5e778a6cec55319 (patch) | |
tree | 9023f171b728b7dca999081ffe264d1bb36bf922 | |
parent | f1d03e9a853d5ebd68d099684c5c67cb9b886426 (diff) | |
download | networkx-deda4c3f08f821888cb3b0ebc5e778a6cec55319.tar.gz |
Minor docstring touchups and test refactor for `is_path` (#5967)
* Touch up docstring.
* Condense conditional.
* Minor refactor of ispath test - parametrize and rm redundant.
* Add release note.
* Update networkx/classes/function.py
Co-authored-by: Dan Schult <dschult@colgate.edu>
Co-authored-by: Dan Schult <dschult@colgate.edu>
-rw-r--r-- | doc/release/index.rst | 1 | ||||
-rw-r--r-- | doc/release/release_2.8.7.rst | 35 | ||||
-rw-r--r-- | networkx/classes/function.py | 12 | ||||
-rw-r--r-- | networkx/classes/tests/test_function.py | 22 |
4 files changed, 51 insertions, 19 deletions
diff --git a/doc/release/index.rst b/doc/release/index.rst index 5def917a..e7f9b7cd 100644 --- a/doc/release/index.rst +++ b/doc/release/index.rst @@ -15,6 +15,7 @@ period. :maxdepth: 2 release_dev + release_2.8.7 release_2.8.6 release_2.8.5 release_2.8.4 diff --git a/doc/release/release_2.8.7.rst b/doc/release/release_2.8.7.rst new file mode 100644 index 00000000..9f3cf3ca --- /dev/null +++ b/doc/release/release_2.8.7.rst @@ -0,0 +1,35 @@ +NetworkX 2.8.7 +============== + +Release date: TBD + +Supports Python 3.8, 3.9, and 3.10. + +NetworkX is a Python package for the creation, manipulation, and study of the +structure, dynamics, and functions of complex networks. + +For more information, please visit our `website <https://networkx.org/>`_ +and our :ref:`gallery of examples <examples_gallery>`. +Please send comments and questions to the `networkx-discuss mailing list +<http://groups.google.com/group/networkx-discuss>`_. + +Highlights +---------- + +Minor documentation and bug fixes. + +Merged PRs +---------- + + +Improvements +------------ + +- [`#5943 <https://github.com/networkx/networkx/pull/5943>`_] + ``is_path`` used to raise a `KeyError` when the ``path`` argument contained + a node that was not in the Graph. The behavior has been updated so that + ``is_path`` returns `False` in this case rather than raising the exception. + +Contributors +------------ + diff --git a/networkx/classes/function.py b/networkx/classes/function.py index a0807942..3750fd11 100644 --- a/networkx/classes/function.py +++ b/networkx/classes/function.py @@ -1289,19 +1289,17 @@ def is_path(G, path): G : graph A NetworkX graph. - path: list - A list of node labels which defines the path to traverse + path : list + A list of nodes which defines the path to traverse Returns ------- - isPath: bool - A boolean representing whether or not the path exists + bool + True if `path` is a valid path in `G` """ for node, nbr in nx.utils.pairwise(path): - if node not in G: - return False - if nbr not in G[node]: + if (node not in G) or (nbr not in G[node]): return False return True diff --git a/networkx/classes/tests/test_function.py b/networkx/classes/tests/test_function.py index df17f200..c738ab58 100644 --- a/networkx/classes/tests/test_function.py +++ b/networkx/classes/tests/test_function.py @@ -768,19 +768,17 @@ def test_pathweight(): pytest.raises(nx.NetworkXNoPath, nx.path_weight, graph, invalid_path, "cost") -def test_ispath(): +@pytest.mark.parametrize( + "G", (nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()) +) +def test_ispath(G): + G.add_edges_from([(1, 2), (2, 3), (1, 2), (3, 4)]) valid_path = [1, 2, 3, 4] - invalid_path = [1, 2, 4, 3] - another_invalid_path = [1, 2, 3, 4, 5] - yet_another_invalid_path = [1, 2, 5, 3, 4] - graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] - edges = [(1, 2), (2, 3), (1, 2), (3, 4)] - for graph in graphs: - graph.add_edges_from(edges) - assert nx.is_path(graph, valid_path) - assert not nx.is_path(graph, invalid_path) - assert not nx.is_path(graph, another_invalid_path) - assert not nx.is_path(graph, yet_another_invalid_path) + invalid_path = [1, 2, 4, 3] # wrong node order + another_invalid_path = [1, 2, 3, 4, 5] # contains node not in G + assert nx.is_path(G, valid_path) + assert not nx.is_path(G, invalid_path) + assert not nx.is_path(G, another_invalid_path) @pytest.mark.parametrize("G", (nx.Graph(), nx.DiGraph())) |