Skip to content

Raise HeadersNeeded instead of TypeError when subscripting a headerless Dataset by column name - #648

Merged
hugovk merged 1 commit into
jazzband:masterfrom
gaoflow:fix-getitem-delitem-no-headers
Jul 6, 2026
Merged

Raise HeadersNeeded instead of TypeError when subscripting a headerless Dataset by column name#648
hugovk merged 1 commit into
jazzband:masterfrom
gaoflow:fix-getitem-delitem-no-headers

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Problem

Dataset.__getitem__ and Dataset.__delitem__ both evaluate key in self.headers when given a string key. When no headers have been set (self.headers is None), Python raises an opaque:

TypeError: argument of type 'NoneType' is not iterable

instead of the library's own HeadersNeeded exception, which every other header-dependent operation (e.g. sort, filter, stack_cols) already raises.

Minimal reproduction:

import tablib
d = tablib.Dataset()
d.append([1, 2, 3])

d["col"]    # TypeError, should be HeadersNeeded
del d["col"]  # TypeError, should be HeadersNeeded

Fix

Add a self.headers is None guard at the top of the isinstance(key, str) branch in both dunder methods that raises HeadersNeeded() before the membership test is attempted.

Tests

Two new tests added to DatasetTests:

  • test_getitem_str_key_no_headers_raises — verifies d["col"] raises HeadersNeeded
  • test_delitem_str_key_no_headers_raises — verifies del d["col"] raises HeadersNeeded

All 155 tests pass (test_cli_export_github deselected as it is a pre-existing failure unrelated to this change).

…n headerless Dataset

`Dataset.__getitem__` and `Dataset.__delitem__` both call `key in self.headers`
when given a string key.  When `headers` is `None` (no headers set), Python
raises `TypeError: argument of type 'NoneType' is not iterable` instead of
the library's own `HeadersNeeded` exception, which all other header-dependent
operations already raise.

Add a `self.headers is None` guard that raises `HeadersNeeded()` before the
membership test in both dunder methods.

@claudep claudep left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, many thanks for your contribution!

@codecov

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.22%. Comparing base (64f0201) to head (ab10a5f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #648      +/-   ##
==========================================
+ Coverage   93.19%   93.22%   +0.02%     
==========================================
  Files          29       29              
  Lines        3247     3261      +14     
==========================================
+ Hits         3026     3040      +14     
  Misses        221      221              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hugovk hugovk added the changelog: Fixed For any bug fixes label Jul 6, 2026
@hugovk hugovk changed the title fix: raise HeadersNeeded instead of TypeError when subscripting a headerless Dataset by column name Raise HeadersNeeded instead of TypeError when subscripting a headerless Dataset by column name Jul 6, 2026
@hugovk hugovk changed the title Raise HeadersNeeded instead of TypeError when subscripting a headerless Dataset by column name Raise HeadersNeeded instead of TypeError when subscripting a headerless Dataset by column name Jul 6, 2026
@hugovk
hugovk merged commit e9da55b into jazzband:master Jul 6, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Fixed For any bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants