diff --git a/doc/whatsnew/fragments/7501.false_positive b/doc/whatsnew/fragments/7501.false_positive new file mode 100644 index 0000000000..0c2d33a075 --- /dev/null +++ b/doc/whatsnew/fragments/7501.false_positive @@ -0,0 +1,3 @@ +Fix false positive for ``unhashable-member`` when subclassing ``dict`` and using the subclass as a dictionary key. + +Closes #7501 diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index ca3d0e362e..0e99b558a9 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -2012,7 +2012,7 @@ def is_hashable(node: nodes.NodeNG) -> bool: """ try: for inferred in node.infer(): - if inferred is astroid.Uninferable: + if inferred is astroid.Uninferable or isinstance(inferred, nodes.ClassDef): return True if not hasattr(inferred, "igetattr"): return True diff --git a/tests/functional/u/unhashable_member.py b/tests/functional/u/unhashable_member.py index a9100b9058..c788668c47 100644 --- a/tests/functional/u/unhashable_member.py +++ b/tests/functional/u/unhashable_member.py @@ -22,3 +22,9 @@ class Unhashable: {"tomato": "tomahto"} {dict: {}} {lambda x: x: "tomato"} # pylint: disable=unnecessary-lambda + + +class FromDict(dict): + ... + +{FromDict: 1}