Repository: gdsfactory/kfactory

Issue:
VKCell.write fails on an empty cell
```python
from kfactory import VKCell

c = VKCell("test")
c.write("test.gds")
```

fails like:
```bash
>
    self.insert_vinsts()
>
    called_cell_indexes = self._kdb_cell.called_cells()
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Internal error: src/db/db/dbLayout.cc:1861 topological_sort () was not true in Cell.called_cells

Discussion:
@sebastian-goeldi: whoops, yeah the write logic is weird there. I can fix that@joamatab: any updates on this?@MatthewMckee4: testing it now with new klayout@sebastian-goeldi: Yeah, unless it's super urgent, I'll fix it after https://github.com/gdsfactory/kfactory/pull/552


As a workaround you can do:

```python
c = kf.kcl.kcell()
c.create_vinst(my_vkcell)
c.insert_vinsts()
c.insts[0].cell.write(...)
c.delete()
```

Solution patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 0a9af8b..e7c9fa4 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -21,7 +21,7 @@ repos:
         exclude: 'changelog\.d/.*|CHANGELOG\.md'
   - repo: https://github.com/astral-sh/ruff-pre-commit
     # Ruff version.
-    rev: v0.9.1
+    rev: v0.9.2
     hooks:
       # Run the linter.
       - id: ruff
@@ -36,13 +36,13 @@ repos:
         files: .ipynb
 
   - repo: https://github.com/codespell-project/codespell
-    rev: v2.3.0
+    rev: v2.4.0
     hooks:
       - id: codespell
         additional_dependencies:
           - tomli
   - repo: https://github.com/PyCQA/bandit
-    rev: 1.8.0
+    rev: 1.8.2
     hooks:
       - id: bandit
         args: [--exit-zero]
diff --git a/src/kfactory/kcell.py b/src/kfactory/kcell.py
index 1627b29..9b2c4c2 100644
--- a/src/kfactory/kcell.py
+++ b/src/kfactory/kcell.py
@@ -5701,7 +5701,7 @@ class VKCell(BaseKCell, arbitrary_types_allowed=True):
         c._settings = self._settings
         c._settings_units = self._settings_units
         c.info = self.info
-        VInstance(self).insert_into(c)
+        VInstance(self).insert_into_flat(c, levels=1)
 
         c.write(
             filename=filename,


Test patch:
diff --git a/tests/test_virtual.py b/tests/test_virtual.py
index 3a9c0b3..93defd5 100644
--- a/tests/test_virtual.py
+++ b/tests/test_virtual.py
@@ -1,6 +1,7 @@
 import kfactory as kf
 from functools import partial
 from conftest import Layers
+from pathlib import Path
 
 
 def test_virtual_cell() -> None:
@@ -52,7 +53,6 @@ def test_virtual_cell_insert(
 
 def test_all_angle_route(LAYER: Layers, wg_enc: kf.LayerEnclosure) -> None:
     bb = [kf.kdb.DPoint(x, y) for x, y in [(0, 0), (500, 0), (250, 200), (500, 250)]]
-    c = kf.KCell("test_virtual")
     vc = kf.VKCell("test_all_angle")
     kf.routing.aa.optical.route(
         vc,
@@ -71,4 +71,8 @@ def test_all_angle_route(LAYER: Layers, wg_enc: kf.LayerEnclosure) -> None:
             enclosure=wg_enc,
         ),
     )
-    kf.VInstance(vc, kf.kdb.DCplxTrans()).insert_into(c)
+    # kf.VInstance(vc, kf.kdb.DCplxTrans()).insert_into(c)
+    file = Path("test_all_angle.oas")
+    vc.write(file)
+    assert file.is_file()
+    file.unlink()