UnboundLocalError

When using custom data configuration, I noticed that when the user passes an invalid value to one of the arguments, the load_dataset() script throws an UnboundLocalError. Here is the trace

Using custom data configuration or_queries-db81180f5f5e3006
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/home/.local/share/virtualenvs/17147-yQ3Y_lVD/lib/python3.8/site-packages/datasets/load.py", line 1734, in load_dataset
    builder_instance = load_dataset_builder(
  File "/Users/home/.local/share/virtualenvs/17147-yQ3Y_lVD/lib/python3.8/site-packages/datasets/load.py", line 1518, in load_dataset_builder
    builder_instance: DatasetBuilder = builder_cls(
  File "/Users/home/.local/share/virtualenvs/17147-yQ3Y_lVD/lib/python3.8/site-packages/datasets/builder.py", line 1357, in __init__
    super().__init__(*args, **kwargs)
  File "/Users/home/.local/share/virtualenvs/17147-yQ3Y_lVD/lib/python3.8/site-packages/datasets/builder.py", line 332, in __init__
    info.update(self._info())
  File "/Users/home/.cache/huggingface/modules/datasets_modules/datasets/bt--os/53db9a9f034b31f75fc6d7c2efcdada9ece579d47c62b9a1c39dd1969da4ecb1/os.py", line 189, in _info
    features=features,
UnboundLocalError: local variable 'features' referenced before assignment

I thought I could catch this error by validating the input but it is ignored. i also tried to add a conditional block around the return statement inside the _info() but it is ignored. How can I throw a ValueError back to the user similar to what happens when passing an invalid name attribute ?

I figured out what I was missing.

While running the load_dataset() I was still calling the script from the Hub instead of locally. A big doh moment there.

The “UnboundLocalError: local variable referenced before assignment” error occurs in Python when you try to access a local variable before it has been assigned a value. This is typically caused by a typo, or by using the same name for a local variable and a global variable in the same scope or when you forget to assign a value to a local variable before using it.

    x = x + 1
    print(x) // This code will result in the error

to solve this…

    x = 0 //assign a value to the local variable
    x = x + 1
    print(x)

To resolve the “UnboundLocalError: local variable referenced before assignment” error, you need to assign a value to the local variable before you use it. Another way to resolve this error is to use the global keyword to access the global variable with the same name.

1 Like