Repository: scikit-hep/uproot5

Issue:
Writing TTrees is getting the branch type from the pattern of values, not the Awkward type
4.1.2

```python
>>> import uproot
>>> import awkward as ak
>>> f = uproot.recreate("whatever.root")
>>> a = ak.Array([[1, 2, 3], [4, 5, 6]])
>>> f["tree"] = {"branch": a}
>>> f["tree"].show()
name                 | typename                 | interpretation                
---------------------+--------------------------+-------------------------------
branch               | int64_t[3]               | AsDtype("('>i8', (3,))")
```

That should be a branch of type `int64_t[]` with a counter `nbranch` because the array type is

```
2 * var * int64
```

not

```
2 * 3 * int64
```

It doesn't matter that both lists happen to have the same length. (In a subsequent batch, they might not.)


Discussion:

Resolving patch:
diff --git a/src/uproot/compression.py b/src/uproot/compression.py
--- a/src/uproot/compression.py
+++ b/src/uproot/compression.py
@@ -130,7 +130,7 @@ def level(self, value):
     def compress(self, data):
         import zlib
 
-        return zlib.compress(data, level=self._level)
+        return zlib.compress(data, self._level)
 
 
 class _DecompressLZMA(object):
diff --git a/src/uproot/writing/identify.py b/src/uproot/writing/identify.py
--- a/src/uproot/writing/identify.py
+++ b/src/uproot/writing/identify.py
@@ -113,14 +113,15 @@ def add_to_directory(obj, name, directory, streamers):
                 metadata[branch_name] = metadatum
 
             else:
-                try:
-                    branch_array = uproot._util.ensure_numpy(branch_array)
-                except TypeError:
-                    module_name = type(branch_array).__module__
-                    if module_name == "awkward" or module_name.startswith("awkward."):
-                        data[branch_name] = branch_array
-                        metadata[branch_name] = branch_array.type
-                    else:
+                module_name = type(branch_array).__module__
+                if module_name == "awkward" or module_name.startswith("awkward."):
+                    data[branch_name] = branch_array
+                    metadata[branch_name] = branch_array.type
+
+                else:
+                    try:
+                        branch_array = uproot._util.ensure_numpy(branch_array)
+                    except TypeError:
                         try:
                             import awkward
                         except ImportError:
@@ -132,13 +133,14 @@ def add_to_directory(obj, name, directory, streamers):
                         else:
                             data[branch_name] = branch_array
                             metadata[branch_name] = awkward.type(branch_array)
-                else:
-                    data[branch_name] = branch_array
-                    branch_dtype = branch_array.dtype
-                    branch_shape = branch_array.shape[1:]
-                    if branch_shape != ():
-                        branch_dtype = numpy.dtype((branch_dtype, branch_shape))
-                    metadata[branch_name] = branch_dtype
+
+                    else:
+                        data[branch_name] = branch_array
+                        branch_dtype = branch_array.dtype
+                        branch_shape = branch_array.shape[1:]
+                        if branch_shape != ():
+                            branch_dtype = numpy.dtype((branch_dtype, branch_shape))
+                        metadata[branch_name] = branch_dtype
 
         else:
             is_ttree = True


Test patch:
diff --git a/tests/test_0439-check-awkward-before-numpy.py b/tests/test_0439-check-awkward-before-numpy.py
new file mode 100644
--- /dev/null
+++ b/tests/test_0439-check-awkward-before-numpy.py
@@ -0,0 +1,20 @@
+# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/main/LICENSE
+
+import os
+
+import numpy as np
+import pytest
+
+import uproot
+
+ak = pytest.importorskip("awkward")
+
+
+def test(tmp_path):
+    filename = os.path.join(str(tmp_path), "whatever.root")
+
+    with uproot.recreate(filename) as file:
+        file["tree"] = {"branch": ak.Array([[1, 2, 3], [4, 5, 6]])}
+
+    with uproot.open(filename) as file:
+        assert isinstance(file["tree/branch"].interpretation, uproot.AsJagged)