jendrikseipp / vulture

Find dead Python code
MIT License
3.41k stars 150 forks source link

False-positives for pydantic #231

Closed MartinThoma closed 3 years ago

MartinThoma commented 4 years ago

Pydantic is a pretty popular data parsing and validation framework. Classes which inherit from pydantic.BaseModel can have a Config class inside them which modify the behavior of pydantic (example)

Vulture 2.1 falsely recognizes this as unused code.

RJ722 commented 3 years ago

Since all of these are used by Pydantic dynamically, I don't see an easy way to get around this. You'd need to create and maintain a whitelist to prevent all the false positives. Vulture can help you with a bit of automation here:

After looking at the results from Vulture and ensuring that there are no more false positives, it can automatically create a whitelist using the --make-whitelist option:

vulture dir/ --make-whitelist > whitelist.py

For example, I tried running this on the example linked above, and it spewed out the following:

$ vulture pyd.py
/private/tmp/pyd.py:9: unused variable 'name' (60% confidence)
/private/tmp/pyd.py:12: unused class 'Config' (60% confidence)
/private/tmp/pyd.py:13: unused variable 'alias_generator' (60% confidence)

$ vulture pyd.py --make-whitelist > whitelist.py

$ cat whitelist.py
name  # unused variable (/private/tmp/pyd.py:9)
Config  # unused class (/private/tmp/pyd.py:12)
alias_generator  # unused variable (/private/tmp/pyd.py:13)

$ vulture pyd.py whitelist.py
jendrikseipp commented 3 years ago

I agree with @RJ722 . A whitelist file is your best option. I recommend using "real" code in the whitelist file, so that python whitelist.py runs without errors.