Skip to content

Commit 9ee2f8d

Browse files
Merge pull request #1261 from ATGE/issue1258
Fix AttributeError: 'NoneType' object has no attribute 'keys
2 parents 3b54105 + 5fd9fb4 commit 9ee2f8d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

SoftLayer/CLI/formatting.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,13 @@ def _format_list(result):
416416
if not result:
417417
return result
418418

419-
if isinstance(result[0], dict):
420-
return _format_list_objects(result)
419+
new_result = [item for item in result if item]
420+
421+
if isinstance(new_result[0], dict):
422+
return _format_list_objects(new_result)
421423

422424
table = Table(['value'])
423-
for item in result:
425+
for item in new_result:
424426
table.add_row([iter_to_table(item)])
425427
return table
426428

@@ -430,12 +432,15 @@ def _format_list_objects(result):
430432

431433
all_keys = set()
432434
for item in result:
433-
all_keys = all_keys.union(item.keys())
435+
if isinstance(item, dict):
436+
all_keys = all_keys.union(item.keys())
434437

435438
all_keys = sorted(all_keys)
436439
table = Table(all_keys)
437440

438441
for item in result:
442+
if not item:
443+
continue
439444
values = []
440445
for key in all_keys:
441446
value = iter_to_table(item.get(key))

tests/CLI/helper_tests.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,10 @@ def test_template_options(self):
439439
class TestExportToTemplate(testing.TestCase):
440440

441441
def test_export_to_template(self):
442-
if(sys.platform.startswith("win")):
442+
if (sys.platform.startswith("win")):
443443
self.skipTest("Test doesn't work in Windows")
444444
# Tempfile creation is wonky on windows
445445
with tempfile.NamedTemporaryFile() as tmp:
446-
447446
template.export_to_template(tmp.name, {
448447
'os': None,
449448
'datacenter': 'ams01',
@@ -487,3 +486,9 @@ def test_format_api_list_non_objects(self):
487486
self.assertIsInstance(result, formatting.Table)
488487
self.assertEqual(result.columns, ['value'])
489488
self.assertEqual(result.rows, [['a'], ['b'], ['c']])
489+
490+
def test_format_api_list_with_none_value(self):
491+
result = formatting._format_list([{'key': [None, 'value']}, None])
492+
493+
self.assertIsInstance(result, formatting.Table)
494+
self.assertEqual(result.columns, ['key'])

0 commit comments

Comments
 (0)