closes #74087: handle case where inference of a module return YES

This avoid some cases of "TypeError: '_Yes' object does not support indexing" when conditionally assigning a nonexisting module to intermediate modules in a dotted name.

This is only a cosmetic fix to avoid the crash, the actual fix probably needs a lot of work on the inference module to support conditional assignment and inspection of several places of assignment, rather than just the latest one. Probably not worth it, either.

patch by Google

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changeseta51589d31587
branchdefault
phasepublic
hiddenno
parent revision#68aadb7d88eb closes #74742: make allowed name for first argument of class method configurable
child revision#da7ed73e8cff Fixes misleading wording in warnings E1001 and E1002 (patch by google)
files modified by this revision
ChangeLog
checkers/variables.py
test/regrtest_data/import_assign.py
test/test_regr.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1315381221 -7200
# Wed Sep 07 09:40:21 2011 +0200
# Node ID a51589d31587490c178eba39b7b4c8d03ed18057
# Parent 68aadb7d88eb54de6945b00323ddb90f8bf00796
closes #74087: handle case where inference of a module return YES

This avoid some cases of "TypeError: '_Yes' object does not support indexing"
when conditionally assigning a nonexisting module to intermediate modules in
a dotted name.

This is only a cosmetic fix to avoid the crash, the actual fix probably
needs a lot of work on the inference module to support conditional
assignment and inspection of several places of assignment, rather than just
the latest one. Probably not worth it, either.

patch by Google

diff --git a/ChangeLog b/ChangeLog
@@ -1,10 +1,14 @@
1  ChangeLog for PyLint
2  ====================
3  --
4 -    * #74742: make allowed name for first argument of class method
5 -      configurable (patch by Google)
6 +    * #74742: make allowed name for first argument of class method configurable
7 +      (patch by Google)
8 +
9 +    * #74087: handle case where inference of a module return YES; this avoid
10 +      some cases of "TypeError: '_Yes' object does not support indexing" (patch
11 +      by Google)
12 
13 
14  2011-07-18  --  0.24.0
15      * #69738: add regular expressions support for "generated-members"
16 
diff --git a/checkers/variables.py b/checkers/variables.py
@@ -1,6 +1,6 @@
17 -# Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE).
18 +# Copyright (c) 2003-2011 LOGILAB S.A. (Paris, FRANCE).
19  # http://www.logilab.fr/ -- mailto:contact@logilab.fr
20  #
21  # This program is free software; you can redistribute it and/or modify it under
22  # the terms of the GNU General Public License as published by the Free Software
23  # Foundation; either version 2 of the License, or (at your option) any later
@@ -481,10 +481,12 @@
24              if name == '__dict__':
25                  module = None
26                  break
27              try:
28                  module = module.getattr(name)[0].infer().next()
29 +                if module is astng.YES:
30 +                    return None
31              except astng.NotFoundError:
32                  self.add_message('E0611', args=(name, module.name), node=node)
33                  return None
34              except astng.InferenceError:
35                  return None
diff --git a/test/regrtest_data/import_assign.py b/test/regrtest_data/import_assign.py
@@ -0,0 +1,5 @@
36 +import shmixml.dom.minidom
37 +import xml.dom.minidom
38 +
39 +if 'dom' not in xml.__dict__:
40 +  xml.dom = shmixml.dom
diff --git a/test/test_regr.py b/test/test_regr.py
@@ -118,10 +118,13 @@
41      def test_package_import_relative_subpackage_no_attribute_error(self):
42          linter.check('import_package_subpackage_module')
43          got = linter.reporter.finalize().strip()
44          self.failUnlessEqual(got, '')
45 
46 +    def test_import_assign_crash(self):
47 +        linter.check(join(REGR_DATA, 'import_assign.py'))
48 +
49      def test_module_global_crash(self):
50          linter.check(join(REGR_DATA, 'module_global.py'))
51          got = linter.reporter.finalize().strip()
52          self.failUnlessEqual(got, '')
53