Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor various typing related issues #4940

Merged
merged 21 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5a1e21d
Add type annotations to ``visit`` & ``leave`` calls
DanielNoord Aug 31, 2021
c81f02f
Fix outstanding mypy issues
DanielNoord Aug 31, 2021
8ea6f70
Fix remaining references to node_classes
DanielNoord Aug 31, 2021
7d2062c
Update typing and mypy configuration
DanielNoord Aug 31, 2021
3fe7bc4
Fix cyclic import in `deprecated.py`
DanielNoord Aug 31, 2021
6bbf2b3
Apply suggestions from code review
DanielNoord Aug 31, 2021
c004d75
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 31, 2021
596fc28
Apply suggestions from code review
DanielNoord Aug 31, 2021
c45ac73
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 31, 2021
1f2d7a7
Add changes from code review
DanielNoord Aug 31, 2021
3c31ed0
Additional typing
DanielNoord Aug 31, 2021
8dbb8fb
Merge branch 'main' of https://github.com/PyCQA/pylint into typing
DanielNoord Sep 1, 2021
8dd90cd
Apply suggestions from code review
DanielNoord Sep 1, 2021
4bf1c59
Merge branch 'main' into typing
DanielNoord Sep 1, 2021
3a47b64
Implement code review
DanielNoord Sep 1, 2021
49aa00c
Fix test
DanielNoord Sep 1, 2021
ce1aa3d
Reverse removal of ``visit_package``
DanielNoord Sep 1, 2021
c2299df
Merge branch 'main' into typing
DanielNoord Sep 2, 2021
1bfe966
Update import of ``objects.ExceptionInstance``
DanielNoord Sep 2, 2021
4e99175
Merge branch 'main' of https://github.com/PyCQA/pylint into typing
DanielNoord Sep 2, 2021
7aeb8c4
Merge branch 'typing' of https://github.com/DanielNoord/pylint into t…
DanielNoord Sep 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
super(UniqueReturnChecker, self).__init__(linter)
self._function_stack = []

def visit_functiondef(self, node):
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
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