[Python-checkins] gh-92128: Add `__class_getitem__` to `logging.LoggerAdapter` and `logging.StreamHandler` (#92129)

JelleZijlstra webhook-mailer at python.org
Mon May 2 11:10:08 EDT 2022


https://github.com/python/cpython/commit/ab616d323dbc473f8d5563b596e882ed3ccdf77b
commit: ab616d323dbc473f8d5563b596e882ed3ccdf77b
branch: main
author: Alex Waygood <Alex.Waygood at Gmail.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-05-02T09:10:02-06:00
summary:

gh-92128: Add `__class_getitem__` to `logging.LoggerAdapter` and `logging.StreamHandler` (#92129)

Closes #92128

files:
A Misc/NEWS.d/next/Library/2022-05-01-21-45-41.gh-issue-92128.Di7VbE.rst
M Lib/logging/__init__.py
M Lib/test/test_genericalias.py

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index d6315b047334e..432fefcb5b314 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -25,6 +25,7 @@
 
 import sys, os, time, io, re, traceback, warnings, weakref, collections.abc
 
+from types import GenericAlias
 from string import Template
 from string import Formatter as StrFormatter
 
@@ -1145,6 +1146,8 @@ def __repr__(self):
             name += ' '
         return '<%s %s(%s)>' % (self.__class__.__name__, name, level)
 
+    __class_getitem__ = classmethod(GenericAlias)
+
 
 class FileHandler(StreamHandler):
     """
@@ -1939,6 +1942,8 @@ def __repr__(self):
         level = getLevelName(logger.getEffectiveLevel())
         return '<%s %s (%s)>' % (self.__class__.__name__, logger.name, level)
 
+    __class_getitem__ = classmethod(GenericAlias)
+
 root = RootLogger(WARNING)
 Logger.root = root
 Logger.manager = Manager(Logger.root)
diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py
index bf96ba065fbb0..635ac0f7a85d7 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -14,6 +14,7 @@
 from dataclasses import Field
 from functools import partial, partialmethod, cached_property
 from graphlib import TopologicalSorter
+from logging import LoggerAdapter, StreamHandler
 from mailbox import Mailbox, _PartialFile
 try:
     import ctypes
@@ -113,6 +114,7 @@ class BaseTest(unittest.TestCase):
                      MappingProxyType, AsyncGeneratorType,
                      DirEntry,
                      chain,
+                     LoggerAdapter, StreamHandler,
                      TemporaryDirectory, SpooledTemporaryFile,
                      Queue, SimpleQueue,
                      _AssertRaisesContext,
diff --git a/Misc/NEWS.d/next/Library/2022-05-01-21-45-41.gh-issue-92128.Di7VbE.rst b/Misc/NEWS.d/next/Library/2022-05-01-21-45-41.gh-issue-92128.Di7VbE.rst
new file mode 100644
index 0000000000000..e4d62d221d8a0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-01-21-45-41.gh-issue-92128.Di7VbE.rst
@@ -0,0 +1,3 @@
+Add :meth:`~object.__class_getitem__` to :class:`logging.LoggerAdapter` and
+:class:`logging.StreamHandler`, allowing them to be parameterized at runtime.
+Patch by Alex Waygood.



More information about the Python-checkins mailing list