pylint icon indicating copy to clipboard operation
pylint copied to clipboard

False positive E1143 (unhashable-member) when creating a set with typing classes as elements

Open mauvilsa opened this issue 3 years ago • 2 comments

Bug description

Creating a set with elements a class from the typing module. For example below using a List.

# pylint: disable=missing-module-docstring,pointless-statement

from typing import List

{List}

The same happens with Set, Dict, DefaultDict, OrderedDict, Counter and TypedDict. I tried a few other classes from typing which didn't give a false positive. Note that these classes are hashable and using them as key in a dict access, e.g. some_dict[List] does not show a false positive.

Configuration

No response

Command used

pylint unhashable_member_false_positive.py

Pylint output

************* Module unhashable_member_false_positive
unhashable_member_false_positive.py:5:1: E1143: 'List' is unhashable and can't be used as a member in a set (unhashable-member)

Expected behavior

No E1143 error should be shown.

Pylint version

pylint 2.15.0
astroid 2.12.5
Python 3.8.10 (default, Jun 22 2022, 20:18:18) 
[GCC 9.4.0]

OS / Environment

Ubuntu 20.04

Additional dependencies

No response

mauvilsa avatar Sep 01 '22 19:09 mauvilsa

This requires a change in the astroid typing brain. We needs to add the __hash__ dunder to some classdefs I think.

DanielNoord avatar Sep 05 '22 07:09 DanielNoord

I also noticed something similar occurring for classes built with Pydantic BaseModel. It uses a metaclass, which might be the issue.

from pydantic import BaseModel

class Foo(BaseModel):
     pass

test = { Foo: "bar" }

tl24 avatar Sep 09 '22 18:09 tl24

This one is also fixed by https://github.com/PyCQA/pylint/issues/7501 (including the Pydantic example)!

from typing import DefaultDict, Counter, Dict, OrderedDict, Set, List, TypedDict
from pydantic import BaseModel

{Counter, DefaultDict, Dict, List, OrderedDict, Set, TypedDict}

class Foo(BaseModel):
     pass

test = { Foo: "bar" }

pylint example.py --extension-pkg-whitelist='pydantic'

mbyrnepr2 avatar Nov 15 '22 20:11 mbyrnepr2