Skip to content

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

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 4 commits into from
May 2, 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/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1145,6 +1146,8 @@ def __repr__(self):
name += ' '
return '<%s %s(%s)>' % (self.__class__.__name__, name, level)

__class_getitem__ = classmethod(GenericAlias)


class FileHandler(StreamHandler):
"""
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +114,7 @@ class BaseTest(unittest.TestCase):
MappingProxyType, AsyncGeneratorType,
DirEntry,
chain,
LoggerAdapter, StreamHandler,
TemporaryDirectory, SpooledTemporaryFile,
Queue, SimpleQueue,
_AssertRaisesContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.