Skip to content

csv.Sniffer.has_header raises csv.Error for valid CR-terminated CSV #158701

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Python csv documentation, Sniffer.has_header(sample): "Analyze the sample text (presumed to be in CSV format) and return True if the first row appears to be a series of column headers." The Dialect.lineterminator documentation also states: "The reader is hard-coded to recognise either '\r' or '\n' as end-of-line."

Expected: Return a bool for the valid sample (normally True).

Actual: Raises csv.Error: new-line character seen in unquoted field - do you need to open the file with newline=''?

import csv
import io

s = "name,age\r1,2\r3,4\r"
try:
    rows = list(csv.reader(io.StringIO(s, newline=""), delimiter=",", strict=True))
    valid = rows == [["name", "age"], ["1", "2"], ["3", "4"]]
except csv.Error:
    valid = False

if not valid:
    print("REFUTATION REJECTED:", "input failed independent CSV validation")
else:
    expected = bool
    try:
        value = csv.Sniffer().has_header(s)
        actual = ("result", value)
        broken = type(value) is not expected
    except csv.Error as e:
        actual = ("exception", type(e).__name__, str(e))
        broken = True
    except Exception as e:
        actual = ("exception", type(e).__name__, str(e))
        broken = False
    if broken:
        print("REFUTATION CONFIRMED:", repr(s), "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED:", "no violation of the claimed bool/csv.Error property;", actual)

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library csv:

REFUTATION CONFIRMED: 'name,age\r1,2\r3,4\r' actual: ('exception', 'Error', "new-line character seen in unquoted field - do you need to open the file with newline=''?") expected: <class 'bool'>

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/csv-20261003-135109-c1

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions