Skip to content

Commit

Permalink
Update to be compatible with Pylint 2.1.1 and Python 3
Browse files Browse the repository at this point in the history
When checking calls to the builtin open function without manager we now
have to check that the module of the 'open' function is '_io'.

We remove the 'file' definition and its corresponding 'builtin-name-used' expectation in 'builtin-name-used.py' since 'file'is no longer a builtin in Python 3.

We replace the Python 2 'print' statements by calls to the Python 3
'print' function.

The Tokenize library used by Pylint now adds an 'ENCODING' token to the
start of the token list when tokenizing a file. We have to ignore this
token since it isn't representing any line of code.
  • Loading branch information
fbessou committed Sep 12, 2018
1 parent 890cd42 commit 8f4cfe4
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
5 changes: 4 additions & 1 deletion checkers/cnes_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pylint.checkers.utils import check_messages
from pylint.checkers.raw_metrics import get_type
from pylint.utils import WarningScope
import tokenize


class DesignChecker(BaseChecker):
Expand Down Expand Up @@ -100,7 +101,7 @@ def visit_call(self, node):
if funcdef.name == 'open':
parent = funcdef.parent
if (isinstance(parent, astroid.Module)
and parent.name == '__builtin__'):
and parent.name == '_io'):
if not isinstance(node.parent, astroid.With):
self.add_message('use-context-manager', node=node,
args='opening the file')
Expand Down Expand Up @@ -216,6 +217,8 @@ def process_tokens(self, tokens):
"""update stats"""
i = 0
tokens = list(tokens)
if tokens[0].type == tokenize.ENCODING:
i = 1
tail = None
while i < len(tokens):
start_line = tokens[i][2][0]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/builtin_name_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class MyClass(object):
def __init__(self):
((self.file, (self.str, self.something)), plop) = (('test.txt', 'hello'), 'plop') # [builtin-name-used, builtin-name-used]
((self.str, self.something), plop) = (('test.txt', 'hello'), 'plop') # [builtin-name-used]

bool = True # [builtin-name-used]

Expand Down
1 change: 0 additions & 1 deletion test/functional/builtin_name_used.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
builtin-name-used:6:MyClass.__init__:file is named after a built-in, consider renaming it
builtin-name-used:6:MyClass.__init__:str is named after a built-in, consider renaming it
builtin-name-used:8:MyClass:bool is named after a built-in, consider renaming it
builtin-name-used:10:MyClass.map:map is named after a built-in, consider renaming it
Expand Down
2 changes: 1 addition & 1 deletion test/functional/os_environ_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def function1():
a = os.environ # [os-environ-used]

print e # [os-environ-used]
print(e) # [os-environ-used]
env = o.getenv() # [os-environ-used]
o.putenv('TOTO', 'titi') # [os-environ-used]
sep = o.sep
Expand Down
2 changes: 1 addition & 1 deletion test/functional/sys_argv_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def function1():
a = s.argv[0] # [sys-argv-used]

print a # [sys-argv-used]
print(a) # [sys-argv-used]

if len(sys.argv > 1): # [sys-argv-used]
param = sys.argv[1] # [sys-argv-used]
Expand Down

0 comments on commit 8f4cfe4

Please sign in to comment.