فهرست منبع

fix: add back the public api DataNode.read_or_raise()

trgiangdo 1 ماه پیش
والد
کامیت
092d78cd0f

+ 13 - 0
taipy/core/data/data_node.py

@@ -395,6 +395,19 @@ class DataNode(_Entity, _Labeled):
         """
         raise NotImplementedError
 
+    def read_or_raise(self) -> Any:
+        """Read the data referenced by this data node.
+
+        Returns:
+            The data referenced by this data node.
+
+        Raises:
+            NoData^: If the data has not been written yet.
+        """
+        from ._data_manager_factory import _DataManagerFactory
+
+        return _DataManagerFactory._build_manager()._read(self)
+
     def read(self) -> Any:
         """Read the data referenced by this data node.
 

+ 4 - 1
taipy/core/exceptions/exceptions.py

@@ -65,7 +65,10 @@ class MultipleDataNodeFromSameConfigWithSameOwner(Exception):
 
 
 class NoData(Exception):
-    """Raised if a data node is read before it has been written."""
+    """Raised if a data node is read before it has been written.
+
+    This exception can be raised by `DataNode.read_or_raise()^`.
+    """
 
 
 class UnknownDatabaseEngine(Exception):

+ 2 - 0
tests/core/data/test_data_node.py

@@ -153,6 +153,8 @@ class TestDataNode:
         assert dn.read() is None
         with pytest.raises(NoData):
             _DataManagerFactory._build_manager()._read(dn)
+        with pytest.raises(NoData):
+            dn.read_or_raise()
         assert dn.write_has_been_called == 0
         assert dn.read_has_been_called == 0
         assert not dn.is_ready_for_reading

+ 2 - 0
tests/core/data/test_in_memory_data_node.py

@@ -55,6 +55,8 @@ class TestInMemoryDataNodeEntity:
         assert no_data_dn.read() is None
         with pytest.raises(NoData):
             _DataManagerFactory._build_manager()._read(no_data_dn)
+        with pytest.raises(NoData):
+            no_data_dn.read_or_raise()
         in_mem_dn = InMemoryDataNode("foo", Scope.SCENARIO, properties={"default_data": "bar"})
         _DataManagerFactory._build_manager()._repository._save(in_mem_dn)
         assert isinstance(in_mem_dn.read(), str)

+ 2 - 0
tests/core/data/test_json_data_node.py

@@ -165,6 +165,8 @@ class TestJSONDataNode:
         assert not_existing_json.read() is None
         with pytest.raises(NoData):
             _DataManagerFactory._build_manager()._read(not_existing_json)
+        with pytest.raises(NoData):
+            not_existing_json.read_or_raise()
 
     def test_read(self):
         path_1 = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_list.json")

+ 2 - 0
tests/core/data/test_pickle_data_node.py

@@ -120,6 +120,8 @@ class TestPickleDataNodeEntity:
         assert no_data_dn.read() is None
         with pytest.raises(NoData):
             _DataManagerFactory._build_manager()._read(no_data_dn)
+        with pytest.raises(NoData):
+            no_data_dn.read_or_raise()
         pickle_str = PickleDataNode("foo", Scope.SCENARIO, properties={"default_data": "bar"})
         _DataManagerFactory._build_manager()._repository._save(pickle_str)
         assert isinstance(pickle_str.read(), str)

+ 2 - 0
tests/core/data/test_read_csv_data_node.py

@@ -37,6 +37,8 @@ def test_raise_no_data_with_header():
     assert not_existing_csv.read() is None
     with pytest.raises(NoData):
         _DataManagerFactory._build_manager()._read(not_existing_csv)
+    with pytest.raises(NoData):
+        not_existing_csv.read_or_raise()
 
 
 def test_read_with_header_pandas():

+ 2 - 0
tests/core/data/test_read_excel_data_node.py

@@ -67,6 +67,8 @@ def test_raise_no_data_with_header():
     assert not_existing_excel.read() is None
     with pytest.raises(NoData):
         _DataManagerFactory._build_manager()._read(not_existing_excel)
+    with pytest.raises(NoData):
+        not_existing_excel.read_or_raise()
 
 
 def test_read_empty_excel_with_header():

+ 2 - 0
tests/core/data/test_read_parquet_data_node.py

@@ -71,6 +71,8 @@ class TestReadParquetDataNode:
         assert not_existing_parquet.read() is None
         with pytest.raises(NoData):
             _DataManagerFactory._build_manager()._read(not_existing_parquet)
+        with pytest.raises(NoData):
+            not_existing_parquet.read_or_raise()
 
     @pytest.mark.parametrize("engine", __engine)
     def test_read_parquet_file_pandas(self, engine, parquet_file_path):