summaryrefslogtreecommitdiff
path: root/networkx/algorithms/approximation
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2021-11-17 21:12:56 -0800
committerGitHub <noreply@github.com>2021-11-17 21:12:56 -0800
commit00c261f951ae6734001e0419d2bc754eef6dd1b2 (patch)
tree54e6ce86c89f3de9f2d960f4726025487216d3c8 /networkx/algorithms/approximation
parent4ebdb5e2ca6c6a1f9fb3bf8c2279f9b857c6d8a7 (diff)
downloadnetworkx-00c261f951ae6734001e0419d2bc754eef6dd1b2.tar.gz
Add Mypy type checking infrastructure (#5127)
* Add minimal mypy configuration file. * Add mypy workflow to GH. * Properly import sentinels from traversal.edgedfs. * mypy doesn't like variables named \"e\". * Rm annotations from single function. * Fix name collisions in test suite. Make sure all tests have unique names. * Rm unused random seed in test setup. * Rm redundant __all__ specification. * Silence mypy error from sum(). Mypy bug? * Fix tsp test instantiation nit. * \"type: ignore\" to suppress conditional fn sigature errors. * Remaining \"type: ignore\" to appease mypy. * Configure mypy to ignore inheritance issues. * Update exclude conf for CI. - Add yaml - Reformat regex containing reportviews * Rm partial annotations from lukes.py. Fixes mypy errors due to unannotated code. * Reorg defaultdict to reduce type: ignore cruft. * Homogenize signatures for fns defined in conditionals. * err as varname only in exception catching. * Fix name collision in Bellman-Ford test suite.
Diffstat (limited to 'networkx/algorithms/approximation')
-rw-r--r--networkx/algorithms/approximation/kcomponents.py2
-rw-r--r--networkx/algorithms/approximation/tests/test_traveling_salesman.py162
2 files changed, 81 insertions, 83 deletions
diff --git a/networkx/algorithms/approximation/kcomponents.py b/networkx/algorithms/approximation/kcomponents.py
index 12048da1..518a0274 100644
--- a/networkx/algorithms/approximation/kcomponents.py
+++ b/networkx/algorithms/approximation/kcomponents.py
@@ -213,7 +213,7 @@ class _AntiGraph(nx.Graph):
def single_edge_dict(self):
return self.all_edge_dict
- edge_attr_dict_factory = single_edge_dict
+ edge_attr_dict_factory = single_edge_dict # type: ignore
def __getitem__(self, n):
"""Returns a dict of neighbors of node n in the dense graph.
diff --git a/networkx/algorithms/approximation/tests/test_traveling_salesman.py b/networkx/algorithms/approximation/tests/test_traveling_salesman.py
index 56574f63..50d02b6c 100644
--- a/networkx/algorithms/approximation/tests/test_traveling_salesman.py
+++ b/networkx/algorithms/approximation/tests/test_traveling_salesman.py
@@ -39,83 +39,84 @@ def test_christofides_ignore_selfloops():
# set up graphs for other tests
-@classmethod
-def _setup_class(cls):
- cls.DG = nx.DiGraph()
- cls.DG.add_weighted_edges_from(
- {
- ("A", "B", 3),
- ("A", "C", 17),
- ("A", "D", 14),
- ("B", "A", 3),
- ("B", "C", 12),
- ("B", "D", 16),
- ("C", "A", 13),
- ("C", "B", 12),
- ("C", "D", 4),
- ("D", "A", 14),
- ("D", "B", 15),
- ("D", "C", 2),
- }
- )
- cls.DG_cycle = ["D", "C", "B", "A", "D"]
- cls.DG_cost = 31.0
-
- cls.DG2 = nx.DiGraph()
- cls.DG2.add_weighted_edges_from(
- {
- ("A", "B", 3),
- ("A", "C", 17),
- ("A", "D", 14),
- ("B", "A", 30),
- ("B", "C", 2),
- ("B", "D", 16),
- ("C", "A", 33),
- ("C", "B", 32),
- ("C", "D", 34),
- ("D", "A", 14),
- ("D", "B", 15),
- ("D", "C", 2),
- }
- )
- cls.DG2_cycle = ["D", "A", "B", "C", "D"]
- cls.DG2_cost = 53.0
-
- cls.unweightedUG = nx.complete_graph(5, nx.Graph())
- cls.unweightedDG = nx.complete_graph(5, nx.DiGraph())
-
- cls.incompleteUG = nx.Graph()
- cls.incompleteUG.add_weighted_edges_from({(0, 1, 1), (1, 2, 3)})
- cls.incompleteDG = nx.DiGraph()
- cls.incompleteDG.add_weighted_edges_from({(0, 1, 1), (1, 2, 3)})
-
- cls.UG = nx.Graph()
- cls.UG.add_weighted_edges_from(
- {
- ("A", "B", 3),
- ("A", "C", 17),
- ("A", "D", 14),
- ("B", "C", 12),
- ("B", "D", 16),
- ("C", "D", 4),
- }
- )
- cls.UG_cycle = ["D", "C", "B", "A", "D"]
- cls.UG_cost = 33.0
-
- cls.UG2 = nx.Graph()
- cls.UG2.add_weighted_edges_from(
- {
- ("A", "B", 1),
- ("A", "C", 15),
- ("A", "D", 5),
- ("B", "C", 16),
- ("B", "D", 8),
- ("C", "D", 3),
- }
- )
- cls.UG2_cycle = ["D", "C", "B", "A", "D"]
- cls.UG2_cost = 25.0
+class TestBase:
+ @classmethod
+ def setup_class(cls):
+ cls.DG = nx.DiGraph()
+ cls.DG.add_weighted_edges_from(
+ {
+ ("A", "B", 3),
+ ("A", "C", 17),
+ ("A", "D", 14),
+ ("B", "A", 3),
+ ("B", "C", 12),
+ ("B", "D", 16),
+ ("C", "A", 13),
+ ("C", "B", 12),
+ ("C", "D", 4),
+ ("D", "A", 14),
+ ("D", "B", 15),
+ ("D", "C", 2),
+ }
+ )
+ cls.DG_cycle = ["D", "C", "B", "A", "D"]
+ cls.DG_cost = 31.0
+
+ cls.DG2 = nx.DiGraph()
+ cls.DG2.add_weighted_edges_from(
+ {
+ ("A", "B", 3),
+ ("A", "C", 17),
+ ("A", "D", 14),
+ ("B", "A", 30),
+ ("B", "C", 2),
+ ("B", "D", 16),
+ ("C", "A", 33),
+ ("C", "B", 32),
+ ("C", "D", 34),
+ ("D", "A", 14),
+ ("D", "B", 15),
+ ("D", "C", 2),
+ }
+ )
+ cls.DG2_cycle = ["D", "A", "B", "C", "D"]
+ cls.DG2_cost = 53.0
+
+ cls.unweightedUG = nx.complete_graph(5, nx.Graph())
+ cls.unweightedDG = nx.complete_graph(5, nx.DiGraph())
+
+ cls.incompleteUG = nx.Graph()
+ cls.incompleteUG.add_weighted_edges_from({(0, 1, 1), (1, 2, 3)})
+ cls.incompleteDG = nx.DiGraph()
+ cls.incompleteDG.add_weighted_edges_from({(0, 1, 1), (1, 2, 3)})
+
+ cls.UG = nx.Graph()
+ cls.UG.add_weighted_edges_from(
+ {
+ ("A", "B", 3),
+ ("A", "C", 17),
+ ("A", "D", 14),
+ ("B", "C", 12),
+ ("B", "D", 16),
+ ("C", "D", 4),
+ }
+ )
+ cls.UG_cycle = ["D", "C", "B", "A", "D"]
+ cls.UG_cost = 33.0
+
+ cls.UG2 = nx.Graph()
+ cls.UG2.add_weighted_edges_from(
+ {
+ ("A", "B", 1),
+ ("A", "C", 15),
+ ("A", "D", 5),
+ ("B", "C", 16),
+ ("B", "D", 8),
+ ("C", "D", 3),
+ }
+ )
+ cls.UG2_cycle = ["D", "C", "B", "A", "D"]
+ cls.UG2_cost = 25.0
def validate_solution(soln, cost, exp_soln, exp_cost):
@@ -128,9 +129,7 @@ def validate_symmetric_solution(soln, cost, exp_soln, exp_cost):
assert cost == exp_cost
-class TestGreedyTSP:
- setup_class = _setup_class
-
+class TestGreedyTSP(TestBase):
def test_greedy(self):
cycle = nx_app.greedy_tsp(self.DG, source="D")
cost = sum(self.DG[n][nbr]["weight"] for n, nbr in pairwise(cycle))
@@ -170,8 +169,7 @@ class TestGreedyTSP:
assert len(cycle) - 1 == len(G) == len(set(cycle))
-class TestSimulatedAnnealingTSP:
- setup_class = _setup_class
+class TestSimulatedAnnealingTSP(TestBase):
tsp = staticmethod(nx_app.simulated_annealing_tsp)
def test_simulated_annealing_directed(self):