Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ed5c56

Browse files
authored
Add __class_getitem__ to csv.DictReader and csv.DictWriter (#92393)
1 parent 3680ebe commit 5ed5c56

4 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/csv.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import re
7+
import types
78
from _csv import Error, __version__, writer, reader, register_dialect, \
89
unregister_dialect, get_dialect, list_dialects, \
910
field_size_limit, \
@@ -126,6 +127,8 @@ def __next__(self):
126127
d[key] = self.restval
127128
return d
128129

130+
__class_getitem__ = classmethod(types.GenericAlias)
131+
129132

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

162+
__class_getitem__ = classmethod(types.GenericAlias)
163+
159164
# Guard Sniffer's type checking against builds that exclude complex()
160165
try:
161166
complex

Lib/test/test_genericalias.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from concurrent.futures.thread import _WorkItem
1212
from contextlib import AbstractContextManager, AbstractAsyncContextManager
1313
from contextvars import ContextVar, Token
14+
from csv import DictReader, DictWriter
1415
from dataclasses import Field
1516
from functools import partial, partialmethod, cached_property
1617
from graphlib import TopologicalSorter
@@ -122,7 +123,8 @@ class BaseTest(unittest.TestCase):
122123
WeakSet, ReferenceType, ref,
123124
ShareableList,
124125
Future, _WorkItem,
125-
Morsel]
126+
Morsel,
127+
DictReader, DictWriter]
126128
if ctypes is not None:
127129
generic_types.extend((ctypes.Array, ctypes.LibraryLoader))
128130
if ValueProxy is not None:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@ Alessandro Moura
12281228
Pablo Mouzo
12291229
Mher Movsisyan
12301230
Ruslan Mstoi
1231+
Marc Mueller
12311232
Valentina Mukhamedzhanova
12321233
Michael Mulich
12331234
Sape Mullender
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :meth:`~object.__class_getitem__` to :class:`csv.DictReader` and
2+
:class:`csv.DictWriter`, allowing them to be parameterized at runtime.
3+
Patch by Marc Mueller.

0 commit comments

Comments
 (0)