[Python-checkins] Add __class_getitem__ to csv.DictReader and csv.DictWriter (#92393)

JelleZijlstra webhook-mailer at python.org
Sun May 8 10:25:18 EDT 2022


https://github.com/python/cpython/commit/5ed5c5612363538a1d73dbc3948fa70ca743ba2c
commit: 5ed5c5612363538a1d73dbc3948fa70ca743ba2c
branch: main
author: Marc Mueller <30130371+cdce8p at users.noreply.github.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-05-08T07:24:54-07:00
summary:

Add __class_getitem__ to csv.DictReader and csv.DictWriter (#92393)

files:
A Misc/NEWS.d/next/Library/2022-05-06-13-00-57.gh-issue-92391.s-Lase.rst
M Lib/csv.py
M Lib/test/test_genericalias.py
M Misc/ACKS

diff --git a/Lib/csv.py b/Lib/csv.py
index a0782705d5af1..bfc850ee96dab 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -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, \
@@ -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",
@@ -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
diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py
index 6959c2ae3c80e..1afb7ea4f85d0 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -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
@@ -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:
diff --git a/Misc/ACKS b/Misc/ACKS
index a55706d508a41..f3d8924ea62af 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1228,6 +1228,7 @@ Alessandro Moura
 Pablo Mouzo
 Mher Movsisyan
 Ruslan Mstoi
+Marc Mueller
 Valentina Mukhamedzhanova
 Michael Mulich
 Sape Mullender
diff --git a/Misc/NEWS.d/next/Library/2022-05-06-13-00-57.gh-issue-92391.s-Lase.rst b/Misc/NEWS.d/next/Library/2022-05-06-13-00-57.gh-issue-92391.s-Lase.rst
new file mode 100644
index 0000000000000..e042671dae816
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-06-13-00-57.gh-issue-92391.s-Lase.rst
@@ -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.



More information about the Python-checkins mailing list