diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-09-03 13:47:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-03 13:47:23 +0200 |
commit | baaa81a2994cdd517fbde8693b0a4b0a67f5a4e3 (patch) | |
tree | c8b26e345fb3c7377a6c8e3a2b7afca28e1806e5 /doc | |
parent | 0981d8bec52f0917168e0e89947fe164f58be683 (diff) | |
download | pylint-git-baaa81a2994cdd517fbde8693b0a4b0a67f5a4e3.tar.gz |
Refactor various typing related issues (#4940)
* 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 <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/how_tos/custom_checkers.rst | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/doc/how_tos/custom_checkers.rst b/doc/how_tos/custom_checkers.rst index add8e1a69..c5deddd94 100644 --- a/doc/how_tos/custom_checkers.rst +++ b/doc/how_tos/custom_checkers.rst @@ -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 @@ -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 @@ -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") @@ -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]: |