问题描述
说明
我已经看到有关自引用Pydantic模型的类似问题,这些问题导致RecursionError: maximum recursion depth exceeded in comparison
,但是据我所知,代码中没有包含自引用模型。我只是在使用Pydantic的BaseModel
类。
代码成功运行,直到下面audit.py
中的函数尝试从模型返回输出为止。
我已包含完整的追溯,因为我不确定从哪里开始此错误。我已经使用PyCharm在没有IDE的情况下运行了代码,它始终会在下面产生回溯,但不会使应用程序崩溃,但会在前端返回http状态代码500。
任何建议将不胜感激。
根据建议,我还尝试过sys.setrecursionlimit(1500)
来提高递归限制。
环境
- 操作系统:Windows 10
- FastAPI版本:0.61.1
- 金字塔版本:1.6.1
- Uvicorn版本:0.11.8
- Python版本:3.7.1
- Pycharm版本:2020.2
应用
main.py
import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from app.api.routes.router import api_router
from app.core.logging import init_logging
from app.core.config import settings
init_logging()
def get_app() -> FastAPI:
application = FastAPI(title=settings.APP_NAME,version=settings.APP_VERSION,debug=settings.DEBUG)
if settings.BACKEND_CORS_ORIGINS:
# middleware support for cors
application.add_middleware(
CORSMiddleware,allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)
application.include_router(api_router,prefix=settings.API_V1_STR)
return application
app = get_app()
if __name__ == "__main__":
uvicorn.run("main:app",host="127.0.0.1",port=80)
router.py
from fastapi import APIRouter
from app.api.routes import audit
api_router = APIRouter()
api_router.include_router(audit.router,tags=["audit"],prefix="/audit")
audit.py
import validators
from fastapi import APIRouter,HTTPException
from loguru import logger
from app.api.dependencies.audit import analyzer
from app.schemas.audit import AuditPayload,AuditResult
router = APIRouter()
@router.post("/",response_model=AuditResult,name="audit",status_code=200)
async def post_audit(payload: AuditPayload) -> AuditResult:
logger.info("Audit request received")
# validate URL
try:
logger.info("Validating URL")
validators.url(payload.url)
except HTTPException:
HTTPException(status_code=404,detail="Invalid URL.")
logger.exception("HTTPException - Invalid URL")
# generate output from route audit.py
logger.info("Running audit analysis. This Could take up to 10 minutes. Maybe grab a coffee...")
analyzed_output = analyzer.analyze(url=payload.url,brand=payload.brand,twitter_screen_name=payload.twitter_screen_name,facebook_page_name=payload.facebook_page_name,instagram_screen_name=payload.instagram_screen_name,youtube_user_name=payload.youtube_user_name,ignore_robots=payload.ignore_robots,ignore_sitemap=payload.ignore_sitemap,google_analytics_view_id=payload.google_analytics_view_id)
output = AuditResult(**analyzed_output)
return output
audit_models.py
from pydantic import BaseModel
class AuditPayload(BaseModel):
url: str
brand: str
twitter_screen_name: str
facebook_page_name: str
instagram_screen_name: str
youtube_user_name: str
ignore_robots: bool
ignore_sitemap: bool
google_analytics_view_id: str
class AuditResult(BaseModel):
base_url: str
run_time: float
website_404: dict
website_302: dict
website_h1_tags: dict
website_duplicate_h1: dict
website_h2_tags: dict
website_page_duplications: dict
website_page_similarities: dict
website_page_desc_duplications: dict
website_page_title_duplications: dict
pages: list
pages_out_links_404: dict = None
pages_canonicals: dict
SEO_phrases: dict
social: dict
google_analytics_report: dict
google_psi_desktop: dict
google_psi_mobile: dict
google_algo_updates: dict
google_sb: list
robots_txt: list
此行在日志中引发错误: 2020-09-10 10:02:31.483 |错误| uvicorn.protocols.http.h11_impl:run_asgi:391-Asgi应用程序中的异常
我相信这一点与理解为什么会发生此错误最相关:
File "pydantic\main.py",line 623,in pydantic.main.BaseModel._get_value
[PrevIoUs line repeated 722 more times]
完整追溯:
Traceback (most recent call last):
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\pydevconsole.py",line 483,in <module>
pydevconsole.start_client(host,port)
│ │ │ └ 50488
│ │ └ '127.0.0.1'
│ └ <function start_client at 0x000001BCEDC19D08>
└ <module 'pydevconsole' from 'C:\\Users\\<user>\\AppData\\Local\\JetBrains\\ToolBox\\apps\\PyCharm-P\\ch-0\\202.6948.78\\pl...
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\pydevconsole.py",line 411,in start_client
process_exec_queue(interpreter)
│ └ <_pydev_bundle.pydev_ipython_console.InterpreterInterface object at 0x000001BCEDC1BF98>
└ <function process_exec_queue at 0x000001BCEDC19A60>
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\pydevconsole.py",line 258,in process_exec_queue
more = interpreter.add_exec(code_fragment)
│ │ └ <_pydev_bundle.pydev_console_types.CodeFragment object at 0x000001BCEDCFE748>
│ └ <function BaseCodeExecutor.add_exec at 0x000001BCECF38488>
└ <_pydev_bundle.pydev_ipython_console.InterpreterInterface object at 0x000001BCEDC1BF98>
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\_pydev_bundle\pydev_code_executor.py",line 106,in add_exec
more = self.do_add_exec(code_fragment)
│ │ └ <_pydev_bundle.pydev_console_types.CodeFragment object at 0x000001BCEDCFE748>
│ └ <function InterpreterInterface.do_add_exec at 0x000001BCEDC15D90>
└ <_pydev_bundle.pydev_ipython_console.InterpreterInterface object at 0x000001BCEDC1BF98>
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\_pydev_bundle\pydev_ipython_console.py",line 36,in do_add_exec
res = bool(self.interpreter.add_exec(code_fragment.text))
│ │ │ │ └ "runfile('E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app/main.py',wdir='E:/Users/<user>/Docume...
│ │ │ └ <_pydev_bundle.pydev_console_types.CodeFragment object at 0x000001BCEDCFE748>
│ │ └ <function _PyDevFrontEnd.add_exec at 0x000001BCEDC15A60>
│ └ <_pydev_bundle.pydev_ipython_console_011._PyDevFrontEnd object at 0x000001BCEDC350B8>
└ <_pydev_bundle.pydev_ipython_console.InterpreterInterface object at 0x000001BCEDC1BF98>
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\_pydev_bundle\pydev_ipython_console_011.py",in add_exec
self.ipython.run_cell(line,store_history=True)
│ │ │ └ "runfile('E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app/main.py',wdir='E:/Users/<user>/Docume...
│ │ └ <function InteractiveShell.run_cell at 0x000001BCED5E7268>
│ └ <_pydev_bundle.pydev_ipython_console_011.PyDevTerminalInteractiveShell object at 0x000001BCEDC350F0>
└ <_pydev_bundle.pydev_ipython_console_011._PyDevFrontEnd object at 0x000001BCEDC350B8>
File "C:\Program Files\python37\lib\site-packages\IPython\core\interactiveshell.py",line 2843,in run_cell
raw_cell,store_history,silent,shell_futures)
│ │ │ └ True
│ │ └ False
│ └ True
└ "runfile('E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app/main.py',wdir='E:/Users/<user>/Docume...
File "C:\Program Files\python37\lib\site-packages\IPython\core\interactiveshell.py",line 2869,in _run_cell
return runner(coro)
│ └ <generator object InteractiveShell.run_cell_async at 0x000001BCEDC49C78>
└ <function _pseudo_sync_runner at 0x000001BCED5D0C80>
File "C:\Program Files\python37\lib\site-packages\IPython\core\async_helpers.py",line 67,in _pseudo_sync_runner
coro.send(None)
│ └ <method 'send' of 'generator' objects>
└ <generator object InteractiveShell.run_cell_async at 0x000001BCEDC49C78>
File "C:\Program Files\python37\lib\site-packages\IPython\core\interactiveshell.py",line 3044,in run_cell_async
interactivity=interactivity,compiler=compiler,result=result)
│ │ └ <ExecutionResult object at 1bcedcd3470,execution_count=2 error_before_exec=None error_in_exec=None info=<ExecutionInfo objec...
│ └ <IPython.core.compilerop.CachingCompiler object at 0x000001BCEDC356D8>
└ 'last_expr'
File "C:\Program Files\python37\lib\site-packages\IPython\core\interactiveshell.py",line 3215,in run_ast_nodes
if (yield from self.run_code(code,result)):
│ │ │ └ <ExecutionResult object at 1bcedcd3470,execution_count=2 error_before_exec=None error_in_exec=None info=<ExecutionInfo objec...
│ │ └ <code object <module> at 0x000001BCEDCDADB0,file "<ipython-input-2-086756a0f1dd>",line 1>
│ └ <function InteractiveShell.run_code at 0x000001BCED5E76A8>
└ <_pydev_bundle.pydev_ipython_console_011.PyDevTerminalInteractiveShell object at 0x000001BCEDC350F0>
File "C:\Program Files\python37\lib\site-packages\IPython\core\interactiveshell.py",line 3291,in run_code
exec(code_obj,self.user_global_ns,self.user_ns)
│ │ │ │ └ {'__name__': 'pydev_umd','__doc__': 'Automatically created module for IPython interactive environment','__package__': None,...
│ │ │ └ <_pydev_bundle.pydev_ipython_console_011.PyDevTerminalInteractiveShell object at 0x000001BCEDC350F0>
│ │ └ <property object at 0x000001BCED5D8958>
│ └ <_pydev_bundle.pydev_ipython_console_011.PyDevTerminalInteractiveShell object at 0x000001BCEDC350F0>
└ <code object <module> at 0x000001BCEDCDADB0,line 1>
File "<ipython-input-2-086756a0f1dd>",line 1,in <module>
runfile('E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app/main.py',wdir='E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app')
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py",line 197,in runfile
pydev_imports.execfile(filename,global_vars,local_vars) # execute the script
│ │ │ │ └ {'__name__': '__main__','__doc__': "\nMain entry point into API for endpoints related to HawkSense's main functionality.\nto...
│ │ │ └ {'__name__': '__main__','__doc__': "\nMain entry point into API for endpoints related to HawkSense's main functionality.\nto...
│ │ └ 'E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app/main.py'
│ └ <function execfile at 0x000001BCECC521E0>
└ <module '_pydev_bundle.pydev_imports' from 'C:\\Users\\<user>\\AppData\\Local\\JetBrains\\ToolBox\\apps\\PyCharm-P\\ch-0\\...
File "C:\Users\<user>\AppData\Local\JetBrains\ToolBox\apps\PyCharm-P\ch-0\202.6948.78\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py",line 18,in execfile
exec(compile(contents+"\n",file,'exec'),glob,loc)
│ │ │ └ {'__name__': '__main__','__doc__': "\nMain entry point into API for endpoints related to HawkSense's main functionality.\nto...
│ │ └ {'__name__': '__main__','__doc__': "\nMain entry point into API for endpoints related to HawkSense's main functionality.\nto...
│ └ 'E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app/main.py'
└ '#!/usr/bin/env python\n\n"""\nMain entry point into API for endpoints related to HawkSense\'s main functionality.\ntodo: htt...
File "E:/Users/<user>/Documents/GitHub/HawkSense/backend/app/app\main.py",line 47,in <module>
uvicorn.run("main:app",port=80) # for debug only
│ └ <function run at 0x000001BCEDE041E0>
└ <module 'uvicorn' from 'C:\\Program Files\\python37\\lib\\site-packages\\uvicorn\\__init__.py'>
File "C:\Program Files\python37\lib\site-packages\uvicorn\main.py",line 362,in run
server.run()
│ └ <function Server.run at 0x000001BCEDE4B510>
└ <uvicorn.main.Server object at 0x000001BCFC722198>
File "C:\Program Files\python37\lib\site-packages\uvicorn\main.py",line 390,in run
loop.run_until_complete(self.serve(sockets=sockets))
│ │ │ │ └ None
│ │ │ └ <function Server.serve at 0x000001BCEDE4B598>
│ │ └ <uvicorn.main.Server object at 0x000001BCFC722198>
│ └ <function BaseEventLoop.run_until_complete at 0x000001BCED49FE18>
└ <_WindowsSelectorEventLoop running=True closed=False debug=False>
File "C:\Program Files\python37\lib\asyncio\base_events.py",line 560,in run_until_complete
self.run_forever()
│ └ <function BaseEventLoop.run_forever at 0x000001BCED49FD90>
└ <_WindowsSelectorEventLoop running=True closed=False debug=False>
File "C:\Program Files\python37\lib\asyncio\base_events.py",line 528,in run_forever
self._run_once()
│ └ <function BaseEventLoop._run_once at 0x000001BCED4A27B8>
└ <_WindowsSelectorEventLoop running=True closed=False debug=False>
File "C:\Program Files\python37\lib\asyncio\base_events.py",line 1764,in _run_once
handle._run()
│ └ <function Handle._run at 0x000001BCED43AB70>
└ <Handle <TaskStepMethWrapper object at 0x000001BCFC7D4B00>()>
File "C:\Program Files\python37\lib\asyncio\events.py",line 88,in _run
self._context.run(self._callback,*self._args)
│ │ │ │ │ └ <member '_args' of 'Handle' objects>
│ │ │ │ └ <Handle <TaskStepMethWrapper object at 0x000001BCFC7D4B00>()>
│ │ │ └ <member '_callback' of 'Handle' objects>
│ │ └ <Handle <TaskStepMethWrapper object at 0x000001BCFC7D4B00>()>
│ └ <member '_context' of 'Handle' objects>
└ <Handle <TaskStepMethWrapper object at 0x000001BCFC7D4B00>()>
> File "C:\Program Files\python37\lib\site-packages\uvicorn\protocols\http\h11_impl.py",line 388,in run_asgi
result = await app(self.scope,self.receive,self.send)
│ │ │ │ │ │ └ <function RequestResponseCycle.send at 0x000001BCFC757840>
│ │ │ │ │ └ <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4A90>
│ │ │ │ └ <function RequestResponseCycle.receive at 0x000001BCFC7578C8>
│ │ │ └ <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4A90>
│ │ └ {'type': 'http','asgi': {'version': '3.0','spec_version': '2.1'},'http_version': '1.1','server': ('127.0.0.1',80),'clie...
│ └ <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4A90>
└ <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x000001BCFC722BA8>
File "C:\Program Files\python37\lib\site-packages\uvicorn\middleware\proxy_headers.py",line 45,in __call__
return await self.app(scope,receive,send)
│ │ │ │ └ <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4A90>>
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <fastapi.applications.FastAPI object at 0x000001BCFC722710>
└ <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x000001BCFC722BA8>
File "C:\Program Files\python37\lib\site-packages\fastapi\applications.py",line 149,in __call__
await super().__call__(scope,send)
│ │ └ <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4A90>>
│ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
└ {'type': 'http','clie...
File "C:\Program Files\python37\lib\site-packages\starlette\applications.py",line 102,in __call__
await self.middleware_stack(scope,send)
│ │ │ │ └ <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4A90>>
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <starlette.middleware.errors.ServerErrorMiddleware object at 0x000001BCFC7B8FD0>
└ <fastapi.applications.FastAPI object at 0x000001BCFC722710>
File "C:\Program Files\python37\lib\site-packages\starlette\middleware\errors.py",line 181,in __call__
raise exc from None
File "C:\Program Files\python37\lib\site-packages\starlette\middleware\errors.py",line 159,in __call__
await self.app(scope,_send)
│ │ │ │ └ <function ServerErrorMiddleware.__call__.<locals>._send at 0x000001BCFC72AE18>
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <starlette.middleware.cors.CORSMiddleware object at 0x000001BCFC7B8F60>
└ <starlette.middleware.errors.ServerErrorMiddleware object at 0x000001BCFC7B8FD0>
File "C:\Program Files\python37\lib\site-packages\starlette\middleware\cors.py",line 84,in __call__
await self.simple_response(scope,send,request_headers=headers)
│ │ │ │ │ └ Headers({'host': '127.0.0.1','connection': 'keep-alive','content-length': '295','accept': 'application/json','user-agent'...
│ │ │ │ └ <function ServerErrorMiddleware.__call__.<locals>._send at 0x000001BCFC72AE18>
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <function CORSMiddleware.simple_response at 0x000001BCEE53DC80>
└ <starlette.middleware.cors.CORSMiddleware object at 0x000001BCFC7B8F60>
File "C:\Program Files\python37\lib\site-packages\starlette\middleware\cors.py",line 140,in simple_response
await self.app(scope,send)
│ │ │ │ └ functools.partial(<bound method CORSMiddleware.send of <starlette.middleware.cors.CORSMiddleware object at 0x000001BCFC7B8F60...
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <starlette.exceptions.ExceptionMiddleware object at 0x000001BCFC7B8E48>
└ <starlette.middleware.cors.CORSMiddleware object at 0x000001BCFC7B8F60>
File "C:\Program Files\python37\lib\site-packages\starlette\exceptions.py",line 82,in __call__
raise exc from None
File "C:\Program Files\python37\lib\site-packages\starlette\exceptions.py",line 71,sender)
│ │ │ │ └ <function ExceptionMiddleware.__call__.<locals>.sender at 0x000001BCFC7C18C8>
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <fastapi.routing.APIRouter object at 0x000001BCFC7220F0>
└ <starlette.exceptions.ExceptionMiddleware object at 0x000001BCFC7B8E48>
File "C:\Program Files\python37\lib\site-packages\starlette\routing.py",line 550,in __call__
await route.handle(scope,send)
│ │ │ │ └ <function ExceptionMiddleware.__call__.<locals>.sender at 0x000001BCFC7C18C8>
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <function Route.handle at 0x000001BCEE4FF6A8>
└ <fastapi.routing.APIRoute object at 0x000001BCFC7B8E80>
File "C:\Program Files\python37\lib\site-packages\starlette\routing.py",line 227,in handle
await self.app(scope,send)
│ │ │ │ └ <function ExceptionMiddleware.__call__.<locals>.sender at 0x000001BCFC7C18C8>
│ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x000001BCFC7D4...
│ │ └ {'type': 'http','clie...
│ └ <function request_response.<locals>.app at 0x000001BCFC7C1A60>
└ <fastapi.routing.APIRoute object at 0x000001BCFC7B8E80>
File "C:\Program Files\python37\lib\site-packages\starlette\routing.py",line 41,in app
response = await func(request)
│ └ <starlette.requests.Request object at 0x000001BCFC7D4588>
└ <function get_request_handler.<locals>.app at 0x000001BCFC7C19D8>
File "C:\Program Files\python37\lib\site-packages\fastapi\routing.py",line 213,in app
is_coroutine=is_coroutine,└ True
File "C:\Program Files\python37\lib\site-packages\fastapi\routing.py",line 113,in serialize_response
exclude_none=exclude_none,└ False
File "C:\Program Files\python37\lib\site-packages\fastapi\routing.py",line 65,in _prepare_response_content
exclude_none=exclude_none,└ False
File "pydantic\main.py",line 386,in pydantic.main.BaseModel.dict
File "pydantic\main.py",line 706,in _iter
File "pydantic\main.py",in pydantic.main.BaseModel._get_value
File "pydantic\main.py",in pydantic.main.BaseModel._get_value
[PrevIoUs line repeated 722 more times]
File "pydantic\main.py",line 605,in pydantic.main.BaseModel._get_value
File "C:\Program Files\python37\lib\abc.py",line 139,in __instancecheck__
return _abc_instancecheck(cls,instance)
│ │ └ 8
│ └ <class 'pydantic.main.BaseModel'>
└ <built-in function _abc_instancecheck>
RecursionError: maximum recursion depth exceeded in comparison```
解决方法
这是一个简单的问题,可以通过修改输出响应以匹配pydantic模型来解决