[issue39142] logging.config.dictConfig will convert namedtuple to ConvertingTuple

Karthikeyan Singaravelan report at bugs.python.org
Sat Dec 28 15:01:14 EST 2019


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

Thanks for the details. Attached is a complete script. Looking at the source code, anything that looks like a tuple is converted into a tuple. namedtuple produces factory function that itself is a subclass of tuple. It led me to this interesting issue over detecting if it's a namedtuple issue7796 where detecting _fields in msg134186 is described as a workaround. So logging might detect namedtuple and skip conversion to ConvertingTuple using the workaround but it appears more of the class of special casing it here in logging  that will depend on namedtuple internals. There are no test failures. I will leave it to vinay on this.

>>> import collections
>>> person = collections.namedtuple('Person', 'age')
>>> isinstance(person(age=20), tuple)
True
>>> print(person._source) # works on 3.6 but verbose and _source were removed in 3.7 with 8b57d7363916869357848e666d03fa7614c47897
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict

class Person(tuple):
    'Person(age,)'
// snip more source code for person

# Detect namedtuple by checking for _fields attribute

diff --git a/Lib/logging/config.py b/Lib/logging/config.py
index 4a3b8966ed..ba6937e725 100644
--- a/Lib/logging/config.py
+++ b/Lib/logging/config.py
@@ -448,7 +448,8 @@ class BaseConfigurator(object):
             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

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39142>
_______________________________________


More information about the Python-bugs-list mailing list