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

Fix return type checkers calls on ellipses functions #5107

Merged
merged 7 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Release date: TBA

* Improve node information for ``invalid-name`` on function argument.

* Prevent return type checkers being called on functions with ellipses as body

Closes #4736


What's New in Pylint 2.11.1?
============================
Expand Down
7 changes: 6 additions & 1 deletion pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
is_attr_protected,
is_builtin_object,
is_comprehension,
is_function_body_ellipsis,
is_iterable,
is_overload_stub,
is_property_setter,
Expand Down Expand Up @@ -2168,7 +2169,11 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:

inferred = _safe_infer_call_result(node, node)
# Only want to check types that we are able to infer
if inferred and node.name in self._protocol_map:
if (
inferred
and node.name in self._protocol_map
and not is_function_body_ellipsis(node)
):
self._protocol_map[node.name](node, inferred)

if node.name in PYMETHODS:
Expand Down
10 changes: 10 additions & 0 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,3 +1566,13 @@ def is_reassigned_after_current(node: nodes.NodeNG, varname: str) -> bool:
a.name == varname and a.lineno > node.lineno
for a in node.scope().nodes_of_class((nodes.AssignName, nodes.FunctionDef))
)


def is_function_body_ellipsis(node: nodes.FunctionDef) -> bool:
"""Checks whether a function body only consisst of a single Ellipsis"""
return (
len(node.body) == 1
and isinstance(node.body[0], nodes.Expr)
and isinstance(node.body[0].value, nodes.Const)
and node.body[0].value.value == Ellipsis
)
44 changes: 44 additions & 0 deletions tests/functional/c/class_protocol_ellipsis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
""""Tests for return type checkers for protocol methods with ellipsis function body"""
# pylint: disable=missing-class-docstring
from typing import Any, Iterator


class MyClass:
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
"""The "invalid-*-returned" messages shouldn't be emitted for stub functions
Original issue: https://github.com/PyCQA/pylint/issues/4736"""

def __len__(self) -> int:
...

def __hash__(self) -> int:
...

def __index__(self) -> int:
...

def __iter__(self) -> Iterator[Any]:
...

def __bool__(self) -> bool:
...

def __repr__(self) -> object:
...

def __str__(self) -> str:
...

def __bytes__(self) -> bytes:
...

def __length_hint__(self) -> int:
...

def __format__(self, format_spec: str) -> str:
...

def __getnewargs__(self) -> tuple:
...

def __getnewargs_ex__(self) -> tuple:
...