guilatrova / tryceratops

A linter to prevent exception handling antipatterns in Python (limited only for those who like dinosaurs).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tryceratops sometimes fails to create `.tryceratops-errors.log`, raising `FileNotFoundError` when checking

paw-lu opened this issue · comments

Thanks for sharing this work. Interesting idea for a linter!


When ran from a pre-commit hook as a local hook:

repos:
  - repo: local
    hooks:
      - id: tryceratops
        name: tryceratops
        entry: tryceratops
        language: system
        types: [python]

and tryceratops-errors.log has not been created yet, this is raised at each check:

Traceback (most recent call last):
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/bin/tryceratops", line 8, in <module>
    sys.exit(main())
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/tryceratops/__main__.py", line 62, in main
    entrypoint(prog_name="tryceratops")
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/tryceratops/__main__.py", line 57, in entrypoint
    interface.present_and_exit()
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/tryceratops/interfaces.py", line 95, in present_and_exit
    self._delete_empty_logs()
  File "/Users/pawlu/Documents/personal/nbpreview/.nox/pre-commit/lib/python3.9/site-packages/tryceratops/interfaces.py", line 86, in _delete_empty_logs
    is_log_empty = os.path.getsize(log_file_path) == 0
  File "/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/genericpath.py", line 50, in getsize
    return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: '/Users/pawlu/Documents/personal/nbpreview/.tryceratops-errors.log'

Things work fine if the command is invoked directly through the terminal.

Hi @paw-lu 👋

Thanks for trying Tryceratops :)
I'll investigate it as soon as possible. Let me know if you find any other edge cases that may help debugging this issue.

I guess I found the issue, try to add require_serial: true, so:

  - repo: local
    hooks:
      - id: tryceratops
        name: tryceratops
        entry: tryceratops
        language: system
        require_serial: true
        types: [python]

It worked for me :)

Full context
Based on pre commit docs: https://pre-commit.com/#new-hooks

(optional: default false) if true this hook will execute using a single process instead of in parallel. new in 1.13.0.

So your pre-commit was launching several processes in parallel, and tryceratops was deleting the log file in the middle of process. By sticking to just one process, the deletion only happens at the end, so it's safe.

Yeah this fixed all issue reported.

Thanks for the fix, and thanks again for sharing your work here!