Kludex / flake8-fastapi

Flake8 FastAPI - Avoid simple FastAPI mistakes ~opinionated~ 🤓
MIT License
45 stars 4 forks source link

AttributeError: 'NoneType' object has no attribute 'body' #28

Open ikreb7 opened 2 years ago

ikreb7 commented 2 years ago

Hello Marcelo,

sadly, I get this error message:

multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 48, in mapstar
    return list(map(*args))
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8/checker.py", line 687, in _run_checks
    return checker.run_checks()
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8/checker.py", line 597, in run_checks
    self.run_ast_checks()
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8/checker.py", line 500, in run_ast_checks
    for (line_number, offset, text, _) in runner:
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8_plugin_utils/plugin.py", line 71, in run
    visitor.visit(self._tree)
  File "/usr/lib/python3.8/ast.py", line 371, in visit
    return visitor(node)
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8_fastapi/visitors/undocumented_exception.py", line 28, in generic_visit
    self._visit_func(child)
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8_fastapi/visitors/undocumented_exception.py", line 44, in _visit_func
    excepted_status = self.find_exceptions_in_body(node)
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8_fastapi/visitors/undocumented_exception.py", line 74, in find_exceptions_in_body
    _excepted_status = self.find_exceptions_in_body(body)
  File "/home/ikreb/netz/project/backend/venv/lib/python3.8/site-packages/flake8_fastapi/visitors/undocumented_exception.py", line 52, in find_exceptions_in_body
    for element in node.body:
AttributeError: 'NoneType' object has no attribute 'body'
"""

This is my system:

flake8 --version
4.0.1 (flake8-fastapi: 0.7.0, mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0) CPython 3.8.10 on Linux

If you need any further information, let me know.

stefan6419846 commented 2 years ago

You should probably try to pinpoint this error to a specific file/expression, for example by digging into the verbose output of flake8 or by running the checker on each file itself. This usually makes it much easier to fix the actual issue.

MarcinBinkowski commented 2 years ago

Hi, I've researched this issue. It seems that this happens when this function

def find_function(self, func_name: str) -> ASTCallable:  # type: ignore[return]
        for child in ast.walk(self.parent):
            if (
                isinstance(child, (ast.AsyncFunctionDef, ast.FunctionDef))
                and child.name == func_name
            ):
                return child

is not able to find the proper return value and returns None instead. This happens in a few scenarios.

  1. Using builtins like print()
  2. Importing functions from other modules. For example, importing "from math import floor" and then using it in the tested file. 
  3. When using not existing methods of the class. For example:
    
    from fastapi import FastAPI, HTTPException

app = FastAPI()

class MyClass:

    ...

@app.get("/") def home():     my_object = MyClass()     my_object.raise_here()


And probably in some other cases.

"Quickfix" for this would be to annotate the return value of `find_function` as  `Optional[ASTCallable]` and add a check if the body is not None before running `self.find_exceptions_in_body` on it. 
Unfortunately, this plugin would lose the ability to detect `UndocumentedHTTPException` on functions/methods that are defined outside of the tested module.
TeaDove commented 1 year ago

Any progress? I am facing the same problem:(