sacred icon indicating copy to clipboard operation
sacred copied to clipboard

Tuples get changed to lists

Open LukasDrude opened this issue 9 years ago • 8 comments

Tuples get changed to list. It did not find this behavior in the documentation, so I guess it might be undesired.

Here is a short example to reproduce it:

from sacred import Experiment
ex = Experiment('test')


@ex.config
def _config():
    axis = (0, 1)
    network = dict(
        axis=(0, 1),
    )
    print('config', type(axis))
    print('config', type(network['axis']))


@ex.automain
def main(axis, network):
    print('main', type(axis))
    print('main', type(network['axis']))

LukasDrude avatar Aug 30 '16 15:08 LukasDrude

Hi! Thank you for this nice bug report.

At the moment said conversion is a technical limitation, since everything is converted to JSON for later storage in a database etc.. And unfortunately JSON doesn't distinguish between lists and tuples. This might change if jsonpickle gets integrated (see #111). But for the moment, conversion is the intended behavior. That being said, I fully agree that it should be documented. I'll update the docs for the next release.

Qwlouse avatar Sep 07 '16 17:09 Qwlouse

This should be resolved in the current 0.7b1 release.

Qwlouse avatar Jan 16 '17 10:01 Qwlouse

Tuples are stilled converted to lists in version 0.7.4, at least in my observation.

YongfeiYan avatar Jan 09 '19 10:01 YongfeiYan

Confirmed, tuples are still converted to lists in version 0.7.4. This function in config/utils.py is causing the issue:

def normalize_or_die(obj):
    if isinstance(obj, dict):
        res = dict()
        for key, value in obj.items():
            assert_is_valid_key(key)
            res[key] = normalize_or_die(value)
        return res
    elif isinstance(obj, (list, tuple)):
        return list([normalize_or_die(value) for value in obj])
    return normalize_numpy(obj)

deluccial avatar Apr 25 '19 14:04 deluccial

Still converted to lists in 0.7.5. This issue shouldn't be closed.

brucechou1983 avatar Aug 07 '19 09:08 brucechou1983

@Qwlouse: is there any progress on resolving this issue? This behaviour is quite unexpected.

GiliR4t1qbit avatar Mar 26 '20 00:03 GiliR4t1qbit

+1 for re-opening this issue - spent several hours today trying to track down this problem, and finally arrived at the function normalize_or_die and this issue.

Using the stable docs at https://sacred.readthedocs.io/ I couldn't see anything about tuples being converted to lists.

aaronsnoswell avatar Dec 15 '20 02:12 aaronsnoswell

Submitted a PR to update the docs with some info about this. Hopefully saves future visitors a few hours de-bugging.

aaronsnoswell avatar Dec 15 '20 02:12 aaronsnoswell