jendrikseipp / vulture

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

Mark imports in __all__ as used #282

Closed kreathon closed 2 years ago

kreathon commented 2 years ago

At the moment, symbols that are referenced in string form in the __all__ variable assignment are not marked as used.

Example

# Currently, 'Bar' would be marked as unused.
from foo import Bar

__all__ = ["Bar"]

Implementation

Add visit_Assign to cover assignments of the following form:

# Default case
__all__ = ["A"]

# Tuples are also tolerated
__all__ = ("B", )

Limitations

The implementation cannot cover dynamic definitions e.g.:

a = ["A"]
__all__ = a

Related Issue

This PR is addressing the issue https://github.com/jendrikseipp/vulture/issues/172.

Checklist:

codecov-commenter commented 2 years ago

Codecov Report

Merging #282 (118eae2) into master (34e93b0) will increase coverage by 0.00%. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #282   +/-   ##
=======================================
  Coverage   99.37%   99.38%           
=======================================
  Files          19       19           
  Lines         638      648   +10     
=======================================
+ Hits          634      644   +10     
  Misses          4        4           
Impacted Files Coverage Δ
vulture/core.py 99.39% <100.00%> (+0.01%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 34e93b0...118eae2. Read the comment docs.

kreathon commented 2 years ago

Thank you for your feedback.

I am going to add a few more test cases (also to increase the branch coverage).

jendrikseipp commented 2 years ago

I agree about the readability, but let's go with that version anyway. Thanks for your great work!