Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: 无法从环境变量中读取列表型变量 #1575

Closed
zeithrold opened this issue Jan 9, 2023 · 12 comments
Closed

Bug: 无法从环境变量中读取列表型变量 #1575

zeithrold opened this issue Jan 9, 2023 · 12 comments
Labels
invalid This doesn't seem right

Comments

@zeithrold
Copy link

描述问题:

nonebot2的变量类型读取能力表格如下:

类型 dotenv 环境变量
其他简单类型变量
dict
list

如何复现?

tests/test_init.py作如下修改,运行测试出现错误:

diff --git a/tests/test_init.py b/tests/test_init.py
index 3d49d5ce..3fbb4965 100644
--- a/tests/test_init.py
+++ b/tests/test_init.py
@@ -3,6 +3,7 @@ import os
 import pytest
 
 os.environ["CONFIG_FROM_ENV"] = '{"test": "test"}'
+os.environ["CONFIG_FROM_ENV_LIST"] = '["test1", "test2"]'
 os.environ["CONFIG_OVERRIDE"] = "new"
 
 
@@ -26,6 +27,7 @@ async def test_init(nonebug_init):
 
     config = get_driver().config
     assert config.config_from_env == {"test": "test"}
+    assert config.config_from_env_list == ["test1", "test2"]
     assert config.config_override == "new"
     assert config.config_from_init == "init"
     assert config.common_config == "common"
# 测试命令参照.github/workflows/codecov.yml
$ poetry run pytest -n auto --cov-report xml

期望的结果

=============================================== short test summary info ================================================
FAILED test_init.py::test_init[nonebug_init0] - AttributeError: 'Config' object has no attribute 'config_from_env_list'
FAILED test_init.py::test_init[nonebug_init1] - AttributeError: 'Config' object has no attribute 'config_from_env_list'
============================================ 2 failed, 109 passed in 14.06s ============================================

环境信息:

该问题在以下环境中可以复现。

OS Python Nonebot
Darwin notebook.zeithrold.local 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:35 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T8101 arm64 3.9.12 gitmaster分支HEAD, afd13ed65d6b925bf4f1587a8dfd7fa3c09da152
Linux codespaces-d7be66 5.4.0-1098-azure #104~18.04.2-Ubuntu SMP Tue Nov 29 12:13:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux 3.10.4 gitmaster分支HEAD, afd13ed65d6b925bf4f1587a8dfd7fa3c09da152

协议端信息:

截图或日志

备注

这似乎是Pydantic的问题,该库文档提供了一种通过自定义解析的方式获取列表型环境变量:

import os
from typing import Any, List

from pydantic import BaseSettings


class Settings(BaseSettings):
    numbers: List[int]

    class Config:
        @classmethod
        def parse_env_var(cls, field_name: str, raw_val: str) -> Any:
            if field_name == 'numbers':
                return [int(x) for x in raw_val.split(',')]
            return cls.json_loads(raw_val)


os.environ['numbers'] = '1,2,3'
print(Settings().dict())
#> {'numbers': [1, 2, 3]}

从环境变量读取配置的原因在于,使用nonebot必然导致输入机密数据,这些数据不适宜存储在文件当中。部分Adapter要求使用列表型配置,但并不支持通过环境变量读取。

如果只能通过Pydantic读取变量,那么至少要在官方文档中醒目标注:目前无法支持通过环境变量传入列表型变量

@zeithrold zeithrold added the bug Something isn't working label Jan 9, 2023
@yanyongyu
Copy link
Member

yanyongyu commented Jan 9, 2023

你需要在.env文件里添加这个参数值,否则nb会认为这是无关环境变量,示例:

# .env
CONFIG_FROM_ENV_LIST=
os.environ["CONFIG_FROM_ENV_LIST"] = '["test"]'

@yanyongyu yanyongyu added invalid This doesn't seem right and removed bug Something isn't working labels Jan 9, 2023
@zeithrold
Copy link
Author

部分环境变量不适宜通过.env传递数据,例如onebot适配器的正向WS链接Nonebot2的SUPERUSERS
这些配置均定义为列表类型。

@yanyongyu
Copy link
Member

.env里留空就行了,让nb知道这个环境变量是要读取的,这样才会去环境变量里获取

@he0119
Copy link
Member

he0119 commented Jan 9, 2023

@yanyongyu
Copy link
Member

目前dotenv没有合适的list配置项解决方案,目前只有dict可以使用delimiter来进行配置,参考pydantic文档

@zeithrold
Copy link
Author

.env里留空就行了,让nb知道这个环境变量是要读取的,这样才会去环境变量里获取

明白了,目前官方文档有类似表述吗?

@he0119
Copy link
Member

he0119 commented Jan 9, 2023

确实该在 环境变量 那个地方加个提示。

@yanyongyu
Copy link
Member

image

@yanyongyu
Copy link
Member

虽然实际上任何存在的配置项都会优先从环境变量中获取

@zeithrold
Copy link
Author

image

至少对我来讲似乎存在一些歧义,不知道添加一行

https://github.com/he0119/CoolQBot/blob/840eee9239428bc54ee47928974da08332ba97a0/.env#L18

比如这样。

类似于这种的代码示例会不会好一些?

@yanyongyu
Copy link
Member

可以考虑在文档中明确一下

@zeithrold
Copy link
Author

好的,明白,谢谢了,那就关闭issue了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Development

No branches or pull requests

3 participants