I know this is a hot topic, just from googling it, but I’m trying to come up
with a simple-ish script for my own use to just try to find hints for where an
async function is called where it should be awaited. So, I’m aware that this is
a complex issue, but I’m just trying to help myself find basic mistakes.
Anyway, in a visit_call method, I'm having a hard time inferring method calls
are going. I have a visit_asyncfunctiondef where I index async functions, and a
visit_call where I match functions to my index, but if I have a class with an
async method called "get", I get false positives for any dict.get call, etc in
my visit_call function, I've tried using safe_infer on both node and node.func,
and always get Uninferrable is it possible to figure out what class a method
call is for? I have a pdb.set_trace in my visit_call, and I'm poking all around
the node and node.func trying to figure out how to see what class it's calling
the method from.
Any hints or tips would be greatly appreciated. Here’s the current version of
the plugin code I’ve been hacking on:
import astroid
from pylint.checkers import BaseChecker, utils
from pylint.interfaces import IAstroidChecker
class AsyncAwaitChecker(BaseChecker):
__implements__ = IAstroidChecker
name = "async-without-await"
priority = -1
msgs = {
"W4242": (
"async function called without await", # Display message
"async-without-await", # message-symbol
"function should be called with 'await' keyword", # Message help
)
}
options = {}
def __init__(self, linter=None):
super().__init__(linter)
self._async_nodes = {}
self._calls = {}
def visit_asyncfunctiondef(self, node):
self._async_nodes[node.name] = node
if node.name in self._calls:
self.add_message("async-without-await", node=self._calls[node.name])
def visit_call(self, node):
# called = utils.safe_infer(node.func)
# if isinstance(called, astroid.scoped_nodes.AsyncFunctionDef):
try:
fname = node.func.name
except AttributeError:
fname = node.func.attrname
if fname in self._async_nodes and not isinstance(
node.parent, astroid.node_classes.Await
):
import pdb
pdb.set_trace()
self.add_message("async-without-await", node=node)
def register(linter):
linter.register_checker(AsyncAwaitChecker(linter))
_______________________________________________
code-quality mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/code-quality.python.org/
Member address: [email protected]