是否可以创建受正则表达式约束的类型提示? [英] Is it possible to create a regex-constrained type hint?

查看:46
本文介绍了是否可以创建受正则表达式约束的类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个辅助函数,可以将 %Y-%m-%d %H:%M:%S 格式的字符串转换为 datetime.datetime:

I have a helper function that converts a %Y-%m-%d %H:%M:%S-formatted string to a datetime.datetime:

def ymdt_to_datetime(ymdt: str) -> datetime.datetime:
    return datetime.datetime.strptime(ymdt, '%Y-%m-%d %H:%M:%S')

我可以在函数本身中验证 ymdt 格式,但是将自定义对象用作参数的类型提示会更有用,例如

I can validate the ymdt format in the function itself, but it'd be more useful to have a custom object to use as a type hint for the argument, something like

from typing import NewType, Pattern

ymdt_pattern = '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
YmdString = NewType('YmdString', Pattern[ymdt_pattern])

def ymdt_to_datetime(ymdt: YmdString)...

我是不是走错了兔子洞?这应该是 mypy 或其他地方的问题吗?或者这可以通过当前的类型提示实现 (3.61) 来完成吗?

Am I going down the wrong rabbit hole? Should this be an issue in mypy or someplace? Or can this be accomplished with the current type hint implementation (3.61)?

推荐答案

不幸的是,目前没有办法静态验证您的字符串是否与精确格式匹配.这部分是因为在编译时检查给定变量可以保存的确切值非常难以实现(实际上,在某些情况下是 NP-hard),部分是因为面对用户输入等问题变得不可能.因此,在不久的将来,这个功能不太可能添加到 mypy 或 Python 类型生态系统中.

There currently is no way for types to statically verify that your string matches a precise format, unfortunately. This is partially because checking at compile time the exact values a given variable can hold is exceedingly difficult to implement (and in fact, is NP-hard in some cases), and partially because the problem becomes impossible in the face of things like user input. As a result, it's unlikely that this feature will be added to either mypy or the Python typing ecosystem in the near future, if at all.

一种潜在的解决方法是利用 NewType,并仔细控制何时构造该格式的字符串.也就是说,你可以这样做:

One potential workaround would be to leverage NewType, and carefully control when exactly you construct a string of that format. That is, you could do:

from typing import NewType
YmdString = NewType('YmdString', str)

def datetime_to_ymd(d: datetime) -> YmdString:
    # Do conversion here
    return YmdStr(s)

def verify_is_ymd(s: str) -> YmdString:
    # Runtime validation checks here
    return YmdString(s)

如果您只使用这些函数来引入 YmdString 类型的值并进行测试以确认您的构造函数"运行良好,您或多或少可以安全地区分字符串和 YmdString 在编译时.然后,您希望设计程序以尽量减少调用这些函数的频率,以避免产生不必要的开销,但希望这样做不会太麻烦.

If you use only functions like these to introduce values of type YmdString and do testing to confirm that your 'constructor functions' are working perfectly, you can more or less safely distinguish between strings and YmdString at compile time. You'd then want to design your program to minimize how frequently you call these functions to avoid incurring unnecessary overhead, but hopefully, that won't be too onerous to do.

这篇关于是否可以创建受正则表达式约束的类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆