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 1 commit
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
13 changes: 12 additions & 1 deletion pylint/checkers/utils.py
Expand Up @@ -19,7 +19,7 @@

import _string
import astroid.objects
from astroid import TooManyLevelsError, nodes
from astroid import TooManyLevelsError, bases, nodes
from astroid.context import InferenceContext
from astroid.exceptions import AstroidError
from astroid.nodes._base_nodes import ImportNode
Expand All @@ -38,6 +38,15 @@
nodes.DictComp,
nodes.GeneratorExp,
)
WITH_IGETATTR = (
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it possible to use has_attr("igetattr" and have something generic instead ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I just figured this was more explicit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Old python is more duck-type-y, and modern python seems to be more explicit. πŸ€·πŸ»β€β™‚οΈ

Copy link
Member

Choose a reason for hiding this comment

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

this was more explicit.

Sure but if another class with igetattr appear we'd need to remember to add it there, which is unlikely to be caught in review, right ?

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense, will change.

nodes.Module,
nodes.ClassDef,
nodes.FunctionDef,
astroid.objects.Super,
nodes.Slice,
bases.UnboundMethod,
bases.BaseInstance,
)
EXCEPTIONS_MODULE = "builtins"
ABC_MODULES = {"abc", "_py_abc"}
ABC_METHODS = {
Expand Down Expand Up @@ -1950,6 +1959,8 @@ def is_hashable(node: nodes.NodeNG) -> bool:
for inferred in node.infer():
if inferred is astroid.Uninferable:
return True
if not isinstance(inferred, WITH_IGETATTR):
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