Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
mypy 1.16.1 documentation
Logo
mypy 1.16.1 documentation

First steps

Type system reference

Configuring and running mypy

Miscellaneous

Project Links

Back to top

Frequently Asked Questions

Why have both dynamic and static typing?

Dynamic typing can be flexible, powerful, convenient and easy. Butit’s not always the best approach; there are good reasons why manydevelopers choose to use statically typed languages or static typingfor Python.

Here are some potential benefits of mypy-style static typing:

  • Static typing can make programs easier to understand andmaintain. Type declarations can serve as machine-checkeddocumentation. This is important as code is typically read much moreoften than modified, and this is especially important for large andcomplex programs.

  • Static typing can help you find bugs earlier and with less testingand debugging. Especially in large and complex projects this can bea major time-saver.

  • Static typing can help you find difficult-to-find bugs before yourcode goes into production. This can improve reliability and reducethe number of security issues.

  • Static typing makes it practical to build very useful developmenttools that can improve programming productivity or software quality,including IDEs with precise and reliable code completion, staticanalysis tools, etc.

  • You can get the benefits of both dynamic and static typing in asingle language. Dynamic typing can be perfect for a small projector for writing the UI of your program, for example. As your programgrows, you can adapt tricky application logic to static typing tohelp maintenance.

See also thefront page of the mypy website.

Would my project benefit from static typing?

For many projects dynamic typing is perfectly fine (we think thatPython is a great language). But sometimes your projects demand biggerguns, and that’s when mypy may come in handy.

If some of these ring true for your projects, mypy (and static typing)may be useful:

  • Your project is large or complex.

  • Your codebase must be maintained for a long time.

  • Multiple developers are working on the same code.

  • Running tests takes a lot of time or work (type checking helpsyou find errors quickly early in development, reducing the number oftesting iterations).

  • Some project members (devs or management) don’t like dynamic typing,but others prefer dynamic typing and Python syntax. Mypy could be asolution that everybody finds easy to accept.

  • You want to future-proof your project even if currently none of theabove really apply. The earlier you start, the easier it will be toadopt static typing.

Can I use mypy to type check my existing Python code?

Mypy supports most Python features and idioms, and many large Pythonprojects are using mypy successfully. Code that uses complexintrospection or metaprogramming may be impractical to type check, butit should still be possible to use static typing in other parts of acodebase that are less dynamic.

Will static typing make my programs run faster?

Mypy only does static type checking and it does not improveperformance. It has a minimal performance impact. In the future, therecould be other tools that can compile statically typed mypy code to Cmodules or to efficient JVM bytecode, for example, but this is outsidethe scope of the mypy project.

Is mypy free?

Yes. Mypy is free software, and it can also be used for commercial andproprietary projects. Mypy is available under the MIT license.

Can I use duck typing with mypy?

Mypy provides support for bothnominal subtyping andstructural subtyping.Structural subtyping can be thought of as “static duck typing”.Some argue that structural subtyping is better suited for languages with ducktyping such as Python. Mypy however primarily uses nominal subtyping,leaving structural subtyping mostly opt-in (except for built-in protocolssuch asIterable that always support structuralsubtyping). Here are some reasons why:

  1. It is easy to generate short and informative error messages whenusing a nominal type system. This is especially important whenusing type inference.

  2. Python provides built-in support for nominalisinstance() tests andthey are widely used in programs. Only limited support for structuralisinstance() is available, and it’s less type safe than nominal type tests.

  3. Many programmers are already familiar with static, nominal subtyping and ithas been successfully used in languages such as Java, C++ andC#. Fewer languages use structural subtyping.

However, structural subtyping can also be useful. For example, a “public API”may be more flexible if it is typed with protocols. Also, using protocol typesremoves the necessity to explicitly declare implementations of ABCs.As a rule of thumb, we recommend using nominal classes where possible, andprotocols where necessary. For more details about protocol types and structuralsubtyping seeProtocols and structural subtyping andPEP 544.

I like Python and I have no need for static typing

The aim of mypy is not to convince everybody to write statically typedPython – static typing is entirely optional, now and in thefuture. The goal is to give more options for Python programmers, tomake Python a more competitive alternative to other statically typedlanguages in large projects, to improve programmer productivity, andto improve software quality.

How are mypy programs different from normal Python?

Since you use a vanilla Python implementation to run mypy programs,mypy programs are also Python programs. The type checker may givewarnings for some valid Python code, but the code is still alwaysrunnable. Also, a few Python features are still notsupported by mypy, but this is gradually improving.

The obvious difference is the availability of static typechecking. The sectionCommon issues and solutions mentions somemodifications to Python code that may be required to make code typecheck without errors. Also, your code must make definedattributes explicit.

Mypy supports modular, efficient type checking, and this seems torule out type checking some language features, such as arbitrarymonkey patching of methods.

How is mypy different from Cython?

Cython is a variant of Python that supportscompilation to CPython C modules. It can give major speedups tocertain classes of programs compared to CPython, and it providesstatic typing (though this is different from mypy). Mypy differs inthe following aspects, among others:

  • Cython is much more focused on performance than mypy. Mypy is onlyabout static type checking, and increasing performance is not adirect goal.

  • The mypy syntax is arguably simpler and more “Pythonic” (no cdef/cpdef, etc.) for statically typed code.

  • The mypy syntax is compatible with Python. Mypy programs are normalPython programs that can be run using any Pythonimplementation. Cython has many incompatible extensions to Pythonsyntax, and Cython programs generally cannot be run without firstcompiling them to CPython extension modules via C. Cython also has apure Python mode, but it seems to support only a subset of Cythonfunctionality, and the syntax is quite verbose.

  • Mypy has a different set of type system features. For example, mypyhas genericity (parametric polymorphism), function types andbidirectional type inference, which are not supported byCython. (Cython has fused types that are different but related tomypy generics. Mypy also has a similar feature as an extension ofgenerics.)

  • The mypy type checker knows about the static types of many Pythonstdlib modules and can effectively type check code that uses them.

  • Cython supports accessing C functions directly and many features aredefined in terms of translating them to C or C++. Mypy just usesPython semantics, and mypy does not deal with accessing C libraryfunctionality.

Does it run on PyPy?

Somewhat. With PyPy 3.8, mypy is at least able to type check itself.With older versions of PyPy, mypy relies ontyped-ast, which uses several APIs thatPyPy does not support (including some internal CPython APIs).

Mypy is a cool project. Can I help?

Any help is much appreciated!Contact the developers if you wouldlike to contribute. Any help related to development, design,publicity, documentation, testing, web site maintenance, financing,etc. can be helpful. You can learn a lot by contributing, and anybodycan help, even beginners! However, some knowledge of compilers and/ortype systems is essential if you want to work on mypy internals.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp