Skip to content
Snippets Groups Projects
Commit cf564865 authored by Bryce Guinta's avatar Bryce Guinta Committed by Claudiu Popa
Browse files

Fix submodule import in six.moves

This commit fixes import errors when modname
started with, but was not equal, to six.moves
parent d768d7f3
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -2,6 +2,10 @@ Change log for the astroid package (used to be astng)
=====================================================
 
--
* Fix submodule imports from six
Close PYCQA/pylint#1640
* Fix missing __module__ and __qualname__ from class definition locals
 
Close PYCQA/pylint#1753
Loading
Loading
Loading
Loading
@@ -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
 
 
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
@@ -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):
Loading
Loading
Loading
Loading
@@ -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 '
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment