jendrikseipp / vulture

Find dead Python code
MIT License
3.29k stars 145 forks source link

Bugfix: Handle when .gitignore is not present. #346

Closed TravisDart closed 6 months ago

TravisDart commented 6 months ago

I cloned the Vulture repo and created the dead_code.py file in the tests/ directory. I ran Vulture with: python ../vulture/__main__.py dead_code.py (maybe there's a more elegant way; I dunno), and I got this error:

$  python ../vulture/__main__.py dead_code.py
Traceback (most recent call last):
  File "/Users/travis.dart/PycharmProjects/vulture/tests/../vulture/__main__.py", line 3, in <module>
    main()
  File "/Users/travis.dart/PycharmProjects/vulture/vulture/core.py", line 751, in main
    vulture.scavenge(config["paths"], exclude=config["exclude"])
  File "/Users/travis.dart/PycharmProjects/vulture/vulture/core.py", line 274, in scavenge
    gitignore = _get_gitignore_pathspec()
                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/travis.dart/PycharmProjects/vulture/vulture/core.py", line 120, in _get_gitignore_pathspec
    with gitignore.open() as fh:
         ^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1044, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/Users/travis.dart/PycharmProjects/vulture/tests/.gitignore'

I found that there's an error in the function that detects the .gitignore file. Basically, we need is_file() instead of is_file.

(Pdb) Path(".gitignore").resolve().is_file
<bound method Path.is_file of PosixPath('/Users/travis.dart/PycharmProjects/vulture/tests/.gitignore')>
(Pdb) bool(Path(".gitignore").resolve().is_file)
True
(Pdb) Path(".gitignore").resolve().is_file()
False

Checklist:

jendrikseipp commented 6 months ago

I merged this to get the fix in quickly. Feel free to make a new PR with a test for this case.