jendrikseipp / vulture

Find dead Python code
MIT License
3.38k stars 148 forks source link

Hint: if 0: -> if _ := False: (type checking imports) #267

Closed dsuch closed 2 years ago

dsuch commented 2 years ago

Hello,

this is just a quick hint that I am leaving for people who would like to use vulture with code that uses imports guarded by "if 0:", "if False:" or similar checks and who would not like to resort to the "debug = False / if debug:" or similar approaches because they use "if 0:" with a specific goal in mind, which is solely to let type checkers know what types there exist, which means that such imports are never "dead" code.

The hint is to use this:

if _ := False:
    from mymodule import Type

It is almost as concise as "if 0:" and vulture does not reject it with a message that it is an "unsatisfiable 'if' condition (100% confidence)."

Same goes for mypy and pyright, both accept it.

jendrikseipp commented 2 years ago

Nice tip! Do you want to make a pull request that adds this to the README under "Handling false positives"?

The-Compiler commented 2 years ago

That seems like an antipattern to me, given that there is a generally accepted way of fixing the same problem, namely using if typing.TYPE_CHECKING: (docs)