Skip to content

gh-92391: Add __class_getitem__ to csv.DictReader and csv.DictWriter #92393

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

Merged
merged 3 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import re
import types
from _csv import Error, __version__, writer, reader, register_dialect, \
unregister_dialect, get_dialect, list_dialects, \
field_size_limit, \
Expand Down Expand Up @@ -126,6 +127,8 @@ def __next__(self):
d[key] = self.restval
return d

__class_getitem__ = classmethod(types.GenericAlias)


class DictWriter:
def __init__(self, f, fieldnames, restval="", extrasaction="raise",
Expand Down Expand Up @@ -156,6 +159,8 @@ def writerow(self, rowdict):
def writerows(self, rowdicts):
return self.writer.writerows(map(self._dict_to_list, rowdicts))

__class_getitem__ = classmethod(types.GenericAlias)

# Guard Sniffer's type checking against builds that exclude complex()
try:
complex
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from concurrent.futures.thread import _WorkItem
from contextlib import AbstractContextManager, AbstractAsyncContextManager
from contextvars import ContextVar, Token
from csv import DictReader, DictWriter
from dataclasses import Field
from functools import partial, partialmethod, cached_property
from graphlib import TopologicalSorter
Expand Down Expand Up @@ -122,7 +123,8 @@ class BaseTest(unittest.TestCase):
WeakSet, ReferenceType, ref,
ShareableList,
Future, _WorkItem,
Morsel]
Morsel,
DictReader, DictWriter]
if ctypes is not None:
generic_types.extend((ctypes.Array, ctypes.LibraryLoader))
if ValueProxy is not None:
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,7 @@ Alessandro Moura
Pablo Mouzo
Mher Movsisyan
Ruslan Mstoi
Marc Mueller
Valentina Mukhamedzhanova
Michael Mulich
Sape Mullender
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :meth:`~object.__class_getitem__` to :class:`csv.DictReader` and
:class:`csv.DictWriter`, allowing them to be parameterized at runtime.
Patch by Marc Mueller.