third_party.pylibs.pylint.src/pylint/checkers/async.py

96 lines
3.8 KiB
Python
Raw Normal View History

2018-09-16 15:33:50 +00:00
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
2018-09-16 15:33:50 +00:00
"""Checker for anything related to the async protocol (PEP 492)."""
import sys
from typing import TYPE_CHECKING
2018-09-16 15:33:50 +00:00
import astroid
from astroid import nodes
2018-09-16 15:33:50 +00:00
from pylint import checkers, interfaces, utils
2018-09-16 15:33:50 +00:00
from pylint.checkers import utils as checker_utils
from pylint.checkers.utils import decorated_with
2018-09-16 15:33:50 +00:00
if TYPE_CHECKING:
from pylint.lint import PyLinter
2018-09-16 15:33:50 +00:00
class AsyncChecker(checkers.BaseChecker):
__implements__ = interfaces.IAstroidChecker
name = "async"
msgs = {
"E1700": (
"Yield inside async function",
"yield-inside-async-function",
"Used when an `yield` or `yield from` statement is "
"found inside an async function.",
{"minversion": (3, 5)},
),
"E1701": (
"Async context manager '%s' doesn't implement __aenter__ and __aexit__.",
"not-async-context-manager",
"Used when an async context manager is used with an object "
"that does not implement the async context management protocol.",
{"minversion": (3, 5)},
),
}
def open(self):
self._mixin_class_rgx = utils.get_global_option(self, "mixin-class-rgx")
self._async_generators = ["contextlib.asynccontextmanager"]
2018-09-16 15:33:50 +00:00
@checker_utils.check_messages("yield-inside-async-function")
def visit_asyncfunctiondef(self, node: nodes.AsyncFunctionDef) -> None:
for child in node.nodes_of_class(nodes.Yield):
2018-09-16 15:33:50 +00:00
if child.scope() is node and (
sys.version_info[:2] == (3, 5) or isinstance(child, nodes.YieldFrom)
2018-09-16 15:33:50 +00:00
):
self.add_message("yield-inside-async-function", node=child)
@checker_utils.check_messages("not-async-context-manager")
def visit_asyncwith(self, node: nodes.AsyncWith) -> None:
2018-09-16 15:33:50 +00:00
for ctx_mgr, _ in node.items:
inferred = checker_utils.safe_infer(ctx_mgr)
if inferred is None or inferred is astroid.Uninferable:
2018-09-16 15:33:50 +00:00
continue
if isinstance(inferred, nodes.AsyncFunctionDef):
# Check if we are dealing with a function decorated
# with contextlib.asynccontextmanager.
if decorated_with(inferred, self._async_generators):
continue
elif isinstance(inferred, astroid.bases.AsyncGenerator):
# Check if we are dealing with a function decorated
# with contextlib.asynccontextmanager.
if decorated_with(inferred.parent, self._async_generators):
continue
else:
2018-09-16 15:33:50 +00:00
try:
inferred.getattr("__aenter__")
inferred.getattr("__aexit__")
except astroid.exceptions.NotFoundError:
if isinstance(inferred, astroid.Instance):
2018-09-16 15:33:50 +00:00
# If we do not know the bases of this class,
# just skip it.
if not checker_utils.has_known_bases(inferred):
2018-09-16 15:33:50 +00:00
continue
# Ignore mixin classes if they match the rgx option.
if (
"not-async-context-manager"
in self.linter.namespace.ignored_checks_for_mixins
and self._mixin_class_rgx.match(inferred.name)
):
continue
2018-09-16 15:33:50 +00:00
else:
continue
self.add_message(
"not-async-context-manager", node=node, args=(inferred.name,)
2018-09-16 15:33:50 +00:00
)
def register(linter: "PyLinter") -> None:
2018-09-16 15:33:50 +00:00
linter.register_checker(AsyncChecker(linter))