| OLD | NEW |
| 1 # Copyright 2012 Google Inc. | 1 # Copyright 2012 Google Inc. |
| 2 # | 2 # |
| 3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr | 3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 4 # This program is free software; you can redistribute it and/or modify it under | 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 | 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 | 6 # Foundation; either version 2 of the License, or (at your option) any later |
| 7 # version. | 7 # version. |
| 8 # | 8 # |
| 9 # This program is distributed in the hope that it will be useful, but WITHOUT | 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 | 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 | 11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details |
| 12 # | 12 # |
| 13 # You should have received a copy of the GNU General Public License along with | 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., | 14 # this program; if not, write to the Free Software Foundation, Inc., |
| 15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 """Checkers for various standard library functions.""" | 16 """Checkers for various standard library functions.""" |
| 17 | 17 |
| 18 import re | 18 import re |
| 19 import sys | 19 import sys |
| 20 | 20 |
| 21 import astroid | 21 import astroid |
| 22 from astroid.bases import Instance |
| 22 | 23 |
| 23 from pylint.interfaces import IAstroidChecker | 24 from pylint.interfaces import IAstroidChecker |
| 24 from pylint.checkers import BaseChecker | 25 from pylint.checkers import BaseChecker |
| 25 from pylint.checkers import utils | 26 from pylint.checkers import utils |
| 26 | 27 |
| 27 _VALID_OPEN_MODE_REGEX = re.compile(r'^(r?U|[rwa]\+?b?)$') | 28 _VALID_OPEN_MODE_REGEX = re.compile(r'^(r?U|[rwa]\+?b?)$') |
| 28 | 29 |
| 29 if sys.version_info >= (3, 0): | 30 if sys.version_info >= (3, 0): |
| 30 OPEN_MODULE = '_io' | 31 OPEN_MODULE = '_io' |
| 31 else: | 32 else: |
| 32 OPEN_MODULE = '__builtin__' | 33 OPEN_MODULE = '__builtin__' |
| 33 | 34 |
| 34 class OpenModeChecker(BaseChecker): | 35 class StdlibChecker(BaseChecker): |
| 35 __implements__ = (IAstroidChecker,) | 36 __implements__ = (IAstroidChecker,) |
| 36 name = 'open_mode' | 37 name = 'stdlib' |
| 37 | 38 |
| 38 msgs = { | 39 msgs = { |
| 39 'W1501': ('"%s" is not a valid mode for open.', | 40 'W1501': ('"%s" is not a valid mode for open.', |
| 40 'bad-open-mode', | 41 'bad-open-mode', |
| 41 'Python supports: r, w, a modes with b, +, and U options. ' | 42 'Python supports: r, w, a modes with b, +, and U options. ' |
| 42 'See http://docs.python.org/2/library/functions.html#open'), | 43 'See http://docs.python.org/2/library/functions.html#open'), |
| 44 'W1502': ('Using datetime.time in a boolean context.', |
| 45 'boolean-datetime', |
| 46 'Using datetetime.time in a boolean context can hide ' |
| 47 'subtle bugs when the time they represent matches ' |
| 48 'midnight UTC. This behaviour was fixed in Python 3.5. ' |
| 49 'See http://bugs.python.org/issue13936 for reference.', |
| 50 {'maxversion': (3, 5)}), |
| 43 } | 51 } |
| 44 | 52 |
| 45 @utils.check_messages('bad-open-mode') | 53 @utils.check_messages('bad-open-mode') |
| 46 def visit_callfunc(self, node): | 54 def visit_callfunc(self, node): |
| 47 """Visit a CallFunc node.""" | 55 """Visit a CallFunc node.""" |
| 48 if hasattr(node, 'func'): | 56 if hasattr(node, 'func'): |
| 49 infer = utils.safe_infer(node.func) | 57 infer = utils.safe_infer(node.func) |
| 50 if infer and infer.root().name == OPEN_MODULE: | 58 if infer and infer.root().name == OPEN_MODULE: |
| 51 if getattr(node.func, 'name', None) in ('open', 'file'): | 59 if getattr(node.func, 'name', None) in ('open', 'file'): |
| 52 self._check_open_mode(node) | 60 self._check_open_mode(node) |
| 53 | 61 |
| 62 @utils.check_messages('boolean-datetime') |
| 63 def visit_unaryop(self, node): |
| 64 if node.op == 'not': |
| 65 self._check_datetime(node.operand) |
| 66 |
| 67 @utils.check_messages('boolean-datetime') |
| 68 def visit_if(self, node): |
| 69 self._check_datetime(node.test) |
| 70 |
| 71 @utils.check_messages('boolean-datetime') |
| 72 def visit_ifexp(self, node): |
| 73 self._check_datetime(node.test) |
| 74 |
| 75 @utils.check_messages('boolean-datetime') |
| 76 def visit_boolop(self, node): |
| 77 for value in node.values: |
| 78 self._check_datetime(value) |
| 79 |
| 80 def _check_datetime(self, node): |
| 81 """ Check that a datetime was infered. |
| 82 If so, emit boolean-datetime warning. |
| 83 """ |
| 84 try: |
| 85 infered = next(node.infer()) |
| 86 except astroid.InferenceError: |
| 87 return |
| 88 if (isinstance(infered, Instance) and |
| 89 infered.qname() == 'datetime.time'): |
| 90 self.add_message('boolean-datetime', node=node) |
| 91 |
| 54 def _check_open_mode(self, node): | 92 def _check_open_mode(self, node): |
| 55 """Check that the mode argument of an open or file call is valid.""" | 93 """Check that the mode argument of an open or file call is valid.""" |
| 56 try: | 94 try: |
| 57 mode_arg = utils.get_argument_from_call(node, position=1, keyword='m
ode') | 95 mode_arg = utils.get_argument_from_call(node, position=1, keyword='m
ode') |
| 58 if mode_arg: | 96 if mode_arg: |
| 59 mode_arg = utils.safe_infer(mode_arg) | 97 mode_arg = utils.safe_infer(mode_arg) |
| 60 if (isinstance(mode_arg, astroid.Const) | 98 if (isinstance(mode_arg, astroid.Const) |
| 61 and not _VALID_OPEN_MODE_REGEX.match(mode_arg.value)): | 99 and not _VALID_OPEN_MODE_REGEX.match(mode_arg.value)): |
| 62 self.add_message('bad-open-mode', node=node, | 100 self.add_message('bad-open-mode', node=node, |
| 63 args=(mode_arg.value)) | 101 args=(mode_arg.value)) |
| 64 except (utils.NoSuchArgumentError, TypeError): | 102 except (utils.NoSuchArgumentError, TypeError): |
| 65 pass | 103 pass |
| 66 | 104 |
| 67 def register(linter): | 105 def register(linter): |
| 68 """required method to auto register this checker """ | 106 """required method to auto register this checker """ |
| 69 linter.register_checker(OpenModeChecker(linter)) | 107 linter.register_checker(StdlibChecker(linter)) |
| 70 | 108 |
| OLD | NEW |