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

Cannot unpack non-iterable NoneType object

niderhoff opened this issue · comments

Hello,

I was trying out your library and I get an exception when running it on a folder:

(.venv) niid@xxx:~/projects/xxx/python_repository$ tryceratops .
Traceback (most recent call last):
  File "/home/niid/projects/xxx/python_repository/.venv/bin/tryceratops", line 8, in <module>
    sys.exit(main())
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/tryceratops/__main__.py", line 52, in main
    entrypoint()
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/click/core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/click/core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/click/core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/tryceratops/__main__.py", line 44, in entrypoint
    parsed_files = list(parse_python_files(dir))
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/tryceratops/files/discovery.py", line 42, in parse_python_files
    yield from parse_python_files_from_dir(file)
  File "/home/niid/projects/xxx/python_repository/.venv/lib/python3.8/site-packages/tryceratops/files/discovery.py", line 35, in parse_python_files_from_dir
    parsed, filefilter = parse_file(filename)
TypeError: cannot unpack non-iterable NoneType object

Any ideas what could be causing this?

Not sure. I'll investigate it over the weekend, thanks for reporting!
If I can't come up with anything, I'll at least improve the logging.

By looking in depth I assume there's some file of yours with invalid python/syntax code.

This is the function that caused the issue:

def parse_tree(content: TextIOWrapper) -> Optional[ast.AST]:
    try:
        return ast.parse(content.read())
    except Exception:
        return None

As you can see, ast.parse failed and returned None.
Anyway, I would expect a proper message, I'm on it!

Again, thank you very much for using Tryceratops :)
I fixed the issue and soon I'll release a new version with this fix.