简体   繁体   English

python:使用类型提示动态检查类型

[英]python : using type hints to dynamically check types

python supports type hints: python 支持类型提示:

https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html

I was wondering if these hints can also be used to dynamically enforce types during runtime.我想知道这些提示是否也可用于在运行时动态强制类型。

For example:例如:

class C:

    def __init__(self):
        self.a : int = 0

    def __str__(self):
        return str(self.a)

    @classmethod
    def get(cls,**kwargs):
        c = cls()
        for k,v  in kwargs.items():
            setattr(c,k,v) 
            # ValueError exception thrown here ?
        return c

attrs = {"a":"a"} # developer wanted an int !
c = C.get(**attrs)
print(c)

In short, I'd like to avoid to re-enter the type of the attribute "a" in the get function:简而言之,我想避免在 get 函数中重新输入属性“a”的类型:

    @classmethod
    def get(cls,**kwargs):
        c = cls()
        for k,v  in kwargs.items():
            if k=="a" and not isinstance(v,int):
                raise ValueError()
            setattr(c,k,v) 
        return c

Would there be a way to "reuse" the information given in the constructor that "a" is expected to be an int ?有没有办法“重用”构造函数中给出的“a”应该是 int 的信息?

Note: answer to this question shows that at least for functions introspection on the arguments type hints can be accessed:注意:对这个问题的回答表明,至少可以访问参数类型提示上的函数自省:

How to introspect on PEP 484 type hints? 如何内省 PEP 484 类型提示?

I was wondering if these hints can also be used to dynamically enforce types during runtime.我想知道这些提示是否也可用于在运行时动态强制类型。

In some cases and with external lib - the answer is yes.在某些情况下,使用外部库 - 答案是肯定的。 Read below.参见下文。

If the actual use-case you have is simple like your C class I would go and use dataclass and a library like dacite.如果您拥有的实际用例像您的C类一样简单,我会去使用数据类和像 dacite 这样的库。 You will not be able to create c2 since you are not passing an int.您将无法创建 c2,因为您没有传递 int。
So dacite managed to "see" that a should be int and raised an exception所以英安岩设法“看到” a应该是 int 并引发异常

from dataclasses import dataclass
from dacite import from_dict
@dataclass
class C:
  a:int = 0

d1 = {'a':3}

c1: C = from_dict(C,d1)
print(c1)

d2 = {'a':'3'}

c2: C = from_dict(C,d2)

output输出

C(a=3) C(a=3)

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    c2: C = from_dict(C,d2)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/dacite/core.py", line 68, in from_dict
    raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
dacite.exceptions.WrongTypeError: wrong value type for field "a" - should be "int" instead of value "3" of type "str"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM