Closed augustomen closed 10 months ago
@augustomen,
Sorry for the delay. I have to fix the GitHub actions before evaluating these changes.
Thanks for the PR.
@augustomen,
As the CI was broken when you submitted this PR, I would like to check if everything is ok now after the fixes.
Could you rebase this PR before we proceed, please?
Rebased and merged in #653
As noted in https://github.com/petl-developers/petl/issues/575, there are still many places where
RuntimeError
could be raised as a result of an unhandledStopIteration
when callingnext()
, due to PEP 479, which was implemented in Python 3.5.This PR has the goal of properly handling headerless tables. This differs from empty tables in that an empty table at least has a header row (which could have no items in it - see
petl.util.base.empty()
). The real world use case for headerless tables are empty data-only CSVs (where the header is previously agreed between two systems), or development starting from an empty list - as is the case with all added tests.A previous PR (https://github.com/petl-developers/petl/pull/613) solved the issue for the function
iterrowslice
. This PR expands this fix to (almost) all such calls. Although a little bit repetitive, I've decided to try..except all calls and handle them individually.Changes
hdr = next(it)
that could potentially raise an exception.Notes
In some places, it made sense to let the exception be raised regardless, and in others it made sense to include a header row where one wasn't before. Namely:
petl.util.base.header()
: When no header is present, it makes sense to let an exception be raised (there was even a test for it). We may discuss if this should raise a custom exception instead of the genericStopIteration
.addfield()
,addcolumn()
and similars: A header is added if none present.cut()
,sort()
,convert()
and any others which depend on finding a header name: These will normally raiseFieldSelectionError
on empty tables, so instead of raisingStopIteration
I've mirrored the behavior on the former.petl.io.db
,petl.io.avro
and other formats that require field names: There is no such thing as a column-less table in RDBMS, so they will inevitably fail.Checklist
Use this checklist to ensure the quality of pull requests that include new code and/or make changes to existing code.
tox
/pytest
master
branch and tested before sending the PR