Sivan Wolberg
Sivan Wolberg

Reputation: 458

issue run my pyhon script that use pydantic package

hi i got issue with this code i got a settings class that use a config layout



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()

now the code that run this called server.py is say it is fall on line 5 that code look like this

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()
....
.....
......

the error it self


(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)

now i try understand what i did here wrong i using fastapi framework but very new to python and such so i learning as i go btw the code is here thanks for the help

Upvotes: 2

Views: 13334

Answers (1)

csum
csum

Reputation: 1992

Your vars need to have types defined if you are using : notation. Much like you are correctly specifying str on the line appName: str = 'Mini App', you need the types for your lists.

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]

You can also leave off the type, as you are doing on the last line: meta_pageSizes = [10, 20, 30, 50, 100, 200]

See python typing and pydantic settings management for more info.

Upvotes: 4

Related Questions