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

Side by Side Diff: third_party/pylint/pylint/checkers/stdlib.py

Issue 1920403002: [content/test/gpu] Run pylint check of gpu tests in unittest instead of PRESUBMIT (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update path to LICENSE.txt of logilab/README.chromium Created 4 years, 7 months 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 unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2012 Google Inc.
2 #
3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by the Free Software
6 # Foundation; either version 2 of the License, or (at your option) any later
7 # version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
12 #
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 """Checkers for various standard library functions."""
17
18 import re
19 import six
20 import sys
21
22 import astroid
23 from astroid.bases import Instance
24
25 from pylint.interfaces import IAstroidChecker
26 from pylint.checkers import BaseChecker
27 from pylint.checkers import utils
28
29
30 if sys.version_info >= (3, 0):
31 OPEN_MODULE = '_io'
32 else:
33 OPEN_MODULE = '__builtin__'
34
35
36 def _check_mode_str(mode):
37 # check type
38 if not isinstance(mode, six.string_types):
39 return False
40 # check syntax
41 modes = set(mode)
42 _mode = "rwatb+U"
43 creating = False
44 if six.PY3:
45 _mode += "x"
46 creating = "x" in modes
47 if modes - set(_mode) or len(mode) > len(modes):
48 return False
49 # check logic
50 reading = "r" in modes
51 writing = "w" in modes
52 appending = "a" in modes
53 updating = "+" in modes
54 text = "t" in modes
55 binary = "b" in modes
56 if "U" in modes:
57 if writing or appending or creating and six.PY3:
58 return False
59 reading = True
60 if not six.PY3:
61 binary = True
62 if text and binary:
63 return False
64 total = reading + writing + appending + (creating if six.PY3 else 0)
65 if total > 1:
66 return False
67 if not (reading or writing or appending or creating and six.PY3):
68 return False
69 # other 2.x constraints
70 if not six.PY3:
71 if "U" in mode:
72 mode = mode.replace("U", "")
73 if "r" not in mode:
74 mode = "r" + mode
75 return mode[0] in ("r", "w", "a", "U")
76 return True
77
78
79 class StdlibChecker(BaseChecker):
80 __implements__ = (IAstroidChecker,)
81 name = 'stdlib'
82
83 msgs = {
84 'W1501': ('"%s" is not a valid mode for open.',
85 'bad-open-mode',
86 'Python supports: r, w, a[, x] modes with b, +, '
87 'and U (only with r) options. '
88 'See http://docs.python.org/2/library/functions.html#open'),
89 'W1502': ('Using datetime.time in a boolean context.',
90 'boolean-datetime',
91 'Using datetetime.time in a boolean context can hide '
92 'subtle bugs when the time they represent matches '
93 'midnight UTC. This behaviour was fixed in Python 3.5. '
94 'See http://bugs.python.org/issue13936 for reference.',
95 {'maxversion': (3, 5)}),
96 'W1503': ('Redundant use of %s with constant '
97 'value %r',
98 'redundant-unittest-assert',
99 'The first argument of assertTrue and assertFalse is'
100 'a condition. If a constant is passed as parameter, that'
101 'condition will be always true. In this case a warning '
102 'should be emitted.')
103 }
104
105 @utils.check_messages('bad-open-mode', 'redundant-unittest-assert')
106 def visit_callfunc(self, node):
107 """Visit a CallFunc node."""
108 if hasattr(node, 'func'):
109 infer = utils.safe_infer(node.func)
110 if infer:
111 if infer.root().name == OPEN_MODULE:
112 if getattr(node.func, 'name', None) in ('open', 'file'):
113 self._check_open_mode(node)
114 if infer.root().name == 'unittest.case':
115 self._check_redundant_assert(node, infer)
116
117 @utils.check_messages('boolean-datetime')
118 def visit_unaryop(self, node):
119 if node.op == 'not':
120 self._check_datetime(node.operand)
121
122 @utils.check_messages('boolean-datetime')
123 def visit_if(self, node):
124 self._check_datetime(node.test)
125
126 @utils.check_messages('boolean-datetime')
127 def visit_ifexp(self, node):
128 self._check_datetime(node.test)
129
130 @utils.check_messages('boolean-datetime')
131 def visit_boolop(self, node):
132 for value in node.values:
133 self._check_datetime(value)
134
135 def _check_redundant_assert(self, node, infer):
136 if (isinstance(infer, astroid.BoundMethod) and
137 node.args and isinstance(node.args[0], astroid.Const) and
138 infer.name in ['assertTrue', 'assertFalse']):
139 self.add_message('redundant-unittest-assert',
140 args=(infer.name, node.args[0].value, ),
141 node=node)
142
143 def _check_datetime(self, node):
144 """ Check that a datetime was infered.
145 If so, emit boolean-datetime warning.
146 """
147 try:
148 infered = next(node.infer())
149 except astroid.InferenceError:
150 return
151 if (isinstance(infered, Instance) and
152 infered.qname() == 'datetime.time'):
153 self.add_message('boolean-datetime', node=node)
154
155
156 def _check_open_mode(self, node):
157 """Check that the mode argument of an open or file call is valid."""
158 try:
159 mode_arg = utils.get_argument_from_call(node, position=1,
160 keyword='mode')
161 except utils.NoSuchArgumentError:
162 return
163 if mode_arg:
164 mode_arg = utils.safe_infer(mode_arg)
165 if (isinstance(mode_arg, astroid.Const)
166 and not _check_mode_str(mode_arg.value)):
167 self.add_message('bad-open-mode', node=node,
168 args=mode_arg.value)
169
170
171 def register(linter):
172 """required method to auto register this checker """
173 linter.register_checker(StdlibChecker(linter))
OLDNEW
« no previous file with comments | « third_party/pylint/pylint/checkers/spelling.py ('k') | third_party/pylint/pylint/checkers/strings.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698