Commit 4341268c authored by Arnaud Fontaine's avatar Arnaud Fontaine

ZODB Components: pylint: Generate astroid Module AST once for both the...

ZODB Components: pylint: Generate astroid Module AST once for both the Component Module and its versioned Module.
parent 74f242a9
...@@ -108,7 +108,6 @@ def erp5_package_transform(node): ...@@ -108,7 +108,6 @@ def erp5_package_transform(node):
# Cannot call string_build() as this would be called again and again # Cannot call string_build() as this would be called again and again
erp5_package_node = nodes.Module('erp5', None) erp5_package_node = nodes.Module('erp5', None)
erp5_package_node.package = True erp5_package_node.package = True
erp5_package_node._absolute_import_activated = True
return erp5_package_node return erp5_package_node
MANAGER.register_transform(nodes.Module, MANAGER.register_transform(nodes.Module,
erp5_package_transform, erp5_package_transform,
...@@ -145,8 +144,14 @@ def _buildAstroidModuleFromComponentModuleName(modname): ...@@ -145,8 +144,14 @@ def _buildAstroidModuleFromComponentModuleName(modname):
continue continue
if obj.getValidationState() in ('modified', 'validated'): if obj.getValidationState() in ('modified', 'validated'):
component_obj = obj version_modname = 'erp5.component.%s.%s_version.%s' % (package,
break version,
reference)
module = MANAGER.astroid_cache.get(
version_modname,
_buildAstroidModuleFromComponentModuleName(version_modname))
MANAGER.astroid_cache[modname] = module
return module
if component_obj is None: if component_obj is None:
raise AstroidBuildingException raise AstroidBuildingException
...@@ -179,7 +184,6 @@ def fail_hook_erp5_component(modname): ...@@ -179,7 +184,6 @@ def fail_hook_erp5_component(modname):
else: else:
module = _buildAstroidModuleFromComponentModuleName(modname) module = _buildAstroidModuleFromComponentModuleName(modname)
module._absolute_import_activated = True
return module return module
MANAGER.register_failed_import_hook(fail_hook_erp5_component) MANAGER.register_failed_import_hook(fail_hook_erp5_component)
...@@ -224,6 +228,18 @@ def _getattr(self, name, *args, **kw): ...@@ -224,6 +228,18 @@ def _getattr(self, name, *args, **kw):
return [ast] return [ast]
Module.getattr = _getattr Module.getattr = _getattr
if sys.version_info < (2, 8):
def _absolute_import_activated(self):
if (self.name.startswith('checkPythonSourceCode') or
self.name.startswith('erp5')):
return True
for stmt in self.locals.get('absolute_import', ()):
if isinstance(stmt, From) and stmt.modname == '__future__':
return True
return False
from logilab.common.decorators import cachedproperty
Module._absolute_import_activated = cachedproperty(_absolute_import_activated)
from astroid import register_module_extender from astroid import register_module_extender
def AccessControl_PermissionRole_transform(): def AccessControl_PermissionRole_transform():
return AstroidBuilder(MANAGER).string_build(''' return AstroidBuilder(MANAGER).string_build('''
......
...@@ -2319,11 +2319,69 @@ undefined() ...@@ -2319,11 +2319,69 @@ undefined()
self.assertEqual( self.assertEqual(
component.getTextContentErrorMessageList(), component.getTextContentErrorMessageList(),
["E: 2, 0: No name 'undefined' in module '%s' (no-name-in-module)" % ["E: 2, 0: No name 'undefined' in module '%s' (no-name-in-module)" %
imported_module2, imported_module2_with_version,
"E: 3, 0: No name 'undefined' in module '%s' (no-name-in-module)" % "E: 3, 0: No name 'undefined' in module '%s' (no-name-in-module)" %
imported_module2_with_version]) imported_module2_with_version])
self.assertEqual(component.getTextContentWarningMessageList(), []) self.assertEqual(component.getTextContentWarningMessageList(), [])
def testPylintAstroidModuleGeneratedOnce(self):
imported_reference = self._generateReference('TestPylintAstroidModuleGeneratedOnceImported')
imported_component = self._newComponent(imported_reference)
self.portal.portal_workflow.doActionFor(imported_component, 'validate_action')
imported_component.setTextContent(imported_component.getTextContent() + """
def hoge():
return 'OK'
""")
self.tic()
self.assertEqual(imported_component.getValidationState(), 'validated')
self.assertEqual(imported_component.getTextContentErrorMessageList(), [])
self.assertEqual(imported_component.getTextContentWarningMessageList(), [])
priority_tuple = self.portal.getVersionPriorityList()
try:
self.portal.setVersionPriorityList(('bar | 42.0',) + priority_tuple)
self.tic()
imported_module_with_bar_version = self._getComponentFullModuleName(
imported_reference, version='erp5')
imported_bar_component = self._newComponent(imported_reference, version='bar')
self.portal.portal_workflow.doActionFor(imported_bar_component, 'validate_action')
imported_bar_component.setTextContent(imported_component.getTextContent())
self.tic()
self.assertEqual(imported_bar_component.getValidationState(), 'validated')
self.assertEqual(imported_bar_component.getTextContentErrorMessageList(), [])
self.assertEqual(imported_bar_component.getTextContentWarningMessageList(), [])
reference = self._generateReference('TestPylintAstroidModuleGeneratedOnce')
component = self._newComponent(reference)
component.setTextContent(component.getTextContent() + """
from %(namespace)s import %(reference)s
from %(namespace)s.bar_version import %(reference)s
from %(namespace)s.erp5_version import %(reference)s
# To avoid 'unused-import' warning...
%(reference)s.hoge()
""" % dict(namespace=self._document_class._getDynamicModuleNamespace(),
reference=imported_reference))
self.portal.portal_workflow.doActionFor(component, 'validate_action')
self.tic()
self.assertEqual(component.getValidationState(), 'validated')
self.assertEqual(component.getTextContentErrorMessageList(), [])
self.assertEqual(component.getTextContentWarningMessageList(), [])
from astroid.builder import MANAGER
imported_module = self._getComponentFullModuleName(imported_reference)
self.assertEqual(
MANAGER.astroid_cache[self._getComponentFullModuleName(imported_reference, version='bar')],
MANAGER.astroid_cache[imported_module])
self.assertNotEqual(
MANAGER.astroid_cache[self._getComponentFullModuleName(imported_reference, version='erp5')],
MANAGER.astroid_cache[imported_module])
finally:
self.portal.setVersionPriorityList(priority_tuple)
self.tic()
def testPylintNamedtupleUnicodeLiteralsRegression(self): def testPylintNamedtupleUnicodeLiteralsRegression(self):
# regression for a bug with our pylint patches on guess encoding # regression for a bug with our pylint patches on guess encoding
# a named tuple with unicode_literals enabled cause UnicodeDecodeError # a named tuple with unicode_literals enabled cause UnicodeDecodeError
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment