Repository: networkx/networkx (Python)

Issue:
Exception not raised when `to_graph6_bytes` is used on directed graphs
When I use `networkx.readwrite.graph6.to_graph6_bytes` with a **directed** graph, I expect a `NetworkXNotImplemented` exception to be raised (because the graph6 format is for undirected graphs only). This is also stated in the method docstring:
```
Raises
------
NetworkXNotImplemented
    If the graph is directed or is a multigraph.
```


### Current Behavior
```
>>> import networkx as nx
>>> nx.to_graph6_bytes(nx.DiGraph())
b'>>graph6<<?\n'
```

### Expected Behavior
```
>>> import networkx as nx
>>> nx.to_graph6_bytes(nx.DiGraph())
...
networkx.exception.NetworkXNotImplemented: not implemented for directed type
```

### Environment
<!--- Please provide details about your local environment -->
Python version: 3.10.4
NetworkX version: 2.7.2rc1.dev0


### Additional context
Here you can find the description of the graph6 format: [http://users.cecs.anu.edu.au/~bdm/data/formats.txt](url)



Discussion:
I'll work on this issue. I'll also add a few tests (there are no tests for the `to_graph6_bytes` method



Patch:
diff --git a/networkx/readwrite/graph6.py b/networkx/readwrite/graph6.py
index 85aa25424a0..886ba299fe9 100644
--- a/networkx/readwrite/graph6.py
+++ b/networkx/readwrite/graph6.py
@@ -128,6 +128,8 @@ def bits():
     return G
 
 
+@not_implemented_for("directed")
+@not_implemented_for("multigraph")
 def to_graph6_bytes(G, nodes=None, header=True):
     """Convert a simple undirected graph to bytes in graph6 format.
 


Test patch:
diff --git a/networkx/readwrite/tests/test_graph6.py b/networkx/readwrite/tests/test_graph6.py
index ec9c1347e31..065fbb50253 100644
--- a/networkx/readwrite/tests/test_graph6.py
+++ b/networkx/readwrite/tests/test_graph6.py
@@ -79,9 +79,10 @@ def test_complete_bipartite_graph(self):
         # The expected encoding here was verified by Sage.
         assert result.getvalue() == b"N??F~z{~Fw^_~?~?^_?\n"
 
-    def test_no_directed_graphs(self):
+    @pytest.mark.parametrize("G", (nx.MultiGraph(), nx.DiGraph()))
+    def test_no_directed_or_multi_graphs(self, G):
         with pytest.raises(nx.NetworkXNotImplemented):
-            nx.write_graph6(nx.DiGraph(), BytesIO())
+            nx.write_graph6(G, BytesIO())
 
     def test_length(self):
         for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
@@ -108,10 +109,60 @@ def test_write_path(self):
             f.seek(0)
             assert f.read() == b">>graph6<<?\n"
 
-    def test_relabeling(self):
-        G = nx.Graph([(0, 1)])
-        assert g6.to_graph6_bytes(G) == b">>graph6<<A_\n"
-        G = nx.Graph([(1, 2)])
-        assert g6.to_graph6_bytes(G) == b">>graph6<<A_\n"
-        G = nx.Graph([(1, 42)])
+    @pytest.mark.parametrize("edge", ((0, 1), (1, 2), (1, 42)))
+    def test_relabeling(self, edge):
+        G = nx.Graph([edge])
+        f = BytesIO()
+        nx.write_graph6(G, f)
+        f.seek(0)
+        assert f.read() == b">>graph6<<A_\n"
+
+
+class TestToGraph6Bytes:
+    def test_null_graph(self):
+        G = nx.null_graph()
+        assert g6.to_graph6_bytes(G) == b">>graph6<<?\n"
+
+    def test_trivial_graph(self):
+        G = nx.trivial_graph()
+        assert g6.to_graph6_bytes(G) == b">>graph6<<@\n"
+
+    def test_complete_graph(self):
+        assert g6.to_graph6_bytes(nx.complete_graph(4)) == b">>graph6<<C~\n"
+
+    def test_large_complete_graph(self):
+        G = nx.complete_graph(67)
+        assert g6.to_graph6_bytes(G, header=False) == b"~?@B" + b"~" * 368 + b"w\n"
+
+    def test_no_header(self):
+        G = nx.complete_graph(4)
+        assert g6.to_graph6_bytes(G, header=False) == b"C~\n"
+
+    def test_complete_bipartite_graph(self):
+        G = nx.complete_bipartite_graph(6, 9)
+        assert g6.to_graph6_bytes(G, header=False) == b"N??F~z{~Fw^_~?~?^_?\n"
+
+    @pytest.mark.parametrize("G", (nx.MultiGraph(), nx.DiGraph()))
+    def test_no_directed_or_multi_graphs(self, G):
+        with pytest.raises(nx.NetworkXNotImplemented):
+            g6.to_graph6_bytes(G)
+
+    def test_length(self):
+        for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
+            G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
+            # Strip the trailing newline.
+            gstr = g6.to_graph6_bytes(G, header=False).rstrip()
+            assert len(gstr) == ((i - 1) * i // 2 + 5) // 6 + (1 if i < 63 else 4)
+
+    def test_roundtrip(self):
+        for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
+            G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
+            data = g6.to_graph6_bytes(G)
+            H = nx.from_graph6_bytes(data.rstrip())
+            assert nodes_equal(G.nodes(), H.nodes())
+            assert edges_equal(G.edges(), H.edges())
+
+    @pytest.mark.parametrize("edge", ((0, 1), (1, 2), (1, 42)))
+    def test_relabeling(self, edge):
+        G = nx.Graph([edge])
         assert g6.to_graph6_bytes(G) == b">>graph6<<A_\n"