diff --git a/redbaron/base_nodes.py b/redbaron/base_nodes.py index 1b5bb9bf..ff63540b 100644 --- a/redbaron/base_nodes.py +++ b/redbaron/base_nodes.py @@ -137,7 +137,7 @@ def get_holder_on_attribute(class_, node): class LiteralyEvaluable(object): def to_python(self): try: - return ast.literal_eval(self.dumps()) + return ast.literal_eval(self.dumps().strip()) except ValueError as e: message = 'to_python method only works on numbers, strings, list, tuple, dict, boolean and None. (using ast.literal_eval). The piece of code that you are trying to convert contains an illegale value, for example, a variable.' e.message = message diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 8def7207..51fd5b5f 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -149,6 +149,11 @@ def test_to_python_name_node_None(): assert red[0].to_python() == None +def test_to_python_with_spacing(): + red = RedBaron("{ 'pouet': d}") + assert red.find("string").to_python() == 'pouet' + + def test_to_python_name_node_otherwise_raise(): red = RedBaron("foo") try: