vulture icon indicating copy to clipboard operation
vulture copied to clipboard

Add support for TypedDict

Open ZeeD opened this issue 4 years ago • 4 comments

snippet:

from typing import TypedDict

class C(TypedDict):
    key: int

c: C = {'key': 0}
print(c['key'])

vulture says unused variable 'key' (60% confidence), but shouldn't

ZeeD avatar Feb 12 '21 11:02 ZeeD

I agree that this should be done, but I'm not sure how complicated would it be. I'll take a look into it. A lot of things on my plate right now, so it will take some time.

RJ722 avatar Mar 11 '21 09:03 RJ722

I don't see an easy way to remove the false positive. Suggestions welcome :-)

jendrikseipp avatar Mar 11 '21 10:03 jendrikseipp

No suggestion, but for other people landing here:
A workaround is to use the function-call syntax like this: C = TypedDict("C", {"key": int})

You can't use the nicer-looking keyword syntax, because then mypy is going to complain again. 🙂

mariuswallraff avatar Aug 12 '22 16:08 mariuswallraff

You can also noqa all the attributes though like the readme says, it ain't pretty:

class ProcessResponse(TypedDict):
    timer: str  # noqa: F841
    status: str  # noqa: F841
    download_filename: str  # noqa: F841
    filesize: int  # noqa: F841
    ...

janosh avatar Sep 04 '22 03:09 janosh

Thanks for the suggestions! I'd use a whitelist module for this. I don't see a way to bake support for this into Vulture.

Example whitelist module:

from myfile import C
C.key

jendrikseipp avatar Jan 08 '23 18:01 jendrikseipp