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

gh-76728: Coerce DictReader and DictWriter fieldnames argument to a list #32225

Merged
merged 15 commits into from
Aug 25, 2022

Conversation

dignissimus
Copy link
Contributor

@dignissimus dignissimus commented Apr 1, 2022

Raises a TypeError when the given argument to the fieldnames parameter is not a sequence in csv.DictWriter and csv.DictReader

https://bugs.python.org/issue32547

Doc/library/csv.rst Outdated Show resolved Hide resolved
Doc/library/csv.rst Outdated Show resolved Hide resolved
Lib/csv.py Outdated Show resolved Hide resolved
Lib/csv.py Outdated Show resolved Hide resolved
dignissimus and others added 4 commits April 1, 2022 11:14
Lib/csv.py Outdated Show resolved Hide resolved
Lib/test/test_csv.py Outdated Show resolved Hide resolved
Lib/test/test_csv.py Outdated Show resolved Hide resolved
@dignissimus dignissimus changed the title bpo-32547: Raise TypeError when fieldnames is not a sequence gh-76728: Raise TypeError when fieldnames is not a sequence Apr 14, 2022
@rhettinger rhettinger self-assigned this Apr 30, 2022
@rhettinger
Copy link
Contributor

I prefer a simpler that solution that won't break existing code:

self._fieldnames = list(fieldnames)          # Coerce to a list

@dignissimus dignissimus changed the title gh-76728: Raise TypeError when fieldnames is not a sequence gh-76728: Coerce DictReader and DictWriter fieldnames argument to a list May 10, 2022
@dignissimus
Copy link
Contributor Author

I prefer a simpler that solution that won't break existing code:

self._fieldnames = list(fieldnames)          # Coerce to a list

@rhettinger
Implemented in cdf572b

@rhettinger
Copy link
Contributor

It would be faster to only do this for iterators. Here's how the issue in handled in the harmonic_mean() function in Lib/statistics.py:

    if iter(data) is data:
        data = list(data)

The docs edit would then say, "iterators are coerced to lists before being used".

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.

None yet

6 participants