gpt4 book ai didi

python - flake8 和 Pydantic 约束类型 : constr(regex=) 的语法错误

转载 作者:行者123 更新时间:2023-12-03 15:47:57 48 4
gpt4 key购买 nike

我在 Python 中使用包 pydantic 和链接器 Flake8。我想将来自 pydantic 的 constr 与常规 Experssion 一起使用。只应传递某些字符。 (a-z、A-Z、0-9 和 _)
正则表达式 "^[a-zA-Z0-9_]*$"有效,但 flake8 向我显示以下错误:

syntax error in forward annotation '^[a-zA-Z0-9_]*$' flake8(F722)

class RedisSettings(BaseModel):
keyInput: constr(regex="^[a-zA-Z0-9_]*$") = ""
keyOutput: constr(regex="^[a-zA-Z0-9_]*$") = ""
你能帮我避免错误信息吗?

最佳答案

这里的错误来自 pyflakes,它试图根据 PEP 484 将类型注释解释为类型注释。
pydantic 使用的注释与 PEP 484 不兼容并导致该错误。您可以阅读有关此内容的更多信息 in this pyflakes issue
我建议要么(1)找到一种不涉及违反 PEP 484 的使用 pydantic 的方法,要么(2)使用 flake8 的 extend-ignore 忽略来自 pyflakes 的错误/# noqa: .../per-file-ignores
免责声明:我是 pyflakes 维护者之一,我是当前的 flake8 维护者

关于python - flake8 和 Pydantic 约束类型 : constr(regex=) 的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64909849/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com