From edfafa713c2404d17d7e745c31f11aa78e2c61af Mon Sep 17 00:00:00 2001 From: Michiel Cottaar <MichielCottaar@gmail.com> Date: Thu, 14 Feb 2019 09:20:58 +0000 Subject: [PATCH] BUG: actually unset variable if set to None --- fsl/utils/filetree/filetree.py | 5 ++++- tests/test_filetree/test_read.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/fsl/utils/filetree/filetree.py b/fsl/utils/filetree/filetree.py index e86f93c04..82fd137e9 100644 --- a/fsl/utils/filetree/filetree.py +++ b/fsl/utils/filetree/filetree.py @@ -190,11 +190,14 @@ class FileTree(object): Creates a new FileTree with updated variables :param variables: new values for the variables - Setting variables to None will force that variable to be empty (useful for non-optional variables) + Setting a variable to None will cause the variable to be unset :return: New FileTree with same templates for directory names and filenames, but updated variables """ new_tree = deepcopy(self) new_tree.variables.update(variables) + for key, value in variables.items(): + if value is None: + del new_tree.variables[key] return new_tree def extract_variables(self, short_name: str, filename: str) -> Dict[str, str]: diff --git a/tests/test_filetree/test_read.py b/tests/test_filetree/test_read.py index b27984477..40ffde224 100644 --- a/tests/test_filetree/test_read.py +++ b/tests/test_filetree/test_read.py @@ -77,8 +77,8 @@ def test_custom_tree(): assert len(tree.get_all('sub_file', glob_vars='all')) == 2 assert len(tree.get_all('sub_file')) == 1 assert len(tree.update(opt=None).get_all('sub_file')) == 1 - assert len(tree.update(opt=None).get_all('sub_file', glob_vars=['opt'])) == 1 - assert len(tree.update(opt=None).get_all('sub_file', glob_vars='all')) == 1 + assert len(tree.update(opt=None).get_all('sub_file', glob_vars=['opt'])) == 2 + assert len(tree.update(opt=None).get_all('sub_file', glob_vars='all')) == 2 for fn, settings in zip(tree.get_all('sub_file', glob_vars='all'), tree.get_all_vars('sub_file', glob_vars='all')): -- GitLab