魔术类型内省和运行时参数类型/值检查。
magic_constraints的Python项目详细描述
#简介
*magic constraints*实现(或破解)了一堆[抽象基类][1(简称abcs)以支持[类型内省][2],即python中的'isinstance'/'issubclass'操作。abc的专门化是支持的,即'sequence[int]`和'sequence[int]`是'sequence'的专门化版本。
此外,*magic constraints*提供了几个修饰符,用于对用户定义的函数和方法的参数和返回值进行运行时类型/值检查。尤其是,这些装饰符与python 3.x中引入的类型注释特性非常吻合:来自magic_constraints import function撸constraints的
``python
,可选
foobar应接受int对象或none对象。
@函数约束
def函数(foobar:optional[int])->;float:
如果foobar为none,则返回类型检查失败。
return 42
else:
好的情况。
return42.0
return42.0
return42.0
else:
好的情况。
return42.0
`` ` ` `
` ` ` ` ` `
>;函数(1)
>42.0
;失败。
1.0为float;
1.0为float;
1.0,则返回浮点值为float;float我是说,而foobar则要求int或type(无)。
>;>;函数(1.0)
回溯(最后一次调用的最近一次):
…
幻方约束。异常。幻方错误:
幻方错误:参数不匹配。
type_u=optional[int])
-----------------------------
失败。
当foobar为none时,函数返回一个float,
导致不匹配的返回类型错误。
>;>;函数(无)
回溯(最近一次调用时间):
…
magic_constraints.exception.magicTypeerror:
magicTypeerror:返回值不匹配。
---------------------------
ret:42
返回类型:returnType(type_=float)
-----------------------------
```
```快速启动
````install
``````
$pip install magic constraints
````
`` magic constraints*提供了'sequence/mutablesequence/immutablesequence'。您可以在以下位置调用"isinstance`/`issubClass`操作:
``python
不可变序列)
3.0是浮点数。
IsInstance([(1,2),(3.0,4)],Sequence[不可变序列[int]])
False,[3,4]是可变序列。
IsInstance([(1,2),[3,4]],Sequence[不可变序列[int]])
True
IssubClass(不可变序列,序列)
True
IssubClass(不可变序列,序列)
false
issubclass(mutablesequence,mutablesequence)
false
issubclass(mutablesequence,mutablesequence)
````
]|
可变序列[类型],[类型,…]|
不可变序列[类型],[类型,…]|
集[类型]
可变集[类型]
不变集[类型]
映射[类型,类型]
可变映射[类型,类型]
不变映射[类型,类型]
迭代器[类型],[类型,….]|
iterable[类型],[类型,…]|
可调用[[类型,…]],类型,,[省略号,类型]
任何*不支持
联合[类型,…]|
可选[类型]
非类型|*不支持*
bar:sequence[int])——>;映射[str,sequence[int]]:
返回{foo:bar}
`````
more decorators:
``python
``from magic\u constraints.decorator导入(
函数约束,
方法约束,
类初始化约束,
)
````
只会引发"syntaxerror"和"typeerror"的派生类:
1。任何与类型相关的操作,例如未能传递"isinstance",都会引发派生类型为"typeerror"的异常。
2。除此之外(1),任何与接口(函数)调用承诺相关的内容,都会引发派生类型"syntaxerror"的异常。
示例:
````
>;gt;func1('42不是序列',42)
回溯(最后一次调用):
…
magic_constraints.exception.magicTypeerror:
magicTypeerror:参数不匹配。
----------
参数:42
参数(name='bar',类型"u=sequence[int]"
----------
失败,bar需要一个int序列。
>;>>;func1('2.0不是int',[1,2.0,3])
回溯(最近一次调用时间):
…
magic_constraints.exception.magicTypeerror:
magicTypeerror:参数不匹配。
--------
参数:[1,2.0,3]
参数:参数(名称='bar',type_=sequence[int])
-----------------------------
```
更多…
*[magic_constraints.types][3]。
*[magic_constraints.decorator][4]。
[1]:https://docs.python.org/3/glossary.html术语抽象基类
[2]:https://en.wikipedia.org/wiki/type_内省
[3]:https://github.com/huntzhan/magic constraints/wiki/magic_constraints.types
[4]:https://github.com/huntzhan/magic-constraints/wiki/magic_constraints.decorator
*magic constraints*实现(或破解)了一堆[抽象基类][1(简称abcs)以支持[类型内省][2],即python中的'isinstance'/'issubclass'操作。abc的专门化是支持的,即'sequence[int]`和'sequence[int]`是'sequence'的专门化版本。
此外,*magic constraints*提供了几个修饰符,用于对用户定义的函数和方法的参数和返回值进行运行时类型/值检查。尤其是,这些装饰符与python 3.x中引入的类型注释特性非常吻合:来自magic_constraints import function撸constraints的
``python
,可选
foobar应接受int对象或none对象。
@函数约束
def函数(foobar:optional[int])->;float:
如果foobar为none,则返回类型检查失败。
return 42
else:
好的情况。
return42.0
return42.0
return42.0
else:
好的情况。
return42.0
`` ` ` `
` ` ` ` ` `
>;函数(1)
>42.0
;失败。
1.0为float;
1.0为float;
1.0,则返回浮点值为float;float我是说,而foobar则要求int或type(无)。
>;>;函数(1.0)
回溯(最后一次调用的最近一次):
…
幻方约束。异常。幻方错误:
幻方错误:参数不匹配。
type_u=optional[int])
-----------------------------
失败。
当foobar为none时,函数返回一个float,
导致不匹配的返回类型错误。
>;>;函数(无)
回溯(最近一次调用时间):
…
magic_constraints.exception.magicTypeerror:
magicTypeerror:返回值不匹配。
---------------------------
ret:42
返回类型:returnType(type_=float)
-----------------------------
```
```快速启动
````install
``````
$pip install magic constraints
````
`` magic constraints*提供了'sequence/mutablesequence/immutablesequence'。您可以在以下位置调用"isinstance`/`issubClass`操作:
``python
不可变序列)
3.0是浮点数。
IsInstance([(1,2),(3.0,4)],Sequence[不可变序列[int]])
False,[3,4]是可变序列。
IsInstance([(1,2),[3,4]],Sequence[不可变序列[int]])
True
IssubClass(不可变序列,序列)
True
IssubClass(不可变序列,序列)
false
issubclass(mutablesequence,mutablesequence)
false
issubclass(mutablesequence,mutablesequence)
````
]|
可变序列[类型],[类型,…]|
不可变序列[类型],[类型,…]|
集[类型]
可变集[类型]
不变集[类型]
映射[类型,类型]
可变映射[类型,类型]
不变映射[类型,类型]
迭代器[类型],[类型,….]|
iterable[类型],[类型,…]|
可调用[[类型,…]],类型,,[省略号,类型]
任何*不支持
联合[类型,…]|
可选[类型]
非类型|*不支持*
bar:sequence[int])——>;映射[str,sequence[int]]:
返回{foo:bar}
`````
more decorators:
``python
``from magic\u constraints.decorator导入(
函数约束,
方法约束,
类初始化约束,
)
````
只会引发"syntaxerror"和"typeerror"的派生类:
1。任何与类型相关的操作,例如未能传递"isinstance",都会引发派生类型为"typeerror"的异常。
2。除此之外(1),任何与接口(函数)调用承诺相关的内容,都会引发派生类型"syntaxerror"的异常。
示例:
````
>;gt;func1('42不是序列',42)
回溯(最后一次调用):
…
magic_constraints.exception.magicTypeerror:
magicTypeerror:参数不匹配。
----------
参数:42
参数(name='bar',类型"u=sequence[int]"
----------
失败,bar需要一个int序列。
>;>>;func1('2.0不是int',[1,2.0,3])
回溯(最近一次调用时间):
…
magic_constraints.exception.magicTypeerror:
magicTypeerror:参数不匹配。
--------
参数:[1,2.0,3]
参数:参数(名称='bar',type_=sequence[int])
-----------------------------
```
更多…
*[magic_constraints.types][3]。
*[magic_constraints.decorator][4]。
[1]:https://docs.python.org/3/glossary.html术语抽象基类
[2]:https://en.wikipedia.org/wiki/type_内省
[3]:https://github.com/huntzhan/magic constraints/wiki/magic_constraints.types
[4]:https://github.com/huntzhan/magic-constraints/wiki/magic_constraints.decorator