Skip to content

row.pop(None) type error when working with rows from DictReader #8075

@simonw

Description

@simonw

csv.DictReader returns rows that include a key of None if you feed it a CSV file where some of the rows have more items than exist in the header:

import csv, io
rows = csv.DictReader(io.StringIO("id,name\n1,Cleo,extra"))
list(rows)

Outputs:

[{'id': '1', 'name': 'Cleo', None: ['extra']}]

I wrote some code that used row.pop(None) to remove that last key - but I got a type error from mypy.

Here's a simple demo.py module showing the error:

import csv, io

rows = csv.DictReader(io.StringIO("id,name\n1,Cleo,extra"))
for row in rows:
    if None in row:
        row.pop(None)
    print(row)

When I run mypy demo.py I get this:

demo.py:6: error: No overload variant of "pop" of "MutableMapping" matches argument type "None"
demo.py:6: note: Possible overload variants:
demo.py:6: note:     def pop(self, key: str) -> str
demo.py:6: note:     def [_T] pop(self, key: str, default: Union[str, _T] = ...) -> Union[str, _T]
Found 1 error in 1 file (checked 1 source file)

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