Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fieldnames of csv.DictReader Optional #3534

Merged
merged 2 commits into from
Dec 9, 2019

Conversation

aloisklink
Copy link
Contributor

The Python docs specify that fieldnames in csv.DictReader can be set to None to use the
first row of a CSV file as the field names:
https://docs.python.org/3.8/library/csv.html#csv.DictReader

Therefore, this pull request changes fieldnames: Sequence[str] = ... to fieldnames: Optional[Sequence[str]] = ....

A git blame of CPython's source states it's been like
that since Python 2.4,
see https://github.com/python/cpython/blame/3.8/Lib/csv.py#L81

I've also ran the file through black and isort (so that #3329 gets slightly easier), but I've put it into a separate commit, so it is easier to see the changes I made.

Test Case

import csv
with open("test.csv", "r") as file:
    csv.DictReader(file, fieldnames=None) #  should be valid

The Python docs specify that fieldnames can be set to None to use the
first row of a CSV file as the fieldnames:
https://docs.python.org/3.8/library/csv.html#csv.DictReader

A git blame of CPython's source states it's been like
that since Python 2.4,
see https://github.com/python/cpython/blame/3.8/Lib/csv.py#L81
Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thanks!

@srittau srittau merged commit a06abc5 into python:master Dec 9, 2019
@aloisklink aloisklink deleted the optional-dictreader-fieldnames branch September 20, 2022 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants