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

标签 python flake8 pydantic

我在 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/

相关文章:

python - 何时使用 tkinter 菜单按钮

python - 使用 Python,如何从 Google 文档中读取纯文本?

cython - 在 cython 代码上运行 pep8 或 pylint

python - vscode 无法捕获 flake8 输出

python - 忽略 SublimeLinter3(flake8 插件)中的特定警告

python - 使用 Pydantic 模型的 FastAPI 查询参数

python - Pydantic 在 FastAPI 中模拟良好实践

python - 是否可以通过 pydantic 中的单独父类来组织验证

python - Scipy map_coordinates 双线性插值与 interp 和 IDL 插值相比

python - 声音库 python