我在Python中使用软件包pydantic和链接器Flake8。我想用从脓毒症中得到的有规律的表达。只应传递某些字符。(a-z,A-Z,0-9和_)
常规的扩展"^[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_]*$") = ""你能帮我避免错误信息吗?
发布于 2020-11-19 18:06:42
发布于 2022-08-04 11:35:18
您可以将constr(..)语句提取到一个单独的变量:
KeyTypeStr = constr(regex="^[a-zA-Z0-9_]*$")
KeyOutputStr = constr(regex="^[a-zA-Z0-9_]*$")
class RedisSettings(BaseModel):
keyInput: KeyTypeStr = ""
keyOutput: KeyOutputStr = ""它看起来甚至更干净,类型注释可以很容易地重用,甚至在其他模块中也是如此。
https://stackoverflow.com/questions/64909849
复制相似问题