#2909·taipy

`UnboundLocalError` in `ExcelDataNode._read_as` Masks Underlying I/O and File Not Found Exceptions

Author: codeCraft-RitikCreated Sep 22, 2026Updated Sep 22, 2026

Bug : UnboundLocalError in ExcelDataNode._read_as Masks Underlying I/O and File Not Found Exceptions

1. File & Lines

2. Description & Real-World Impact

When reading custom data structures from an Excel file in ExcelDataNode._read_as, the openpyxl workbook loader is invoked inside a try: block:

python
def _read_as(self, path: str):
    try:
        properties = self.properties
        excel_file = load_workbook(path, read_only=True)
        ...
    finally:
        excel_file.close()

If load_workbook(path, read_only=True) raises an exception (e.g. FileNotFoundError if the file is missing, zipfile.BadZipFile if corrupt, PermissionError if locked by another program, or InvalidFileException), execution jumps immediately to the finally: block.

At that point, excel_file was never bound in the local frame. Python intercepts this and raises:

UnboundLocalError: cannot access local variable 'excel_file' where it is not associated with a value

This masks the actual root cause. Any automated recovery, retry logic, or user exception handling expecting FileNotFoundError or IOError fails, presenting the end user with a misleading error about an unassigned internal variable.

3. Vulnerable Source Code

python
# taipy/core/data/excel.py lines 172-175 and 224-225
def _read_as(self, path: str):
    try:
        properties = self.properties
        excel_file = load_workbook(path, read_only=True)  # <--- If this raises, excel_file is unbound
        ...
    finally:
        excel_file.close()                                # <--- UnboundLocalError raised here!

4. Minimal Reproduction Script

python
from openpyxl import load_workbook

def simulate_read_as(path: str):
    try:
        excel_file = load_workbook(path, read_only=True)
    finally:
        excel_file.close()

# Calling with a non-existent file
try:
    simulate_read_as("missing_dataset.xlsx")
except Exception as e:
    import traceback
    traceback.print_exc()
    # Notice: Python reports 'UnboundLocalError: cannot access local variable excel_file'
    # instead of cleanly surfacing FileNotFoundError to the user or caller.

5. Proposed Fix (Git Patch)

diff
diff --git a/taipy/core/data/excel.py b/taipy/core/data/excel.py
index 1122334..5566778 100644
--- a/taipy/core/data/excel.py
+++ b/taipy/core/data/excel.py
@@ -170,8 +170,9 @@ class ExcelDataNode(DataNode, _TabularDataNodeMixin):
         return None
 
     def _read_as(self, path: str):
+        excel_file = None
         try:
             properties = self.properties
             excel_file = load_workbook(path, read_only=True)
             exposed_type = properties[self._EXPOSED_TYPE_PROPERTY]
@@ -222,7 +223,8 @@ class ExcelDataNode(DataNode, _TabularDataNodeMixin):
                         res[i] = sheet_exposed_type(*row)
                 work_books[sheet_name] = res  # type: ignore
         finally:
-            excel_file.close()
+            if excel_file is not None:
+                excel_file.close()
 
         if len(user_provided_sheet_names) == 1:
             return work_books[cast(list, user_provided_sheet_names)[0]]