From db27831b649b3bf61e2a705f13a21d746bd6b07a Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Mon, 17 Jan 2022 18:02:46 -0500 Subject: [PATCH] fix flake8 discovered a few real errors in the tests doing this! Also setup isort to play nice with black --- ipycytoscape/__init__.py | 9 ++++++- ipycytoscape/cytoscape.py | 29 +++++++++++++---------- ipycytoscape/tests/_util.py | 19 +++++++++++++++ ipycytoscape/tests/test_graph_creation.py | 13 +--------- ipycytoscape/tests/test_graph_deletion.py | 6 +++++ ipycytoscape/tests/test_graph_methods.py | 22 ++++------------- pyproject.toml | 6 +++++ setup.cfg | 4 ++-- 8 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 ipycytoscape/tests/_util.py diff --git a/ipycytoscape/__init__.py b/ipycytoscape/__init__.py index 4092326c..20bddf4c 100644 --- a/ipycytoscape/__init__.py +++ b/ipycytoscape/__init__.py @@ -7,7 +7,14 @@ # The full license is in the file LICENSE, distributed with this software. from ._version import __version__, version_info -from .cytoscape import * +from .cytoscape import ( + MONITORED_USER_INTERACTIONS, + MONITORED_USER_TYPES, + CytoscapeWidget, + Edge, + Graph, + Node, +) from .nbextension import _jupyter_nbextension_paths npm_pkg_name = "jupyter-cytoscape" diff --git a/ipycytoscape/cytoscape.py b/ipycytoscape/cytoscape.py index ff425e41..76fb43d3 100644 --- a/ipycytoscape/cytoscape.py +++ b/ipycytoscape/cytoscape.py @@ -75,17 +75,17 @@ "vmousedown", # alias for 'tapstart' "tapdrag", # normalised move event (either touchmove or mousemove) "vmousemove", # alias for 'tapdrag' - "tapdragover", # normalised over element event (either touchmove or mousemove/mouseover) - "tapdragout", # normalised off of element event (either touchmove or mousemove/mouseout) + "tapdragover", # normalised over element event (either touchmove or mousemove/mouseover) # noqa + "tapdragout", # normalised off of element event (either touchmove or mousemove/mouseout) # noqa "tapend", # normalised tap end event (either mouseup or touchend) "vmouseup", # alias for 'tapend' - "tap", # normalised tap event (either click, or touchstart followed by touchend without touchmove) + "tap", # normalised tap event (either click, or touchstart followed by touchend without touchmove) # noqa "vclick", # alias for 'tap' "taphold", # normalised tap hold event "cxttapstart", # normalised right-click mousedown or two-finger tapstart "cxttapend", # normalised right-click mouseup or two-finger tapend "cxttap", # normalised right-click or two-finger tap - "cxtdrag", # normalised mousemove or two-finger drag after cxttapstart but before cxttapend + "cxtdrag", # normalised mousemove or two-finger drag after cxttapstart but before cxttapend # noqa "cxtdragover", # when going over a node via cxtdrag "cxtdragout", # when going off a node via cxtdrag "boxstart", # when starting box selection @@ -335,8 +335,9 @@ def add_edge(self, edge, directed=False, multiple_edges=False): def add_edges(self, edges, directed=False, multiple_edges=False): """ - Appends edges from the end of the list. If either the source or target Node of an - Edge is not already in the graph it will be created and added to he Nodes list. + Appends edges from the end of the list. If either the source or target Node + of an Edge is not already in the graph it will be created and added to + the Nodes list. Parameters ---------- @@ -354,13 +355,16 @@ def add_edges(self, edges, directed=False, multiple_edges=False): if multiple_edges and "multiple_edges" not in edge.classes: edge.classes += " multiple_edges " - # If multiple edges are allowed, it's okay to add more edges between the source and target + # If multiple edges are allowed, it's okay to add more + # edges between the source and target if multiple_edges: new_edge = True - # Check to see if the edge source -> target exists in the graph (don't add it again) + # Check to see if the edge source -> target exists in the graph + # If it does then don't add it again elif source in self._adj and target in self._adj[source]: new_edge = False - # Check to see if the edge target-> source exists in an undirected graph (don't add it again) + # Check to see if the edge target-> source exists in an + # undirected graph (don't add it again) elif not directed and target in self._adj and source in self._adj[target]: new_edge = False # If the edge doesn't exist already @@ -419,7 +423,8 @@ def remove_edge(self, edge): self._adj[target][source] -= 1 except ValueError: raise ValueError( - f"Edge from {edge.data['source']} to {edge.data['target']} is not present in the graph." + f"Edge from {edge.data['source']} to {edge.data['target']} " + "is not present in the graph." ) def remove_edge_by_id(self, source_id, target_id): @@ -717,7 +722,7 @@ def create_tooltip(node_attributes, node_labels): node_attributes = convert_types_to_string(node_attributes) # create tooltip text string - if not "tooltip" in node_attributes: + if "tooltip" not in node_attributes: tooltip_text = create_tooltip(node_attributes, node.labels) node_attributes["tooltip"] = tooltip_text @@ -750,7 +755,7 @@ def create_tooltip(node_attributes, node_labels): rel_attributes = convert_types_to_string(rel_attributes) # assign name of the relationship - if not "name" in rel_attributes: + if "name" not in rel_attributes: rel_attributes["name"] = rel.__class__.__name__ # assign unique node ids diff --git a/ipycytoscape/tests/_util.py b/ipycytoscape/tests/_util.py new file mode 100644 index 00000000..42b99d39 --- /dev/null +++ b/ipycytoscape/tests/_util.py @@ -0,0 +1,19 @@ +from typing import List + +from ipycytoscape.cytoscape import Edge, Node + + +def compare_nodes(expected_nodes: List[Node], actual_nodes: List[Node]): + # if one list is empty + assert bool(expected_nodes) == bool(actual_nodes) + for expected, actual in zip(expected_nodes, actual_nodes): + assert expected.data == actual.data + assert expected.classes == actual.classes + assert expected.position == actual.position + + +def compare_edges(expected_edges: List[Edge], actual_edges: List[Edge]): + assert bool(expected_edges) == bool(actual_edges) + for expected, actual in zip(expected_edges, actual_edges): + assert expected.data == actual.data + assert expected.classes == actual.classes diff --git a/ipycytoscape/tests/test_graph_creation.py b/ipycytoscape/tests/test_graph_creation.py index de700549..f49cf35e 100644 --- a/ipycytoscape/tests/test_graph_creation.py +++ b/ipycytoscape/tests/test_graph_creation.py @@ -11,18 +11,7 @@ from ipycytoscape.cytoscape import Edge, Graph, Node - -def compare_nodes(expected_nodes, actual_nodes): - for expected, actual in zip(expected_nodes, actual_nodes): - assert expected.data == actual.data - assert expected.classes == actual.classes - assert expected.position == actual.position - - -def compare_edges(expected_edges, actual_edges): - for expected, actual in zip(expected_edges, actual_edges): - assert expected.data == actual.data - assert expected.classes == actual.classes +from ._util import compare_edges, compare_nodes class TestNetworkx: diff --git a/ipycytoscape/tests/test_graph_deletion.py b/ipycytoscape/tests/test_graph_deletion.py index 6f150c0a..93337b5e 100644 --- a/ipycytoscape/tests/test_graph_deletion.py +++ b/ipycytoscape/tests/test_graph_deletion.py @@ -11,6 +11,8 @@ from ipycytoscape.cytoscape import Graph, Node +from ._util import compare_nodes + class TestNetworkx: def test_lonely_nodes(self): @@ -34,6 +36,7 @@ def test_lonely_nodes(self): Node(data={"id": "unconnected_node"}, position={}), ] + compare_nodes(expected_nodes, graph.nodes) # remove individual node using node as input graph.remove_node(graph.nodes[0]) @@ -44,6 +47,7 @@ def test_lonely_nodes(self): Node(data={"id": "4"}, position={}), Node(data={"id": "unconnected_node"}, position={}), ] + compare_nodes(expected_nodes, graph.nodes) # remove individual node using index node as input graph.remove_node_by_id("3") @@ -54,6 +58,8 @@ def test_lonely_nodes(self): Node(data={"id": "4"}, position={}), Node(data={"id": "unconnected_node"}, position={}), ] + compare_nodes(expected_nodes, graph.nodes) # remove all nodes of the graph graph.clear() + compare_nodes([], graph.nodes) diff --git a/ipycytoscape/tests/test_graph_methods.py b/ipycytoscape/tests/test_graph_methods.py index 79e209c2..0215ebe4 100644 --- a/ipycytoscape/tests/test_graph_methods.py +++ b/ipycytoscape/tests/test_graph_methods.py @@ -11,21 +11,7 @@ from ipycytoscape.cytoscape import Edge, Graph, Node - -def compare_nodes(expected_nodes, actual_nodes): - # if one list is empty - assert bool(expected_nodes) == bool(actual_nodes) - for expected, actual in zip(expected_nodes, actual_nodes): - assert expected.data == actual.data - assert expected.classes == actual.classes - assert expected.position == actual.position - - -def compare_edges(expected_edges, actual_edges): - assert bool(expected_edges) == bool(actual_edges) - for expected, actual in zip(expected_edges, actual_edges): - assert expected.data == actual.data - assert expected.classes == actual.classes +from ._util import compare_edges, compare_nodes @pytest.fixture(name="edges", scope="function") @@ -264,7 +250,8 @@ def test_add_node(self): def test_add_edges(self, edges): """ - Test to ensure that edges with the corresponding nodes will be added to the graph + Test to ensure that edges with the corresponding nodes will be + added to the graph. """ expected_nodes = [ @@ -336,7 +323,8 @@ def test_add_edges_multiple_edges(self, edges): def test_add_edges_2(self): """ - Test to ensure that an edge with the corresponding nodes will be added to the graph + Test to ensure that an edge with the corresponding nodes will be + added to the graph. """ edge = Edge(data={"source": "0", "target": "1"}) edge_inv = Edge(data={"source": "1", "target": "0"}) diff --git a/pyproject.toml b/pyproject.toml index 80cf1f7d..554c4dc2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,9 @@ [build-system] requires = ["jupyter_packaging~=0.7.0", "jupyterlab>=3.0.0rc4,==3.*", "setuptools>=40.8.0", "wheel"] build-backend = "setuptools.build_meta" + + +[tool.isort] +profile = "black" +src_paths = "ipycytoscape" +multi_line_output = 3 diff --git a/setup.cfg b/setup.cfg index 7b01d512..9012fef5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,12 +5,12 @@ universal=1 license_file = LICENSE [flake8] -exclude = docs, _version.py, .eggs, example +exclude = docs/*, _version.py, .eggs, example/* max-line-length = 88 docstring-convention = "numpy" extend-ignore = # See https://github.com/PyCQA/pycodestyle/issues/373 - E203, + E203, E302, E303 per-file-ignores = ipycytoscape/__init__.py:F401 ipycytoscape/nbextension/__init__.py:F401