Fix Table.from_fixed reading data as schema for file-like schema_path - #813
Merged
jpmckinney merged 1 commit intoJul 4, 2026
Merged
Conversation
When schema_path is passed as a file-like object (has a read attribute) rather than a path string, from_fixed took the else branch and assigned schema_f = path -- the data file object -- instead of schema_f = schema_path. As a result the data stream was parsed as the schema, which almost always raised "Schema must contain exactly three columns" (or, if the data happened to look like a valid schema, silently produced the wrong columns). Only the string-path branch, which reopens schema_path by name, worked; the file-object branch was never exercised by the tests. Assign schema_f = schema_path in the else branch, mirroring how the data file's else branch assigns f = path. Add a regression test that passes both the fixed-width file and its schema as open file objects and asserts the parsed columns, types and rows match the string-path result. It fails without the fix (ValueError from the schema check) and passes with it.
jpmckinney
approved these changes
Jul 4, 2026
jpmckinney
left a comment
Member
There was a problem hiding this comment.
Thank you! Looks like a clear typo.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #813 +/- ##
==========================================
+ Coverage 93.41% 93.48% +0.06%
==========================================
Files 98 98
Lines 2978 2978
==========================================
+ Hits 2782 2784 +2
+ Misses 196 194 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Table.from_fixed()reads the data file as the schema wheneverschema_pathis passed as a file-like object instead of a path string, so it fails (or silently mis-parses).Root cause
In
agate/table/from_fixed.py, the schema file is resolved like this:When
schema_pathalready has areadattribute (a file-like object), theelsebranch assignsschema_f = path— the fixed-width data stream — rather thanschema_f = schema_path. The data is then handed to the schema CSV reader.The sibling branch a few lines above resolves the data file correctly (
f = pathin its ownelse), so only the schema branch is wrong. The existing test only passes string paths, which take theopen(schema_path, ...)branch, so the file-object path was never exercised.Symptom
(And in the rarer case where the data's first line happens to parse as a valid schema, it silently produces the wrong columns rather than erroring.)
Fix
Assign
schema_f = schema_pathin theelsebranch, mirroring how the data file'selsebranch assignsf = path. One line.schema_path= string pathschema_path= file-like objectValueError/ wrong columnsTest
Added
test_from_fixed_file_like_objects, which passes the existingexamples/testfixedandexamples/testfixed_schema.csvfixtures as open file objects and asserts the resulting column names, types, and rows match the string-path result.Proof it guards the bug (stash only the source fix, keep the test):
The pre-existing
test_from_fixed(string-path branch) passes in every state, confirming the change is surgical.Verification
396 passed(was 395; +1 new test)ruff checkon changed files: cleanisort --line-length 119 --check-onlyon changed files: cleanAlso added a
CHANGELOG.rstbullet under1.14.3 - Unreleasedand self-added toAUTHORS.rst.