感谢您阅读我的代码!
事实上,在 Python 中创建一个通用的注解实施器并不难。这是我的看法:
'''Very simple enforcer of type annotations.
This toy super-decorator can decorate all functions in a given module that have
annotations so that the type of input and output is enforced; an AssertionError is
raised on mismatch.
This module also has a test function func() which should fail and logging facility
log which defaults to print.
Since this is a test module, I cut corners by only checking *keyword* arguments.
'''
import sys
log = print
def func(x:'int' = 0) -> 'str':
'''An example function that fails type checking.'''
return x
# For simplicity, I only do keyword args.
def check_type(*args):
param, value, assert_type = args
log('Checking {0} = {1} of {2}.'.format(*args))
if not isinstance(value, assert_type):
raise AssertionError(
'Check failed - parameter {0} = {1} not {2}.'
.format(*args))
return value
def decorate_func(func):
def newf(*args, **kwargs):
for k, v in kwargs.items():
check_type(k, v, ann[k])
return check_type('<return_value>', func(*args, **kwargs), ann['return'])
ann = {k: eval(v) for k, v in func.__annotations__.items()}
newf.__doc__ = func.__doc__
newf.__type_checked = True
return newf
def decorate_module(module = '__main__'):
'''Enforces type from annotation for all functions in module.'''
d = sys.modules[module].__dict__
for k, f in d.items():
if getattr(f, '__annotations__', {}) and not getattr(f, '__type_checked', False):
log('Decorated {0!r}.'.format(f.__name__))
d[k] = decorate_func(f)
if __name__ == '__main__':
decorate_module()
# This will raise AssertionError.
func(x = 5)
鉴于这种简单性,乍一看这东西不是主流很奇怪。但是,我相信它没有看起来那么有用是有充分理由的。通常,类型检查会有所帮助,因为如果您添加整数和字典,您很可能会犯一些明显的错误(如果您的意思是合理的,那么显式比隐式更好)。
但是在现实生活中,您经常混合使用编译器看到的相同计算机类型但明显不同的人类类型的数量,例如以下代码段包含一个明显的错误:
height = 1.75 # Bob's height in meters.
length = len(sys.modules) # Number of modules imported by program.
area = height * length # What's that supposed to mean???
只要知道变量的“人类类型”height
并且length
即使在计算机看来它是和的完全合法的乘法,int
任何人都应该立即在上面的行中看到一个错误float
。
关于这个问题的可能解决方案还有很多可以说的,但是强制执行“计算机类型”显然是一个半解决方案,所以,至少在我看来,这比没有解决方案更糟糕。这也是为什么Systems Hungarian是一个糟糕的想法而Apps Hungarian是一个很棒的想法的原因。在Joel Spolsky的信息非常丰富的帖子中还有更多内容。
现在,如果有人要实现某种 Pythonic 第三方库,该库会自动将其人类类型分配给现实世界的数据,然后小心地转换该类型width * height -> area
并使用函数注释强制检查,我认为这将是一种类型检查人真的可以用!