spine-tools / Spine-Database-API

Database interface to Spine generic data model
https://www.tools-for-energy-system-modelling.org/
GNU Lesser General Public License v3.0
6 stars 5 forks source link

Trying to get a parameter value that does not exist Tracebacks #401

Closed soininen closed 1 month ago

soininen commented 1 month ago

The DatabaseMapping.get_*_item() methods generally return an empty dict if the item does not exist. However, when at least the following is in effect:

  1. There is a parameter definition x that is using a value list
  2. There is a parameter defined for x in an alternative, say A

Trying to db_map.get_parameter_value_item() for an alternative that is not A gives a Traceback:

Traceback (most recent call last):
  File "tests\test_DatabaseMapping.py", line 828, in test_nonexistent_parameter_value_with_list_value_does_not_traceback
    value = db_map.get_parameter_value_item(
  File "spinedb_api\db_mapping.py", line 426, in get_item
    item = mapped_table.find_item(kwargs)
  File "spinedb_api\db_mapping_base.py", line 437, in find_item
    return self._find_item_by_unique_key(item, skip_keys=skip_keys, fetch=fetch)
  File "spinedb_api\db_mapping_base.py", line 456, in _find_item_by_unique_key
    error = mapped_item.polish()
  File "spinedb_api\mapped_items.py", line 437, in polish
    type_ = super().__getitem__(self._type_key)
  File "spinedb_api\mapped_items.py", line 380, in __getitem__
    return super().__getitem__(key)
  File "spinedb_api\db_mapping_base.py", line 1200, in __getitem__
    return super().__getitem__(key)
KeyError: 'type'

It should rather return an empty dict.