|
| 1 | +# Copyright (c) 2003-2015 LOGILAB S.A. (Paris, FRANCE). |
| 2 | +# http://www.logilab.fr/ -- mailto:[email protected] |
| 3 | +# Copyright (c) 2009-2010 Arista Networks, Inc. |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify it under |
| 6 | +# the terms of the GNU General Public License as published by the Free Software |
| 7 | +# Foundation; either version 2 of the License, or (at your option) any later |
| 8 | +# version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License along with |
| 15 | +# this program; if not, write to the Free Software Foundation, Inc., |
| 16 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | +"""Checker for anything related to the async protocol (PEP 492).""" |
| 18 | + |
| 19 | +import astroid |
| 20 | +from astroid import exceptions |
| 21 | +from astroid import helpers |
| 22 | + |
| 23 | +from pylint import checkers |
| 24 | +from pylint.checkers import utils as checker_utils |
| 25 | +from pylint import interfaces |
| 26 | +from pylint import utils |
| 27 | + |
| 28 | + |
| 29 | +class AsyncChecker(checkers.BaseChecker): |
| 30 | + __implements__ = interfaces.IAstroidChecker |
| 31 | + name = 'async' |
| 32 | + msgs = { |
| 33 | + 'E1700': ('Yield inside async function', |
| 34 | + 'yield-inside-async-function', |
| 35 | + 'Used when an `yield` or `yield from` statement is ' |
| 36 | + 'found inside an async function.', |
| 37 | + {'minversion': (3, 5)}), |
| 38 | + 'E1701': ("Async context manager '%s' doesn't implement __aenter__ and __aexit__.", |
| 39 | + 'not-async-context-manager', |
| 40 | + 'Used when an async context manager is used with an object ' |
| 41 | + 'that does not implement the async context management protocol.', |
| 42 | + {'minversion': (3, 5)}), |
| 43 | + } |
| 44 | + |
| 45 | + def open(self): |
| 46 | + self._ignore_mixin_members = utils.get_global_option(self, 'ignore-mixin-members') |
| 47 | + |
| 48 | + @checker_utils.check_messages('yield-inside-async-function') |
| 49 | + def visit_asyncfunctiondef(self, node): |
| 50 | + for child in node.nodes_of_class(astroid.Yield): |
| 51 | + if child.scope() is node: |
| 52 | + self.add_message('yield-inside-async-function', node=child) |
| 53 | + |
| 54 | + @checker_utils.check_messages('not-async-context-manager') |
| 55 | + def visit_asyncwith(self, node): |
| 56 | + for ctx_mgr, _ in node.items: |
| 57 | + infered = helpers.safe_infer(ctx_mgr) |
| 58 | + if infered is None or infered is astroid.YES: |
| 59 | + continue |
| 60 | + |
| 61 | + if isinstance(infered, astroid.Instance): |
| 62 | + try: |
| 63 | + infered.getattr('__aenter__') |
| 64 | + infered.getattr('__aexit__') |
| 65 | + except exceptions.NotFoundError: |
| 66 | + if isinstance(infered, astroid.Instance): |
| 67 | + # If we do not know the bases of this class, |
| 68 | + # just skip it. |
| 69 | + if not helpers.has_known_bases(infered): |
| 70 | + continue |
| 71 | + # Just ignore mixin classes. |
| 72 | + if self._ignore_mixin_members: |
| 73 | + if infered.name[-5:].lower() == 'mixin': |
| 74 | + continue |
| 75 | + else: |
| 76 | + continue |
| 77 | + |
| 78 | + self.add_message('not-async-context-manager', |
| 79 | + node=node, args=(infered.name, )) |
| 80 | + |
| 81 | + |
| 82 | +def register(linter): |
| 83 | + """required method to auto register this checker""" |
| 84 | + linter.register_checker(AsyncChecker(linter)) |
0 commit comments