Repository: dask-contrib/dask-awkward

Issue:
Cannot use `dak.num(events, axis=0)` after having computed a slice of the `events` when `open_files=False` during reading
To reproduce
```py
import uproot
import dask_awkward as dak

events = uproot.dask({"../coffea/tests/samples/nano_dy.root": "Events"}, open_files=False)
events[:10].compute()
dak.num(events, axis=0)
```
will raise
```
TypeError: unsupported dtype: dtype('O'). Must be one of

    bool, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64, complex64, complex128, datetime64, timedelta64, float16

or a datetime64/timedelta64 with units (e.g. 'datetime64[15us]')

This error occurred while calling

    ak.to_layout(
        TypeTracerArray-instance
        allow_record = False
        regulararray = False
        primitive_policy = 'error'
    )
```
Similarly in coffea:
```py
from coffea.nanoevents import NanoEventsFactory
import dask_awkward as dak

events = NanoEventsFactory.from_root({"../coffea/tests/samples/nano_dy.root": "Events"}, mode="dask").events()
events[:10].compute()
dak.num(events, axis=0)
```

Discussion:

Solution patch:
diff --git a/src/dask_awkward/lib/core.py b/src/dask_awkward/lib/core.py
index afed802..4139b2e 100644
--- a/src/dask_awkward/lib/core.py
+++ b/src/dask_awkward/lib/core.py
@@ -706,6 +706,8 @@ def new_known_scalar(
             dtype = np.dtype(int)
         elif isinstance(s, (float, np.floating)):
             dtype = np.dtype(float)
+        elif hasattr(s, "dtype"):
+            dtype = getattr(s, "dtype")
         else:
             dtype = np.dtype(type(s))
     else:
@@ -1434,7 +1436,7 @@ class Array(DaskMethodsMixin, NDArrayOperatorsMixin):
 
         if not self.known_divisions:
             self.eager_compute_divisions()
-        stop = sl.stop or self.defined_divisions[-1]
+        stop = self.defined_divisions[-1] if sl.stop is None else sl.stop
         start = start if start >= 0 else self.defined_divisions[-1] + start
         stop = stop if stop >= 0 else self.defined_divisions[-1] + stop
         if step < 0:


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index c34b7ae..0766a20 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -466,6 +466,14 @@ def test_new_known_scalar() -> None:
     assert c.compute() == s1
 
 
+def test_new_known_scalar_from_array() -> None:
+    s = np.array(0.0)
+    c = new_known_scalar(s)
+    assert c.compute() == s
+    assert c._meta is not None
+    assert c.dtype == s.dtype
+
+
 def test_scalar_dtype() -> None:
     s = 2
     c = new_known_scalar(s)
diff --git a/tests/test_getitem.py b/tests/test_getitem.py
index e7dd3f8..831b609 100644
--- a/tests/test_getitem.py
+++ b/tests/test_getitem.py
@@ -85,6 +85,11 @@ def test_single_ellipsis(daa: dak.Array, caa: ak.Array) -> None:
     assert_eq(daa[...], caa[...])
 
 
+def test_length0_slice(daa: dak.Array, caa: ak.Array) -> None:
+    assert_eq(daa[0:0], caa[0:0])
+    assert len(daa[0:0]) == len(caa[0:0]) == 0
+
+
 def test_empty_slice(daa: dak.Array, caa: ak.Array) -> None:
     assert_eq(daa[:], caa[:])
     assert_eq(daa[:, "points"], caa[:, "points"])