From 9b359ad676dff97a35321976c19ca0f6c4fc44ad Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Mon, 12 Sep 2022 03:38:41 -0400 Subject: [PATCH] Fix `unhashable-member` crash when `lambda` used as a dict key (#7454) --- doc/whatsnew/fragments/7453.bugfix | 3 +++ pylint/checkers/utils.py | 2 ++ tests/functional/u/unhashable_member.py | 1 + 3 files changed, 6 insertions(+) create mode 100644 doc/whatsnew/fragments/7453.bugfix diff --git a/doc/whatsnew/fragments/7453.bugfix b/doc/whatsnew/fragments/7453.bugfix new file mode 100644 index 0000000000..94b5240dd4 --- /dev/null +++ b/doc/whatsnew/fragments/7453.bugfix @@ -0,0 +1,3 @@ +Fixed a crash in the ``unhashable-member`` checker when using a ``lambda`` as a dict key. + +Closes #7453 diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 68452db0e4..d00b8a6537 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -1942,6 +1942,8 @@ def is_hashable(node: nodes.NodeNG) -> bool: for inferred in node.infer(): if inferred is astroid.Uninferable: return True + if not hasattr(inferred, "igetattr"): + return True hash_fn = next(inferred.igetattr("__hash__")) if hash_fn.parent is inferred: return True diff --git a/tests/functional/u/unhashable_member.py b/tests/functional/u/unhashable_member.py index 1225543756..a9100b9058 100644 --- a/tests/functional/u/unhashable_member.py +++ b/tests/functional/u/unhashable_member.py @@ -21,3 +21,4 @@ class Unhashable: {[1, 2, 3]} # [unhashable-member] {"tomato": "tomahto"} {dict: {}} +{lambda x: x: "tomato"} # pylint: disable=unnecessary-lambda