Commit 5b55bfed authored by Jérome Perrin's avatar Jérome Perrin

fixup! fixup! ERP5Type: more pylint / astroid fixes

parent cf3462a8
Pipeline #33749 failed with stage
in 0 seconds
......@@ -22,6 +22,7 @@ from __future__ import absolute_import
import sys
import warnings
import six
import types
from Products.ERP5Type import IS_ZOPE2
# TODO: make sure that trying to use it does not import isort, because the
......@@ -239,8 +240,10 @@ def _getattr(self, name, *args, **kw):
except NotFoundError as e:
if self.name.startswith('erp5.'):
raise
real_module = __import__(self.name, fromlist=[self.name], level=0)
real_module = __import__(
self.name,
fromlist=[self.name] if six.PY2 else [name],
level=0)
try:
attr = getattr(real_module, name)
except AttributeError:
......@@ -256,13 +259,21 @@ def _getattr(self, name, *args, **kw):
except AttributeError:
from astroid import nodes
if isinstance(attr, dict):
if six.PY2:
ast = nodes.Dict(attr)
else:
ast = nodes.Dict(attr, 0, None, end_lineno=0, end_col_offset=0)
elif isinstance(attr, list):
ast = nodes.List(attr)
elif isinstance(attr, tuple):
ast = nodes.Tuple(attr)
elif isinstance(attr, set):
if six.PY2:
ast = nodes.Set(attr)
else:
ast = nodes.Set(attr, 0, None, end_lineno=0, end_col_offset=0)
elif isinstance(attr, types.ModuleType):
ast = MANAGER.ast_from_module(attr)
else:
try:
ast = nodes.Const(attr)
......@@ -272,18 +283,15 @@ def _getattr(self, name, *args, **kw):
if self.name == origin_module_name:
raise
# ast_from_class() actually works for any attribute of a Module,
# but it raises some AssertionError when the class is defined
# dynamically, for example with zope.hookable.hookable , which
# (in version 6.0) is defined as:
#
# if _PURE_PYTHON or _c_hookable is None:
# hookable = _py_hookable
# else: # pragma: no cover
# hookable = _c_hookable
# on py2 ast_from_class() actually works for any attribute of a Module
try:
ast = MANAGER.ast_from_class(attr)
except (AssertionError, AstroidError):
except AstroidError:
if six.PY2:
raise e
try:
ast = list(MANAGER.infer_ast_from_something(attr))
except AstroidError:
raise e
self.locals[name] = [ast]
......
......@@ -2282,10 +2282,10 @@ def hoge():
"""# -*- coding: utf-8 -*-
# Source code with non-ASCII character should not fail: éàホゲ
from %(namespace)s import %(reference1)s
from %(namespace)s.erp5_version import %(reference1)s
from %(namespace)s.erp5_version import %(reference1)s # pylint:disable=reimported
from %(module2)s import hoge
from %(module2_with_version)s import hoge
from %(module2_with_version)s import hoge # pylint:disable=reimported
import %(module2)s
import %(module2_with_version)s
......@@ -2310,7 +2310,8 @@ from AccessControl.PermissionRole import rolesForPermissionOn, PermissionRole, i
# Monkey patch of astroid 1.3.8: it raised 'no-name-in-module' because
# Shared.DC was not considered a namespace package
from Shared.DC.ZRDB.Results import Results # pylint: disable=unused-import
from Shared.DC.ZRDB.Results import Results
_ = Results
import lxml.etree
lxml.etree.Element('test')
......@@ -2377,9 +2378,11 @@ _ = ZBigArray
self.tic()
self.assertEqual(component.getValidationState(), 'modified')
if six.PY2:
self.assertEqual(
component.getTextContentErrorMessageList(),
["E: 3, 0: No name '%s' in module '%s' (no-name-in-module)" %
[
"E: 3, 0: No name '%s' in module '%s' (no-name-in-module)" %
(imported_reference1, namespace),
"E: 4, 0: No name '%s' in module '%s.erp5_version' (no-name-in-module)" %
(imported_reference1, namespace),
......@@ -2403,9 +2406,41 @@ _ = ZBigArray
"E: 10, 0: No name '%s' in module '%s.erp5_version' (no-name-in-module)" %
(imported_reference2, namespace),
"F: 10, 0: Unable to import '%s' (import-error)" %
imported_module2_with_version])
imported_module2_with_version,
],
)
else:
self.assertEqual(
component.getTextContentErrorMessageList(),
[
"E: 3, 0: No name '%s' in module '%s' (no-name-in-module)" %
(imported_reference1, namespace),
"E: 4, 0: No name '%s' in module '%s.erp5_version' (no-name-in-module)" %
(imported_reference1, namespace),
"E: 6, 0: Unable to import '%s.%s' (import-error)" %
(namespace, imported_reference2),
# Spurious message but same as filesystem modules: 2 errors raised
# (no-name-in-module and import-error)
"E: 6, 0: No name '%s' in module '%s' (no-name-in-module)" %
(imported_reference2, namespace),
"E: 7, 0: Unable to import '%s' (import-error)" %
imported_module2_with_version,
# Spurious message (see above comment)
"E: 7, 0: No name '%s' in module '%s.erp5_version' (no-name-in-module)" %
(imported_reference2, namespace),
"E: 9, 0: Unable to import '%s.%s' (import-error)" %
(namespace, imported_reference2),
# Spurious message (see above comment)
"E: 9, 0: No name '%s' in module '%s' (no-name-in-module)" %
(imported_reference2, namespace),
"E: 10, 0: Unable to import '%s' (import-error)" %
imported_module2_with_version,
# Spurious message (see above comment)
"E: 10, 0: No name '%s' in module '%s.erp5_version' (no-name-in-module)" %
(imported_reference2, namespace),
],
)
self.assertEqual(component.getTextContentWarningMessageList(), [])
## Simulate user:
# 1) First check and validate 'imported' Components
self.portal.portal_workflow.doActionFor(imported_component1, 'validate_action')
......@@ -2416,20 +2451,29 @@ _ = ZBigArray
message_list = component.checkSourceCode()
self.assertEqual(message_list, [])
self._assertAstroidCacheContent(
must_be_in_cache_set={'%s' % namespace,
'%s.erp5_version' % namespace,
must_be_in_cache_set = {
imported_module1,
imported_module1_with_version,
imported_module2,
imported_module2_with_version},
imported_module2_with_version,
}
if six.PY2:
must_be_in_cache_set.update({
'%s' % namespace,
'%s.erp5_version' % namespace,
})
self._assertAstroidCacheContent(
must_be_in_cache_set=must_be_in_cache_set,
must_not_be_in_cache_set=set())
# 2) Then modify the main one so that it automatically 'validate'
component.setTextContent(component.getTextContent() + '\n')
self.tic()
must_be_in_cache_set = set()
if six.PY2:
must_be_in_cache_set.add(str(namespace))
self._assertAstroidCacheContent(
must_be_in_cache_set={'%s' % namespace},
must_be_in_cache_set=must_be_in_cache_set,
must_not_be_in_cache_set={'%s.erp5_version' % namespace,
imported_module1,
imported_module1_with_version,
......@@ -2442,10 +2486,11 @@ _ = ZBigArray
component.setTextContent(
"""# -*- coding: utf-8 -*-
from %(module)s import undefined
from %(module_with_version)s import undefined
from %(module_with_version)s import undefined2
# To avoid 'unused-import' warning...
undefined()
undefined2()
""" % (dict(module=imported_module2,
module_with_version=imported_module2_with_version)) +
......@@ -2456,7 +2501,7 @@ undefined()
component.getTextContentErrorMessageList(),
["E: 2, 0: No name 'undefined' in module '%s' (no-name-in-module)" %
imported_module2_with_version,
"E: 3, 0: No name 'undefined' in module '%s' (no-name-in-module)" %
"E: 3, 0: No name 'undefined2' in module '%s' (no-name-in-module)" %
imported_module2_with_version])
self.assertEqual(component.getTextContentWarningMessageList(), [])
......
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