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 unhashable-member crash when lambda used as a dict key #7454

Merged
merged 2 commits into from Sep 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions pylint/checkers/utils.py
Expand Up @@ -1950,6 +1950,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"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy will complain about this when we add py.typed to astroid, but that is a problem to fix then I think.

return True
hash_fn = next(inferred.igetattr("__hash__"))
if hash_fn.parent is inferred:
return True
Expand Down
1 change: 1 addition & 0 deletions tests/functional/u/unhashable_member.py
Expand Up @@ -21,3 +21,4 @@ class Unhashable:
{[1, 2, 3]} # [unhashable-member]
{"tomato": "tomahto"}
{dict: {}}
{lambda x: x: "tomato"} # pylint: disable=unnecessary-lambda