Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #524 check for type list and improve error msg #534

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,14 @@ def _set_dict(self, pickle):
data.dict = [{'age': 90, 'first_name': 'Kenneth', 'last_name': 'Reitz'}]

"""

error_details = "Please check format documentation https://tablib.readthedocs.io/en/stable/formats.html#yaml"
if not len(pickle):
return

if not isinstance(pickle, list):
# sometimes pickle is a dict and len(pickle) returns True.
# since we access index 0 we should check if the type is list
raise UnsupportedFormat(error_details)

# if list of rows
if isinstance(pickle[0], list):
Expand All @@ -350,7 +355,7 @@ def _set_dict(self, pickle):
for row in pickle:
self.append(Row(list(row.values())))
else:
raise UnsupportedFormat
raise UnsupportedFormat(error_details)

dict = property(_get_dict, _set_dict)

Expand Down
1 change: 1 addition & 0 deletions tests/files/issue_524.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Voice of Miki Vanoušek
7 changes: 7 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,13 @@ def test_yaml_export(self):
output = self.founders.yaml
self.assertEqual(output, expected)

def test_yaml_load(self):
""" test issue 524: invalid format """
yaml_source = Path(__file__).parent / 'files' / 'issue_524.yaml'
with yaml_source.open(mode='rb') as fh:
with self.assertRaises(UnsupportedFormat):
tablib.Dataset().load(fh, 'yaml')


class LatexTests(BaseTestCase):
def test_latex_export(self):
Expand Down