Why does SecretStr in Settings class throws an error?

102 views Asked by At

I have standart .env file with telegram bot token, for example:

BOT_TOKEN = <bot_token>

Also, there is a config_reader file that should parse .env file to get token:

from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import SecretStr


class Settings(BaseSettings):
    bot_token: SecretStr

    model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')


config = Settings()

There is the main bot.py code:

async def main():
    bot = Bot(token=config.bot_token.get_secret_value(), parse_mode="HTML")
    dp = Dispatcher()

    logging.basicConfig(filename="bot_logging.log", encoding="utf-8", 
                        format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %H:%M:%S",
                        level=logging.INFO)
    
    dp.include_routers(messages.router, different_types.router)

    await bot.delete_webhook(drop_pending_updates=True)
    await dp.start_polling(bot)

When I start the main file, the following error occurres:

__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
bot_token
  Field required [type=missing, input_value={}, input_type=dict]
0

There are 0 answers