Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Optional static typing for Python

License

NotificationsYou must be signed in to change notification settings

python/mypy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mypy logo

Mypy: Static Typing for Python

Stable VersionDownloadsBuild StatusDocumentation StatusChat at https://gitter.im/python/typingChecked with mypyCode style: blackLinting: 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 documentationandtype hints cheat sheetcan 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 codecorrectly. With mypy, add type hints (PEP 484)to your Python programs, and mypy will warn you when you use those typesincorrectly.

Python is a dynamic language, so usually you'll only see errors in your codewhen you attempt to run it. Mypy is astatic checker, so it finds bugsin 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 wouldotherwise run. Think of type hints as similar to comments! You can always usethe Python interpreter to run your code, even if mypy reports errors.

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

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

Seethe documentation formore 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 therepo directly:

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

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

mypy PROGRAM

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

python3 PROGRAM

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

dmypy run -- PROGRAM

You can also try mypy in anonline playground (developed byYusuke Miyazaki).

Integrations

Mypy can be integrated into popular IDEs:

  • VS Code: providesbasic integration with mypy.
  • Vim:
    • UsingSyntastic: in~/.vimrc addlet g:syntastic_python_checkers=['mypy']
    • UsingALE: should be enabled by default whenmypy is installed,or can be explicitly enabled by addinglet b:ale_linters = ['mypy'] in~/vim/ftplugin/python.vim
  • Emacs: usingFlycheck
  • Sublime Text:SublimeLinter-contrib-mypy
  • PyCharm:mypy plugin
  • pre-commit: usepre-commit mirrors-mypy, althoughnote by default this will limit mypy's ability to analyse your third party dependencies.

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 ishighly appreciated and useful to the project. There are tasks forcontributors of all experience levels.

To get started with developing mypy, seeCONTRIBUTING.md.

Mypyc and compiled version of mypy

Mypyc uses Python type hints to compile Pythonmodules to faster C extensions. Mypy is itself compiled using mypyc: this makesmypy 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 developmentversion of mypy, directly install a binary fromhttps://github.com/mypyc/mypy_mypyc-wheels/releases/latest.

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


[8]ページ先頭

©2009-2025 Movatter.jp