Tools for static type checking in Python

前端 未结 7 2019
灰色年华
灰色年华 2020-12-01 09:00

I\'m working with a large existing Python codebase and would like to start adding in type annotations so I can get some level of static checking. I\'m imagining something l

7条回答
  •  一生所求
    2020-12-01 09:36

    Edit 2016-11-11: Just use mypy. Type hints can be added gradually. In Python 3 source code, it verifies standard PEP 484 type hints. Types can still be expressed in Python 2 using special comments. Guido likes it.

    This post was originally written a long time ago before mypy was a thing. I've preserved the post's original content below, even though it isn't quite accurate.


    Original post:

    You might want to check out some of the projects mentioned in this related StackOverflow post on static analysis for Python.

    In summary:

    • pylint
    • PyFlakes
    • PyChecker

    Since Python uses duck typing extensively, things that might be called "type errors" in other languages might end up being "object X doesn't support method Y" in Python.

    Edit 2011-05-17:

    I agree with delnan that static typing is not possible for Python [apparently wrong]. But since our skepticism doesn't seem to deter you, I can only give you more information on the subject. I present:

    • A discussion of type inference for Python. (Other links are from here.)
    • Guido van van Rossum's articles on adding optional static typing: part 1 and part 2.
    • RPython, a subset of Python that might stand a chance of being statically analyzed enough to do some form of type checking.

提交回复
热议问题