Repository: ecmwf/earthkit-data
Pull request #630 (2025-02-25 10:24:13)

Fix empty source iteration

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/630?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf) Report
Attention: Patch coverage is `60.00000%` with `2 lines` in your changes missing coverage. Please review.
> Please [upload](https://docs.codecov.com/docs/codecov-uploader) report for BASE (`main@e00170b`). [Learn more](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf#section-missing-base-commit) about missing BASE report.

| [Files with missing lines](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/630?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf) | Patch % | Lines |
|---|---|---|
| [tests/sources/test\_empty.py](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/630?src=pr&el=tree&filepath=tests%2Fsources%2Ftest_empty.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf#diff-dGVzdHMvc291cmNlcy90ZXN0X2VtcHR5LnB5) | 60.00% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/630?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf) |

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #630   +/-   ##
=======================================
  Coverage        ?   90.10%           
=======================================
  Files           ?      151           
  Lines           ?    11182           
  Branches        ?      533           
=======================================
  Hits            ?    10075           
  Misses          ?      937           
  Partials        ?      170           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/630?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf).


Patch:
diff --git a/docs/release_notes/version_0.12_updates.rst b/docs/release_notes/version_0.12_updates.rst
index 9b7ce83..6b985c1 100644
--- a/docs/release_notes/version_0.12_updates.rst
+++ b/docs/release_notes/version_0.12_updates.rst
@@ -2,6 +2,16 @@ Version 0.12 Updates
 /////////////////////////
 
 
+Version 0.12.5
+===============
+
+Fixed
+++++++++
+
+- Fixed issue when trying to iterate an empty source caused a crash (:pr:`630`)
+
+
+
 Version 0.12.4
 ===============
 
diff --git a/src/earthkit/data/sources/empty.py b/src/earthkit/data/sources/empty.py
index a4eb952..94b051c 100644
--- a/src/earthkit/data/sources/empty.py
+++ b/src/earthkit/data/sources/empty.py
@@ -16,6 +16,9 @@ class EmptySource(FieldList):
         # Used by multi-source
         return True
 
+    def __getitem__(self, key):
+        raise IndexError("Empty source")
+
     def __len__(self):
         return 0
 


Test patch:
diff --git a/tests/sources/test_empty.py b/tests/sources/test_empty.py
index 0f5bdc0..3340944 100644
--- a/tests/sources/test_empty.py
+++ b/tests/sources/test_empty.py
@@ -13,7 +13,7 @@ from earthkit.data import from_source
 from earthkit.data.testing import earthkit_examples_file
 
 
-def test_empty_source():
+def test_empty_source_len():
     ds = from_source("empty")
     assert len(ds) == 0
 
@@ -52,3 +52,9 @@ def test_empty_source_concat3():
     assert len(ds) == 2
     meta = ds.metadata("shortName")
     assert meta == ["2t", "msl"]
+
+
+def test_empty_source_iterate():
+    ds = from_source("empty")
+    for _ in ds:
+        assert False, "Empty source should not iterate"