summaryrefslogtreecommitdiff
path: root/checkers/variables.py
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2014-08-11 18:49:31 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2014-08-11 18:49:31 +0300
commit383df7317eb572747867e10ecff6371954fee1c0 (patch)
tree026ea2bea0940b2fc7e023087c5cb5d185a0dd81 /checkers/variables.py
parentd3753607dd6be1073feda038e3f2f3e99feb7f39 (diff)
downloadpylint-git-383df7317eb572747867e10ecff6371954fee1c0.tar.gz
Don't emit 'unused-import' when a special object is imported (__all__, __doc__ etc.). Closes issue #309.
Diffstat (limited to 'checkers/variables.py')
-rw-r--r--checkers/variables.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/checkers/variables.py b/checkers/variables.py
index 747195b5f..a88bf3b9c 100644
--- a/checkers/variables.py
+++ b/checkers/variables.py
@@ -17,6 +17,7 @@
"""
import os
import sys
+import re
from copy import copy
import astroid
@@ -33,6 +34,8 @@ from pylint.checkers.utils import (
assign_parent, check_messages, is_inside_except, clobber_in_except,
get_all_elements)
+SPECIAL_OBJ = re.compile("^_{2}[a-z]+_{2}$")
+
def in_for_else_branch(parent, stmt):
"""Returns True if stmt in inside the else branch for a parent For stmt."""
@@ -367,12 +370,21 @@ builtins. Remember that you should avoid to define new builtins when possible.'
if (isinstance(stmt, astroid.Import) or
(isinstance(stmt, astroid.From) and
not stmt.modname)):
+ if (isinstance(stmt, astroid.From) and
+ SPECIAL_OBJ.search(imported_name)):
+ # Filter special objects (__doc__, __all__) etc.,
+ # because they can be imported for exporting.
+ continue
if as_name is None:
msg = "import %s" % imported_name
else:
msg = "%s imported as %s" % (imported_name, as_name)
self.add_message('unused-import', args=msg, node=stmt)
elif isinstance(stmt, astroid.From) and stmt.modname != '__future__':
+ if SPECIAL_OBJ.search(imported_name):
+ # Filter special objects (__doc__, __all__) etc.,
+ # because they can be imported for exporting.
+ continue
if imported_name == '*':
self.add_message('unused-wildcard-import',
args=name, node=stmt)