Python >=3.5: Checking type annotation at runtime

前端 未结 3 1576
春和景丽
春和景丽 2020-12-29 04:59

Does the typing module (or any other module) exhibit an API to typecheck a variable at runtime, similar to isinstance() but understanding the type

相关标签:
3条回答
  • 2020-12-29 05:45

    The library Pyadaaah offers automatic runtime type and range checking as well as signals for class attributes.

    E.g. have a look at the belonging youtube video (series) starting with this: https://www.youtube.com/watch?v=Y3Hmg_HQtsY

    Which, by the way, also is a quite nice introduction to the (underlying) property/getter/setter mechanism as well.

    Pyadaaah does not use the typing library though.

    0 讨论(0)
  • 2020-12-29 05:56

    I was looking for something similar and found the library typeguard. This can automatically do runtime type checks wherever you want. Checking types directly as in the question is also supported. From the docs,

    from typeguard import check_type
    
    # Raises TypeError if there's a problem
    check_type('variablename', [1234], List[int])
    
    0 讨论(0)
  • 2020-12-29 06:05

    There is no such function in the typing module, and most likely there won't ever be.

    Checking whether an object is an instance of a class - which only means "this object was created by the class' constructor" - is a simple matter of testing some tagging.

    However, checking whether an object is an "instance" of a type is not necessarily decidable:

    assert isinstance(foo, Callable[[int], str]), 'Wrong type'
    

    Although it is easy to inspect the typing annotations of foo (assuming it's not a lambda), checking whether it complies to them is generally undecidable, by Rice's theorem.

    Even with simpler types, such as List[int] the test will easily become far too inefficient to be used for anything but the smallest toy examples.

    xs = set(range(10000))
    xs.add("a")
    xs.pop()
    assert isinstance(xs, Set[int]), 'Wrong type'
    

    The trick that allows type checker to perform this operation in a relatively efficient way, is to be conservative: the type checker tries to prove that foo always return int. If it fails, it rejects the program, even though the program may be valid, i.e. this function is likely to be rejected, although it is perfectly safe:

    def foo() -> int:
        if "a".startswith("a"):
            return 1
        return "x"
    
    0 讨论(0)
提交回复
热议问题