Skip to content

fix: handle empty and headerless datasets in DBF export#660

Open
santhreal wants to merge 1 commit into
jazzband:masterfrom
santhreal:fix/dbf-empty-and-headerless
Open

fix: handle empty and headerless datasets in DBF export#660
santhreal wants to merge 1 commit into
jazzband:masterfrom
santhreal:fix/dbf-empty-and-headerless

Conversation

@santhreal

Copy link
Copy Markdown

DBF export_set inferred each field's type from dataset[0]. On a headers-only dataset (columns defined, no rows) that raised IndexError, and because the function never checked for headers, a headerless dataset raised a raw TypeError: 'NoneType' object is not iterable from zip(dataset.headers, ...).

Two changes: default field types to character when there are no rows (a headers-only dataset now exports to a valid dbf that round-trips), and raise HeadersNeeded when the dataset has no headers, since dbf fields must be named (matches the HeadersNeeded pattern used elsewhere rather than leaking a TypeError).

import tablib
d = tablib.Dataset(); d.headers = ['name', 'gpa']
d.export('dbf')  # IndexError before, valid dbf after
tablib.import_set('[[1,2]]', format='json').export('dbf')  # TypeError before, HeadersNeeded after

export_set inferred field types from `dataset[0]`, which raised IndexError
on a headers-only dataset (no rows) and, because it never checked for
headers, raised a raw TypeError on a headerless dataset. Default field
types to character when there are no rows, and raise HeadersNeeded (dbf
needs field names) instead of leaking a TypeError.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant