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

Fix: 声明为 str 的配置项在 dotenv 中写为纯数字时在 Pydantic 2 下的解析错误 #2800

Closed
wants to merge 1 commit into from

Conversation

lgc2333
Copy link
Contributor

@lgc2333 lgc2333 commented Jul 6, 2024

写的有点暴力

from pydantic import BaseModel, TypeAdapter


class Model(BaseModel):
    a: int
    b: str


print(TypeAdapter(Model).validate_python({"a": "1", "b": "2"}))
print(TypeAdapter(Model).validate_python({"a": 1, "b": 2}))
a=1 b='2'
Traceback (most recent call last):
  File "d:\Coding\python\nb2-workspace\private\test-nb2\debug.py", line 10, in <module>
    print(TypeAdapter(Model).validate_python({"a": 1, "b": 2}))
  File "D:\Coding\python\nb2-workspace\.venv\lib\site-packages\pydantic\type_adapter.py", line 142, in wrapped
    return func(self, *args, **kwargs)
  File "D:\Coding\python\nb2-workspace\.venv\lib\site-packages\pydantic\type_adapter.py", line 373, in validate_python
    return self.validator.validate_python(object, strict=strict, from_attributes=from_attributes, context=context)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Model
b
  Input should be a valid string [type=string_type, input_value=2, input_type=int]
    For further information visit https://errors.pydantic.dev/2.8/v/string_type

要是 nb 不改就去和 pydantic 爆了吧

@yanyongyu
Copy link
Member

这纯粹是你的dotenv写法不对

Copy link
Contributor

github-actions bot commented Jul 6, 2024

🚀 Deployed on https://deploy-preview-2800--nonebot2.netlify.app

@github-actions github-actions bot temporarily deployed to pull request July 6, 2024 12:53 Inactive
@lgc2333
Copy link
Contributor Author

lgc2333 commented Jul 6, 2024

对不起 打扰了
我是傻逼

@lgc2333 lgc2333 closed this Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants