From 1da7da9fc16828486c3af90a18652c8a9f02f442 Mon Sep 17 00:00:00 2001 From: yanyongyu <42488585+yanyongyu@users.noreply.github.com> Date: Thu, 27 Jan 2022 11:15:44 +0800 Subject: [PATCH] :bug: fix dict(config) raise error --- nonebot/config.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/nonebot/config.py b/nonebot/config.py index d661a462..4fa4edf9 100644 --- a/nonebot/config.py +++ b/nonebot/config.py @@ -12,9 +12,18 @@ import os from pathlib import Path from datetime import timedelta from ipaddress import IPv4Address -from typing import Any, Set, Dict, Tuple, Union, Mapping, Optional +from typing import ( + TYPE_CHECKING, + Any, + Set, + Dict, + Tuple, + Union, + Mapping, + Optional, +) -from pydantic import BaseSettings, IPvAnyAddress, validator +from pydantic import BaseSettings, IPvAnyAddress from pydantic.env_settings import ( SettingsError, EnvSettingsSource, @@ -100,9 +109,10 @@ class CustomEnvSettings(EnvSettingsSource): class BaseConfig(BaseSettings): - # dummy getattr for pylance checking, actually not used - def __getattr__(self, name: str) -> Any: # pragma: no cover - return self.__dict__.get(name) + if TYPE_CHECKING: + # dummy getattr for pylance checking, actually not used + def __getattr__(self, name: str) -> Any: # pragma: no cover + return self.__dict__.get(name) class Config: @classmethod @@ -149,8 +159,8 @@ class Config(BaseConfig): 配置方法参考: [配置](https://v2.nonebot.dev/docs/tutorial/configuration) """ - _env_file: str - _common_config: Dict[str, Any] + _env_file: str = ".env" + _common_config: Dict[str, Any] = {} # nonebot configs driver: str = "~fastapi"