From ab616d323dbc473f8d5563b596e882ed3ccdf77b Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 2 May 2022 16:10:02 +0100 Subject: gh-92128: Add `__class_getitem__` to `logging.LoggerAdapter` and `logging.StreamHandler` (#92129) Closes #92128 --- Lib/logging/__init__.py | 5 +++++ Lib/test/test_genericalias.py | 2 ++ .../next/Library/2022-05-01-21-45-41.gh-issue-92128.Di7VbE.rst | 3 +++ 3 files changed, 10 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-05-01-21-45-41.gh-issue-92128.Di7VbE.rst diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index d6315b0..432fefc 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -25,6 +25,7 @@ To use, simply 'import logging' and log away! 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 @@ class StreamHandler(Handler): name += ' ' return '<%s %s(%s)>' % (self.__class__.__name__, name, level) + __class_getitem__ = classmethod(GenericAlias) + class FileHandler(StreamHandler): """ @@ -1939,6 +1942,8 @@ class LoggerAdapter(object): 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 bf96ba0..635ac0f 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -14,6 +14,7 @@ from contextvars import ContextVar, Token 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 0000000..e4d62d2 --- /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. -- cgit v0.12