From fa618cb7ce74a423e4be8a5da7dedc3c3fd6d383 Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Mon, 14 Nov 2022 10:23:48 +0100 Subject: [PATCH] Fix false positive for ``unhashable-member`` when subclassing ``dict``. (#7757) Closes #7501 Co-authored-by: Pierre Sassoulas --- doc/whatsnew/fragments/7501.false_positive | 3 +++ pylint/checkers/utils.py | 2 +- tests/functional/u/unhashable_member.py | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/7501.false_positive 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 b7e0596d16..0ccf1d883d 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -1955,7 +1955,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}