[code-quality] Having trouble making pylint+astroid truly ignore context

Daniel Balparda (梅巴達) balparda at google.com
Sat Apr 4 00:25:02 CEST 2015


  Hi,

  Inside Google we use pylint in many ways. In some we have plenty
context for imports, etc. In others we have only on file in isolation and
need to lint it the best we can without any context for imports, etc. To
support that we have a pylint mode where it should ignore everything
but the file given to it.

  This has been kind of working for a long time, but some bugs were
filed where we noticed our pylint was in fact behaving differently for
files with context available and those without. I eventually traced the
issue to the fact that when astroid sees a star import (from foo import *)
it will try to load symbols for the import. When the import makes sense
(in one example, the file is in a directory where it may work) it succeeds
in importing symbols; when the import makes no sense (in another example,
the file is isolated somewhere python won't find any symbol to import)
then it will not add the symbols.

  The specific place this happens is in astroid/builder.py:

def add_from_names_to_locals(self, node):
  """store imported names to the locals;
  resort the locals if coming from a delayed node
  """
  _key_func = lambda node: node.fromlineno
  def sort_locals(my_list):
      my_list.sort(key=_key_func)
  for (name, asname) in node.names:
      if name == '*':
          try:
              imported = node.do_import_module()
          except InferenceError:
              continue
          for name in imported.wildcard_import_names():
              node.parent.set_local(name, node)
              sort_locals(node.parent.scope().locals[name])
      else:
          node.parent.set_local(asname or name, node)
          sort_locals(node.parent.scope().locals[asname or name])

  My questions is: Is there a (non-hacky) way of telling astroid/pylint
that I really *really* don't want them to consider anything else from
context and actually use only the file I provided? If not, is it reasonable
to add an option for that? (I can do the coding, but not before discussing
our options.)

  Thank you very much,

  Best regards,

  Daniel

ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι══ι
*Daniel Balparda de Carvalho* 梅巴達 <balparda at google.com>
Python Team, Google US-MTV-CL2 3K5B, +1-650-933-8587
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/code-quality/attachments/20150403/11728294/attachment.html>


More information about the code-quality mailing list