Skip to content

Commit

Permalink
Refactor various typing related issues (#4940)
Browse files Browse the repository at this point in the history
* Add type annotations to ``visit`` & ``leave`` calls
This adds typing to most calls that visit nodes. All other changes are
due to mypy errors resulting from introduction of typing.

* Fix outstanding mypy issues
This removes some of the `type: ignore` comments in favour of
solving the mypy issues these comments were surpressing.

* Fix remaining references to node_classes
Except for two references to node_classes in the changelog this should be the last of them

Co-authored-by: Marc Mueller <[email protected]>
Co-authored-by: Pierre Sassoulas <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2021
1 parent 0981d8b commit baaa81a
Show file tree
Hide file tree
Showing 47 changed files with 375 additions and 349 deletions.
16 changes: 9 additions & 7 deletions doc/how_tos/custom_checkers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ Firstly we will need to fill in some required boilerplate:
.. code-block:: python
import astroid
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from pylint.lint import PyLinter
class UniqueReturnChecker(BaseChecker):
__implements__ = IAstroidChecker
Expand Down Expand Up @@ -117,14 +119,14 @@ Next we'll track when we enter and leave a function.

.. code-block:: python
def __init__(self, linter=None):
def __init__(self, linter: PyLinter =None) -> None:
super(UniqueReturnChecker, self).__init__(linter)
self._function_stack = []
def visit_functiondef(self, node):
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
self._function_stack.append([])
def leave_functiondef(self, node):
def leave_functiondef(self, node: nodes.FunctionDef) -> None:
self._function_stack.pop()
In the constructor we initialise a stack to keep a list of return nodes
Expand All @@ -138,13 +140,13 @@ and to remove the list of return nodes when we leave the function.

Finally we'll implement the check.
We will define a ``visit_return`` function,
which is called with a :class:`.astroid.node_classes.Return` node.
which is called with a :class:`.astroid.nodes.Return` node.

.. _astroid_extract_node:
.. TODO We can shorten/remove this bit once astroid has API docs.
We'll need to be able to figure out what attributes a
:class:`.astroid.node_classes.Return` node has available.
:class:`.astroid.nodes.Return` node has available.
We can use :func:`astroid.extract_node` for this::

>>> node = astroid.extract_node("return 5")
Expand Down Expand Up @@ -178,8 +180,8 @@ Now we know how to use the astroid node, we can implement our check.

.. code-block:: python
def visit_return(self, node):
if not isinstance(node.value, astroid.node_classes.Const):
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.value, nodes.Const):
return
for other_return in self._function_stack[-1]:
Expand Down
4 changes: 2 additions & 2 deletions pylint/checkers/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def open(self):
self._async_generators = ["contextlib.asynccontextmanager"]

@checker_utils.check_messages("yield-inside-async-function")
def visit_asyncfunctiondef(self, node):
def visit_asyncfunctiondef(self, node: nodes.AsyncFunctionDef) -> None:
for child in node.nodes_of_class(nodes.Yield):
if child.scope() is node and (
sys.version_info[:2] == (3, 5) or isinstance(child, nodes.YieldFrom)
):
self.add_message("yield-inside-async-function", node=child)

@checker_utils.check_messages("not-async-context-manager")
def visit_asyncwith(self, node):
def visit_asyncwith(self, node: nodes.AsyncWith) -> None:
for ctx_mgr, _ in node.items:
inferred = checker_utils.safe_infer(ctx_mgr)
if inferred is None or inferred is astroid.Uninferable:
Expand Down
Loading

0 comments on commit baaa81a

Please sign in to comment.