1

嗨,我遇到了这段代码的问题我有一个使用配置布局的设置类



class Settings(BaseSettings):
    appName: str = 'Mini App'
    version: str = '1.0.1'
    description: str = 'Mini Application Mini Shop writing with react/react native'
    debug: bool = True
    prefixAPI: str = 'api'
    host: str = '0.0.0.0'
    port: int = 5700
    secret: str = ''
    workers: int = 1
    database_type: str = 'postgresql'
    database_host: str = '127.0.0.1'
    database_port: int = 5432
    database_name: str = 'miniapp'
    database_user: str = 'postgres'
    database_password: str = 'postgres'
    logs_enable: bool = True
    logs_level: str = 'debug'
    logs_enable_sentry: bool
    logs_sentry_dns: str
    cords_enable: bool = True
    cords_domains: []
    cords_methods: ['GET', 'PUT', 'POST', 'DELETE']
    cords_headers: ["*"]
    meta_pageSizeDef: int = 50
    meta_pageSizes = [10, 20, 30, 50, 100, 200]

    class Config:
        env_file: str = '.env'


settings = Settings()

现在运行这个名为 server.py 的代码说它位于第 5 行,代码看起来像这样

import uvicorn
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pprint import pprint
from common.bootstrap import getApplication, loadCordsMiddleware, bindLogs, registerLogger, writeLog, settings
from middlewares.configMiddleware import registerConfigMiddleware
from middlewares.databaseMiddleware import registerDatabaseMiddleware
from middlewares.loggerMiddeware import registerLoggerMiddleware
from routes.routes import router
print("Starting Server")
application = getApplication()
pprint(f"Server Start With Config({vars(settings)})")
application = loadCordsMiddleware()
....
.....
......

它自己的错误


(miniApp) D:\Projects\miniApp>python server.py
Traceback (most recent call last):
  File "pydantic\validators.py", line 579, in pydantic.validators.find_validators
TypeError: issubclass() arg 1 must be a class

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "server.py", line 5, in <module>
    from common.bootstrap import getApplication, loadCordsMiddleware, bindLogs, registerLogger, writeLog, settings
  File "D:\Projects\miniApp\common\bootstrap.py", line 5, in <module>
    from config.settings import settings
  File "D:\Projects\miniApp\config\settings.py", line 4, in <module>
    class Settings(BaseSettings):
  File "pydantic\main.py", line 252, in pydantic.main.ModelMetaclass.__new__
  File "pydantic\fields.py", line 309, in pydantic.fields.ModelField.infer
  File "pydantic\fields.py", line 271, in pydantic.fields.ModelField.__init__
  File "pydantic\fields.py", line 351, in pydantic.fields.ModelField.prepare
  File "pydantic\fields.py", line 529, in pydantic.fields.ModelField.populate_validators
  File "pydantic\validators.py", line 588, in find_validators
RuntimeError: error checking inheritance of [] (type: list)

现在我试着理解我在这里做错了 什么

4

1 回答 1

1

:如果您使用符号,您的变量需要定义类型。就像您str在 line 上正确指定一样appName: str = 'Mini App',您需要列表的类型。

from typing import List
# ...

class Settings(BaseSettings):
    # ...
    # using str (or int, etc.) within List is optional.
    # you can also just use list instead of typing.List
    #
    cords_domains: List = []
    cords_methods: List[str] = ['GET', 'PUT', 'POST', 'DELETE']
    cords_headers: List[str] = ["*"]
    meta_pageSizeDef: int = 50
    meta_pageSizes = [10, 20, 30, 50, 100, 200]

您也可以不使用类型,就像在最后一行所做的那样:meta_pageSizes = [10, 20, 30, 50, 100, 200]

有关更多信息,请参阅 python类型pydantic 设置管理

于 2020-07-23T21:01:01.960 回答