diff options
| author | Bryce Guinta <bryce.paul.guinta@gmail.com> | 2018-01-07 14:28:42 -0700 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-03-16 17:19:58 +0100 |
| commit | da3afd694acc854063a8e544fe94d33c3623758e (patch) | |
| tree | c117344596d2343d18fef8605f670cda970a3f0d | |
| parent | 8a8e1a4e546ef170a54135c7017886474871539c (diff) | |
| download | astroid-git-da3afd694acc854063a8e544fe94d33c3623758e.tar.gz | |
Backport "Fix submodule import in six.moves" (commit cf5648658c87b6e3b2fa4394b5921792827246dc) to 1.6 release branch
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/brain/brain_six.py | 37 | ||||
| -rw-r--r-- | astroid/tests/unittest_brain.py | 14 |
3 files changed, 51 insertions, 4 deletions
@@ -1,6 +1,10 @@ Change log for the astroid package (used to be astng) ===================================================== + * Fix submodule imports from six + + Close PYCQA/pylint#1640 + * Enhancement of brain_numpy by adding different types from numpy.core.numerictypes diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index f16a2938..06b068ec 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -11,7 +11,7 @@ from textwrap import dedent from astroid import MANAGER, register_module_extender from astroid.builder import AstroidBuilder -from astroid.exceptions import AstroidBuildingError, InferenceError +from astroid.exceptions import AstroidBuildingError, InferenceError, AttributeInferenceError from astroid import nodes @@ -187,7 +187,7 @@ else: import html.entities as html_entities import html.parser as html_parser import http.client as http_client - import http.server + import http.server as http_server BaseHTTPServer = CGIHTTPServer = SimpleHTTPServer = http.server import pickle as cPickle import queue @@ -218,7 +218,8 @@ else: import tkinter.filedialog as tkinter_tkfiledialog import tkinter.font as tkinter_font import tkinter.messagebox as tkinter_messagebox - import urllib.request + import urllib + import urllib.request as urllib_request import urllib.robotparser as urllib_robotparser import urllib.parse as urllib_parse import urllib.error as urllib_error @@ -241,10 +242,38 @@ def six_moves_transform(): def _six_fail_hook(modname): - if modname != 'six.moves': + """Fix six.moves imports due to the dynamic nature of this + class. + + Construct a psuedo-module which contains all the nessecary imports + for six + + :param modname: Name of failed module + :type modname: str + + :return: An astroid module + :rtype: nodes.Module + """ + + attribute_of = (modname != "six.moves" and + modname.startswith("six.moves")) + if modname != 'six.moves' and not attribute_of: raise AstroidBuildingError(modname=modname) module = AstroidBuilder(MANAGER).string_build(_IMPORTS) module.name = 'six.moves' + if attribute_of: + # Facilitate import of submodules in Moves + start_index = len(module.name) + attribute = modname[start_index:].lstrip(".").replace(".", "_") + try: + import_attr = module.getattr(attribute)[0] + except AttributeInferenceError: + raise AstroidBuildingError(modname=modname) + if isinstance(import_attr, nodes.Import): + submodule = MANAGER.ast_from_module_name(import_attr.names[0][0]) + return submodule + # Let dummy submodule imports pass through + # This will cause an Uninferable result, which is okay return module def transform_six_add_metaclass(node): diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index f4edc7bb..ce3eb793 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -373,6 +373,20 @@ class SixBrainTest(unittest.TestCase): qname = 'httplib.HTTPSConnection' self.assertEqual(inferred.qname(), qname) + @unittest.skipIf(six.PY2, + "The python 2 six brain uses dummy classes") + def test_from_submodule_imports(self): + """Make sure ulrlib submodules can be imported from + + See PyCQA/pylint#1640 for relevant issue + """ + ast_node = builder.extract_node(''' + from six.moves.urllib.parse import urlparse + urlparse #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.FunctionDef) + @unittest.skipUnless(HAS_MULTIPROCESSING, 'multiprocesing is required for this test, but ' |
