【问题标题】:Syntax Error with flake8 and Pydantic Constrained Types: constr(regex=)flake8 和 Pydantic 约束类型的语法错误:constr(regex=)
【发布时间】:2021-03-02 17:03:58
【问题描述】:

我在 Python 中使用包 pydantic 和链接器 Flake8。我想将 pydantic 的 constr 与常规的 Experssion 一起使用。只应传递某些字符。 (a-z、A-Z、0-9 和 _)

常规的 Experssion "^[a-zA-Z0-9_]*$" 有效,但 flake8 显示以下错误:

前向注释中的语法错误 '^[a-zA-Z0-9_]*$' flake8(F722)

class RedisSettings(BaseModel):
    keyInput: constr(regex="^[a-zA-Z0-9_]*$") = "" 
    keyOutput: constr(regex="^[a-zA-Z0-9_]*$") = ""

你能帮我避免错误信息吗?

【问题讨论】:

    标签: python flake8 pydantic


    【解决方案1】:

    这里的错误来自pyflakes,它试图根据PEP 484将类型注释解释为类型注释

    pydantic 使用的注释与 PEP 484 不兼容并导致该错误。你可以阅读更多关于这个in this pyflakes issue

    我建议 (1) 找到一种不违反 PEP 484 的使用 pydantic 的方法,或者 (2) 使用 flake8 的 extend-ignore / # noqa: ... / per-file-ignores 忽略 pyflakes 的错误


    免责声明:我是 pyflakes 维护者之一,我是当前 flake8 维护者

    【讨论】:

    • 好的,谢谢,现在我明白这个错误了。我已将 flake8 消息添加到忽略列表中。 (2)
    猜你喜欢
    • 2022-01-19
    • 2021-06-29
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2018-01-07
    • 1970-01-01
    • 2020-02-01
    相关资源
    最近更新 更多