Skip to content

patches/pylint: fix regression with namedtuples

Jérome Perrin requested to merge fix/pylint_guess_encoding into master

with namedtuples and unicode literals enabled, our patch break with:

Module py2stdlib, line 266, in infer_named_tuple
  ''' % {'name': name, 'fields': attributes})
Module Products.ERP5Type.patches.pylint, line 74, in string_build
  encoding = _guess_encoding(data)
Module astroid.builder, line 65, in _guess_encoding
  if string.startswith('\xef\xbb\xbf'):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

because pylint/astroid uses string_build to evaluate the named tuple and the source code enabled unicode_literals future, string_build is called with an unicode object.

-> for inferred in node.func.infer():
  astroid-1.3.8-py2.7.egg/astroid/bases.py(391)infer()
-> return self._explicit_inference(self, context, **kwargs)
  astroid-1.3.8-py2.7.egg/astroid/brain/py2stdlib.py(266)infer_named_tuple()
-> ''' % {'name': name, 'fields': attributes})
> erp5/product/ERP5Type/patches/pylint.py(77)string_build()
-> data = data.encode('utf-8')
(Pdb) up
> astroid-1.3.8-py2.7.egg/astroid/brain/py2stdlib.py(266)infer_named_tuple()
-> ''' % {'name': name, 'fields': attributes})
(Pdb) l
249     def infer_named_tuple(node, context=None):
250         """Specific inference function for namedtuple CallFunc node"""
251         class_node, name, attributes = infer_func_form(node, nodes.Tuple._proxied,
252                                                        context=context)
253         fake = AstroidBuilder(MANAGER).string_build('''
254     class %(name)s(tuple):
255         _fields = %(fields)r
256         def _asdict(self):
257             return self.__dict__
258         @classmethod
259         def _make(cls, iterable, new=tuple.__new__, len=len):
260             return new(cls, iterable)
261         def _replace(_self, **kwds):
262             result = _self._make(map(kwds.pop, %(fields)r, _self))
263             if kwds:
264                 raise ValueError('Got unexpected field names: %%r' %% list(kwds))
265             return result
266  ->     ''' % {'name': name, 'fields': attributes})
(Pdb) pp name
u'NamedTuple'
(Pdb) pp attributes
[u'foo', u'bar']
(Pdb) pp [ (arg, arg.value) for arg in node.args ]
[(<Const(unicode) l.4 [checkPythonSourceCodelXOzr3] at 0x7f9f1caee250>,
  u'NamedTuple'),
 (<Const(unicode) l.4 [checkPythonSourceCodelXOzr3] at 0x7f9f1caeebd0>,
  u'foo bar')]

Merge request reports