Skip to content

[3.8] bpo-39142: Avoid converting namedtuple instances to ConvertingTuple. (GH-17773) #17785

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 1 commit into from
Jan 1, 2020
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
2 changes: 1 addition & 1 deletion Lib/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def convert(self, value):
value = ConvertingList(value)
value.configurator = self
elif not isinstance(value, ConvertingTuple) and\
isinstance(value, tuple):
isinstance(value, tuple) and not hasattr(value, '_fields'):
value = ConvertingTuple(value)
value.configurator = self
elif isinstance(value, str): # str for py3k
Expand Down
31 changes: 31 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3361,6 +3361,37 @@ def test_baseconfig(self):
self.assertRaises(ValueError, bc.convert, 'cfg://!')
self.assertRaises(KeyError, bc.convert, 'cfg://adict[2]')

def test_namedtuple(self):
# see bpo-39142
from collections import namedtuple

class MyHandler(logging.StreamHandler):
def __init__(self, resource, *args, **kwargs):
super().__init__(*args, **kwargs)
self.resource: namedtuple = resource

def emit(self, record):
record.msg += f' {self.resource.type}'
return super().emit(record)

Resource = namedtuple('Resource', ['type', 'labels'])
resource = Resource(type='my_type', labels=['a'])

config = {
'version': 1,
'handlers': {
'myhandler': {
'()': MyHandler,
'resource': resource
}
},
'root': {'level': 'INFO', 'handlers': ['myhandler']},
}
with support.captured_stderr() as stderr:
self.apply_config(config)
logging.info('some log')
self.assertEqual(stderr.getvalue(), 'some log my_type\n')

class ManagerTest(BaseTest):
def test_manager_loggerclass(self):
logged = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A change was made to logging.config.dictConfig to avoid converting instances
of named tuples to ConvertingTuple. It's assumed that named tuples are too
specialised to be treated like ordinary tuples; if a user of named tuples
requires ConvertingTuple functionality, they will have to implement that
themselves in their named tuple class.