for now, it catches the following incorrect function: async def show_foo() -> Awaitable[None]: return show_something_else("foo") because to correctly show the result, the caller would have to "await (await show_foo())" (this should either be "async def show_foo() -> None", or "def show_foo() -> Awaitable[None]")pull/3426/head
parent
3d356b25ab
commit
f0d6539d3d
@ -0,0 +1,2 @@
|
|||||||
|
*.egg-info
|
||||||
|
/build
|
@ -0,0 +1,23 @@
|
|||||||
|
# Custom Pylint rule checker
|
||||||
|
|
||||||
|
For now, it catches the following problem (`async-awaitable-return`):
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def show_foo() -> Awaitable[None]:
|
||||||
|
return show_something("foo")
|
||||||
|
```
|
||||||
|
|
||||||
|
This is almost certainly a mistake -- the caller would need to say `await (await
|
||||||
|
show_foo())` to actually show the foo.
|
||||||
|
|
||||||
|
The function should be one of:
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def show_foo() -> None:
|
||||||
|
return await show_something("foo")
|
||||||
|
|
||||||
|
# ... or ...
|
||||||
|
|
||||||
|
def show_foo() -> Awaitable[None]:
|
||||||
|
return show_something("foo")
|
||||||
|
```
|
@ -0,0 +1,15 @@
|
|||||||
|
[tool.poetry]
|
||||||
|
name = "trezor-pylint-plugin"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
authors = ["matejcik <ja@matejcik.cz>"]
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
[tool.poetry.dependencies]
|
||||||
|
python = "^3.8"
|
||||||
|
pylint = "*"
|
||||||
|
astroid = "*"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry-core"]
|
||||||
|
build-backend = "poetry.core.masonry.api"
|
@ -0,0 +1,29 @@
|
|||||||
|
from astroid import AsyncFunctionDef
|
||||||
|
from pylint.checkers import BaseChecker
|
||||||
|
from pylint.checkers.utils import check_messages
|
||||||
|
from pylint.interfaces import IAstroidChecker
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncAwaitableChecker(BaseChecker):
|
||||||
|
__implements__ = IAstroidChecker
|
||||||
|
|
||||||
|
name = "async-awaitable-checker"
|
||||||
|
priority = -1
|
||||||
|
msgs = {
|
||||||
|
"W9999": (
|
||||||
|
'Async function "%s" is likely not meant to return an Awaitable.',
|
||||||
|
"async-awaitable-return",
|
||||||
|
"Used when an async function returns an Awaitable instead of the result.",
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
@check_messages("async-awaitable-return")
|
||||||
|
def visit_asyncfunctiondef(self, node: AsyncFunctionDef):
|
||||||
|
# Check if the return type is explicitly an Awaitable
|
||||||
|
if node.returns and "Awaitable" in node.returns.as_string():
|
||||||
|
self.add_message("async-awaitable-return", node=node, args=(node.name,))
|
||||||
|
|
||||||
|
|
||||||
|
def register(linter):
|
||||||
|
"""Required method to auto register this checker."""
|
||||||
|
linter.register_checker(AsyncAwaitableChecker(linter))
|
Loading…
Reference in new issue