Skip to content

csv.DictReader.line_num is stale after skipping trailing blank lines #158777

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The Python csv documentation, under DictReader.line_num, says: “The number of lines read from the source iterator. This is not the same as the number of records returned, as records can span multiple lines.”

Expected: After exhaustion, reader.line_num == 3.

Actual: All three source lines are consumed, but reader.line_num == 2.

import csv

lines = ['name\n', '\n', '\n']
if list(csv.reader(lines)) != [['name'], [], []]:
    print('REFUTATION REJECTED: input is not a header followed by valid blank CSV lines')
else:
    consumed = []
    reader = csv.DictReader(consumed.append(line) or line for line in lines)
    list(reader)
    actual, expected = reader.line_num, len(consumed)
    if actual != expected:
        print('REFUTATION CONFIRMED:', lines, 'actual:', actual, 'expected:', expected)
    else:
        print('REFUTATION REJECTED: line_num equals the number of source lines consumed')

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

REFUTATION CONFIRMED: ['name\n', '\n', '\n'] actual: 2 expected: 3

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-022404-c3

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