Browse Source

format_dict() returns no value for None

We have some subtle cases to distinguish:
* value of None   (the Python literal)
* value of ''     (an empty string)
* value of 'None' (a string)

Return no representation of data when the value is the Python literal None
rather than the string 'None' which can be confused for an actual string value.

Returning an empty string ('') can be confused for an actual empty
string so that is also not an option.  By leaving off the quotes we can accurately
reflect the distinct value of None.

Change-Id: Icdc1712e626a7e8a706348586a6be2e7612c4376
Signed-off-by: Dean Troyer <dtroyer@gmail.com>
tags/1.14.1^0
Dean Troyer 2 weeks ago
parent
commit
a0d9746eb5
2 changed files with 9 additions and 7 deletions
  1. 6
    6
      osc_lib/tests/utils/test_utils.py
  2. 3
    1
      osc_lib/utils/__init__.py

+ 6
- 6
osc_lib/tests/utils/test_utils.py View File

@@ -690,18 +690,18 @@ class TestFindResource(test_utils.TestCase):
690 690
         self.assertIsNone(utils.format_dict(None))
691 691
 
692 692
     def test_format_dict_recursive(self):
693
-        expected = "a='b', c.1='d', c.2='e'"
693
+        expected = "a='b', c.1='d', c.2=''"
694 694
         self.assertEqual(
695 695
             expected,
696
-            utils.format_dict({'a': 'b', 'c': {'1': 'd', '2': 'e'}})
696
+            utils.format_dict({'a': 'b', 'c': {'1': 'd', '2': ''}})
697 697
         )
698 698
         self.assertEqual(
699 699
             expected,
700
-            utils.format_dict({'c': {'1': 'd', '2': 'e'}, 'a': 'b'})
700
+            utils.format_dict({'c': {'1': 'd', '2': ''}, 'a': 'b'})
701 701
         )
702 702
         self.assertIsNone(utils.format_dict(None))
703 703
 
704
-        expected = "a1='A', a2.b1.c1='B', a2.b1.c2='C', a2.b2='D'"
704
+        expected = "a1='A', a2.b1.c1='B', a2.b1.c2=, a2.b2='D'"
705 705
         self.assertEqual(
706 706
             expected,
707 707
             utils.format_dict(
@@ -710,7 +710,7 @@ class TestFindResource(test_utils.TestCase):
710 710
                     'a2': {
711 711
                         'b1': {
712 712
                             'c1': 'B',
713
-                            'c2': 'C',
713
+                            'c2': None,
714 714
                         },
715 715
                         'b2': 'D',
716 716
                     },
@@ -723,7 +723,7 @@ class TestFindResource(test_utils.TestCase):
723 723
                 {
724 724
                     'a2': {
725 725
                         'b1': {
726
-                            'c2': 'C',
726
+                            'c2': None,
727 727
                             'c1': 'B',
728 728
                         },
729 729
                         'b2': 'D',

+ 3
- 1
osc_lib/utils/__init__.py View File

@@ -315,8 +315,10 @@ def format_dict(data, prefix=None):
315 315
             # NOTE(dtroyer): Only append the separator chars here, quoting
316 316
             #                is completely handled in the terminal case.
317 317
             output = output + format_dict(data[s], prefix=key_str) + ", "
318
-        else:
318
+        elif data[s] is not None:
319 319
             output = output + key_str + "='" + six.text_type(data[s]) + "', "
320
+        else:
321
+            output = output + key_str + "=, "
320 322
     return output[:-2]
321 323
 
322 324
 

Loading…
Cancel
Save