mypy README

The project's README from github.com/python/mypy. As one markdown file: README.md.

mypy logo

Mypy: Static Typing for Python

Stable Version Downloads Build Status Documentation Status Chat at https://gitter.im/python/typing Checked with mypy Code style: black Linting: Ruff

Got a question?

We are always happy to answer questions! Here are some good places to ask them:

If you're just getting started, the documentation and type hints cheat sheet can also help answer questions.

If you think you've found a bug:

To report a bug or request an enhancement:

To discuss a new type system feature:

What is mypy?

Mypy is a static type checker for Python.

Type checkers help ensure that you're using variables and functions in your code correctly. With mypy, add type hints (PEP 484) to your Python programs, and mypy will warn you when you use those types incorrectly.

Python is a dynamic language, so usually you'll only see errors in your code when you attempt to run it. Mypy is a static checker, so it finds bugs in your programs without even running them!

Here is a small example to whet your appetite:

number = input("What is your favourite number?")
print("It is", number + 1)  # error: Unsupported operand types for + ("str" and "int")

Adding type hints for mypy does not interfere with the way your program would otherwise run. Think of type hints as similar to comments! You can always use the Python interpreter to run your code, even if mypy reports errors.

Mypy is designed with gradual typing in mind. This means you can add type hints to your code base slowly and that you can always fall back to dynamic typing when static typing is not convenient.

Mypy has a powerful and easy-to-use type system, supporting features such as type inference, generics, callable types, tuple types, union types, structural subtyping and more. Using mypy will make your programs easier to understand, debug, and maintain.

See the documentation for more examples and information.

In particular, see:

Quick start

Mypy can be installed using pip:

python3 -m pip install -U mypy

If you want to run the latest version of the code, you can install from the repo directly:

python3 -m pip install -U git+https://github.com/python/mypy.git

Now you can type-check the statically typed parts of a program like this:

mypy PROGRAM

You can always use the Python interpreter to run your statically typed programs, even if mypy reports type errors:

python3 PROGRAM

If you are working with large code bases, you can run mypy in daemon mode, that will give much faster (often sub-second) incremental updates:

dmypy run -- PROGRAM

You can also try mypy in an online playground (developed by Yusuke Miyazaki).

Integrations

Mypy can be integrated into popular IDEs:

Web site and documentation

Additional information is available at the web site:

https://www.mypy-lang.org/

Jump straight to the documentation:

https://mypy.readthedocs.io/

Follow along our changelog at:

https://mypy-lang.blogspot.com/

Contributing

Help in testing, development, documentation and other tasks is highly appreciated and useful to the project. There are tasks for contributors of all experience levels.

To get started with developing mypy, see CONTRIBUTING.md.

Mypyc and compiled version of mypy

Mypyc uses Python type hints to compile Python modules to faster C extensions. Mypy is itself compiled using mypyc: this makes mypy approximately 4 times faster than if interpreted!

To install an interpreted mypy instead, use:

python3 -m pip install --no-binary mypy -U mypy

To use a compiled version of a development version of mypy, directly install a binary from https://github.com/mypyc/mypy_mypyc-wheels/releases/latest.

To contribute to the mypyc project, check out the issue tracker at https://github.com/mypyc/mypyc

mypy (python/mypy)

Searching Database…

20.7K ·GitHub
这是什么?Mypy 是一个 Python 静态类型检查工具:你在代码里添加类型标注(PEP 484),当你用错这些类型时它会发出警告,不用运行程序就能发现 bug。
这和直接运行代码看哪里报错有什么区别?在 Python 这样的动态语言里,通常只有运行代码时才能看到错误。Mypy 是静态检查程序的,所以不用执行代码就能发现 bug。
我该怎么开始使用它?先用 pip 安装(运行 'pip install mypy'),然后对你的程序文件运行 'mypy'。如果想用最新的开发版代码,可以直接从仓库安装。
我是不是得先给整个代码库都加上类型标注,mypy 才有用?不用——mypy 就是为渐进式类型设计 的:可以一点点地添加标注,静态类型不方便的地方就继续用动态类型。
如果 mypy 报告了类型错误,我的程序还能运行吗?能。类型标注就像注释一样,不会改变程序的运行方式——即使 mypy 报告了错误,你照样可以用 Python 解释器运行代码。
我不安装任何东西就能试用 mypy 吗?可以——有一个在线 playground(由 Yusuke Miyazaki 开发),可以在浏览器里直接试用 mypy。
Mypyc 是什么?一个配套项目,它利用 Python 类型标注把 Python 模块编译成更快的 C 扩展。Mypy 本身就是用 mypyc 编译的,因此比解释执行的版本快约 4 倍。
通过 pre-commit 运行 mypy 有什么需要注意的吗?pre-commit 钩子(mirrors-mypy)是可用的,但默认情况下它会限制 mypy 对第三方依赖的分析能力。
Creator
python
Size
85.7 MB
Stars
20.7K
Updated
CommitOct 17
Age
13y
Commits · 90 Days