jendrikseipp / vulture

Find dead Python code
MIT License
3.48k stars 152 forks source link

Add support for TypedDict #249

Closed ZeeD closed 1 year ago

ZeeD commented 3 years ago

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

RJ722 commented 3 years ago

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.

jendrikseipp commented 3 years ago

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

mariuswallraff commented 2 years ago

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. 🙂

janosh commented 2 years ago

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
    ...
jendrikseipp commented 1 year ago

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