Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Unified Diff: third_party/logilab/astroid/brain/py2stdlib.py

Issue 753543006: pylint: upgrade to 1.4.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/logilab/astroid/brain/py2qt4.py ('k') | third_party/logilab/astroid/builder.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/logilab/astroid/brain/py2stdlib.py
diff --git a/third_party/logilab/astroid/brain/py2stdlib.py b/third_party/logilab/astroid/brain/py2stdlib.py
index 2ead081b1e78b18441c25fb94258878bd65a679c..973c3ad912c908c356d63d233a4ecd83a917f721 100644
--- a/third_party/logilab/astroid/brain/py2stdlib.py
+++ b/third_party/logilab/astroid/brain/py2stdlib.py
@@ -11,12 +11,11 @@ from textwrap import dedent
from astroid import (
MANAGER, AsStringRegexpPredicate,
UseInferenceDefault, inference_tip,
- YES, InferenceError)
+ YES, InferenceError, register_module_extender)
from astroid import exceptions
from astroid import nodes
from astroid.builder import AstroidBuilder
-MODULE_TRANSFORMS = {}
PY3K = sys.version_info > (3, 0)
PY33 = sys.version_info >= (3, 3)
@@ -26,7 +25,7 @@ def infer_func_form(node, base_type, context=None, enum=False):
"""Specific inference function for namedtuple or Python 3 enum. """
def infer_first(node):
try:
- value = node.infer(context=context).next()
+ value = next(node.infer(context=context))
if value is YES:
raise UseInferenceDefault()
else:
@@ -90,39 +89,31 @@ def infer_func_form(node, base_type, context=None, enum=False):
# module specific transformation functions #####################################
-def transform(module):
- try:
- tr = MODULE_TRANSFORMS[module.name]
- except KeyError:
- pass
- else:
- tr(module)
-MANAGER.register_transform(nodes.Module, transform)
-
-# module specific transformation functions #####################################
-
-def hashlib_transform(module):
+def hashlib_transform():
template = '''
-class %s(object):
+class %(name)s(object):
def __init__(self, value=''): pass
def digest(self):
- return u''
+ return %(digest)s
+ def copy(self):
+ return self
def update(self, value): pass
def hexdigest(self):
- return u''
+ return ''
+ @property
+ def name(self):
+ return %(name)r
'''
-
algorithms = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
- classes = "".join(template % hashfunc for hashfunc in algorithms)
+ classes = "".join(
+ template % {'name': hashfunc, 'digest': 'b""' if PY3K else '""'}
+ for hashfunc in algorithms)
+ return AstroidBuilder(MANAGER).string_build(classes)
- fake = AstroidBuilder(MANAGER).string_build(classes)
- for hashfunc in algorithms:
- module.locals[hashfunc] = fake.locals[hashfunc]
-
-def collections_transform(module):
- fake = AstroidBuilder(MANAGER).string_build('''
+def collections_transform():
+ return AstroidBuilder(MANAGER).string_build('''
class defaultdict(dict):
default_factory = None
@@ -146,11 +137,9 @@ class deque(object):
''')
- for klass in ('deque', 'defaultdict'):
- module.locals[klass] = fake.locals[klass]
-def pkg_resources_transform(module):
- fake = AstroidBuilder(MANAGER).string_build('''
+def pkg_resources_transform():
+ return AstroidBuilder(MANAGER).string_build('''
def resource_exists(package_or_requirement, resource_name):
pass
@@ -187,11 +176,8 @@ def cleanup_resources(force=False):
''')
- for func_name, func in fake.locals.items():
- module.locals[func_name] = func
-
-def subprocess_transform(module):
+def subprocess_transform():
if PY3K:
communicate = (bytes('string', 'ascii'), bytes('string', 'ascii'))
init = """
@@ -217,7 +203,7 @@ def subprocess_transform(module):
wait_signature = 'def wait(self, timeout=None)'
else:
wait_signature = 'def wait(self)'
- fake = AstroidBuilder(MANAGER).string_build('''
+ return AstroidBuilder(MANAGER).string_build('''
class Popen(object):
returncode = pid = 0
@@ -241,18 +227,17 @@ class Popen(object):
'communicate': communicate,
'wait_signature': wait_signature})
- for func_name, func in fake.locals.items():
- module.locals[func_name] = func
-
-
-
-MODULE_TRANSFORMS['hashlib'] = hashlib_transform
-MODULE_TRANSFORMS['collections'] = collections_transform
-MODULE_TRANSFORMS['pkg_resources'] = pkg_resources_transform
-MODULE_TRANSFORMS['subprocess'] = subprocess_transform
# namedtuple support ###########################################################
+def looks_like_namedtuple(node):
+ func = node.func
+ if type(func) is nodes.Getattr:
+ return func.attrname == 'namedtuple'
+ if type(func) is nodes.Name:
+ return func.name == 'namedtuple'
+ return False
+
def infer_named_tuple(node, context=None):
"""Specific inference function for namedtuple CallFunc node"""
class_node, name, attributes = infer_func_form(node, nodes.Tuple._proxied,
@@ -285,11 +270,12 @@ def infer_enum(node, context=None):
context=context, enum=True)[0]
return iter([class_node.instanciate_class()])
-def infer_enum_class(node, context=None):
+def infer_enum_class(node):
""" Specific inference for enums. """
names = set(('Enum', 'IntEnum', 'enum.Enum', 'enum.IntEnum'))
for basename in node.basenames:
- # TODO: doesn't handle subclasses yet.
+ # TODO: doesn't handle subclasses yet. This implementation
+ # is a hack to support enums.
if basename not in names:
continue
if node.root().name == 'enum':
@@ -299,22 +285,26 @@ def infer_enum_class(node, context=None):
if any(not isinstance(value, nodes.AssName)
for value in values):
continue
- parent = values[0].parent
- real_value = parent.value
+
+ stmt = values[0].statement()
+ if isinstance(stmt.targets[0], nodes.Tuple):
+ targets = stmt.targets[0].itered()
+ else:
+ targets = stmt.targets
+
new_targets = []
- for target in parent.targets:
+ for target in targets:
# Replace all the assignments with our mocked class.
classdef = dedent('''
class %(name)s(object):
@property
def value(self):
- return %(value)s
+ # Not the best return.
+ return None
@property
def name(self):
return %(name)r
- %(name)s = %(value)s
- ''' % {'name': target.name,
- 'value': real_value.as_string()})
+ ''' % {'name': target.name})
fake = AstroidBuilder(MANAGER).string_build(classdef)[target.name]
fake.parent = target.parent
for method in node.mymethods():
@@ -324,8 +314,13 @@ def infer_enum_class(node, context=None):
break
return node
+
MANAGER.register_transform(nodes.CallFunc, inference_tip(infer_named_tuple),
- AsStringRegexpPredicate('namedtuple', 'func'))
+ looks_like_namedtuple)
MANAGER.register_transform(nodes.CallFunc, inference_tip(infer_enum),
AsStringRegexpPredicate('Enum', 'func'))
MANAGER.register_transform(nodes.Class, infer_enum_class)
+register_module_extender(MANAGER, 'hashlib', hashlib_transform)
+register_module_extender(MANAGER, 'collections', collections_transform)
+register_module_extender(MANAGER, 'pkg_resourcds', pkg_resources_transform)
+register_module_extender(MANAGER, 'subprocess', subprocess_transform)
« no previous file with comments | « third_party/logilab/astroid/brain/py2qt4.py ('k') | third_party/logilab/astroid/builder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698