Skip to content

Fix #5557: Don't emit comparison-with-callable if the callable raises #5563

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

Merged
merged 6 commits into from
Dec 21, 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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Release date: TBA

Closes #3675

* Fix ``comparison-with-callable`` false positive for callables that raise, such
as typing constants.

Closes #5557

* Fix ``unnecessary_dict_index_lookup`` false positive when deleting a dictionary's entry.

Closes #4716
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ Other Changes

* ``fatal`` was added to the variables permitted in score evaluation expressions.

* Fix ``comparison-with-callable`` false positive for callables that raise, such
as typing constants.

Closes #5557

* The ``PyLinter`` class will now be initialized with a ``TextReporter``
as its reporter if none is provided.

Expand Down
20 changes: 12 additions & 8 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2511,14 +2511,18 @@ def _check_callable_comparison(self, node):
left_operand, right_operand = node.left, node.ops[0][1]
# this message should be emitted only when there is comparison of bare callable
# with non bare callable.
if (
sum(
1
for operand in (left_operand, right_operand)
if isinstance(utils.safe_infer(operand), bare_callables)
)
== 1
):
number_of_bare_callables = 0
for operand in left_operand, right_operand:
inferred = utils.safe_infer(operand)
# Ignore callables that raise, as well as typing constants
# implemented as functions (that raise via their decorator)
if (
isinstance(inferred, bare_callables)
and "typing._SpecialForm" not in inferred.decoratornames()
and not any(isinstance(x, nodes.Raise) for x in inferred.body)
):
number_of_bare_callables += 1
if number_of_bare_callables == 1:
self.add_message("comparison-with-callable", node=node)

@utils.check_messages(
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/c/comparison_with_callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ def fake_property(self, prop):
b = 786
if a == b:
pass


def eventually_raise():
print()
raise Exception


if a == eventually_raise:
# Does not emit comparison-with-callable because the
# function (eventually) raises
pass
18 changes: 18 additions & 0 deletions tests/functional/c/comparison_with_callable_typing_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Typing constants are actually implemented as functions, but they
raise when called, so Pylint uses that to avoid false positives for
comparison-with-callable.
"""
from typing import Any, Optional


def check_any(type_) -> bool:
"""See https://github.com/PyCQA/pylint/issues/5557"""
return type_ == Any


def check_optional(type_) -> bool:
"""
Unlike Any, Optional does not raise in its body.
It raises via its decorator: typing._SpecialForm.__call__()
"""
return type_ == Optional