We’ve just uploaded mypy 2.1.0 to the Python Package Index (PyPI).
Mypy is a static type checker for Python. This release includes new features, performance
improvements and bug fixes. You can install it as follows:
python3 -m pip install -U mypy
You can read the full documentation for this release on Read the Docs.
librt.vecs: Fast Growable Array Type for Mypyc
The new librt.vecs module provides an efficient growable array type vec that is
optimized for mypyc use. It provides fast, packed arrays with integer and floating point
value types, which can be several times faster than list, and tens of times faster
than array.array in code compiled using mypyc. It also supports nested vec objects and
non-value-type items, such as vec[vec[str]].
librt.random: Fast Pseudo-Random Number Generation
The new librt.random module provides fast pseudo-random number generation that is
optimized for code compiled using mypyc. It can be 3x to 10x faster than the stdlib
random module in compiled code.
We’ve just uploaded mypy 2.0.0 to the Python Package Index (PyPI).
Mypy is a static type checker for Python. This release includes new features, performance
improvements and bug fixes. There are also changes to options and defaults.
You can install it as follows:
python3 -m pip install -U mypy
You can read the full documentation for this release on Read the Docs.
Enable --local-partial-types by Default
This flag affects the inference of types based on assignments in other scopes.
For now, explicitly disabling this continues to be supported, but this support will be removed
in the future as the legacy behaviour is hard to support with other current and future features
in mypy, like the daemon or the new implementation of flexible redefinitions.
Contributed by Ivan Levkivskyi, Jukka Lehtosalo, Shantanu in PR 21163.
Enable --strict-bytes by Default
Per PEP 688, mypy no longer treats bytearray and memoryview
values as assignable to the bytes type.
The --allow-redefinition flag now behaves like --allow-redefinition-new in mypy 1.20
and earlier. The new behavior is generally more flexible. For example, you can have different
types for a variable in different blocks:
# mypy: allow-redefinition
def foo(cond: bool) -> None:
if cond:
for x in ["a", "b"]:
# Type of "x" is "str" here
...
else:
for x in [1, 2]:
# Type of "x" is "int" here
...
The new behavior requires --local-partial-types, which is now enabled by default.
However, --allow-redefinition doesn't allow giving two type annotations for the same
variable. The old behavior (sometimes) allows this. Code like this now generates an error
when using --allow-redefinition:
You can still use --allow-redefinition-old to fall back to the old behavior. We have no
plans to remove the legacy behavior, but the old functionality is maintained on a best effort
basis.
Mypy now supports experimental parallel and incremental type checking. Use --num-workers N
or -nN to use N worker processes to type check in parallel. The speedup depends on the
import structure of your codebase and your environment, but for large projects we've seen
performance gains of up to 5x when using 8 worker processes.
Parallel type checking implicitly enables the new native parser. There are still some
minor semantic differences between parallel and non-parallel modes, which we will be fixing
in future mypy releases.
Contributed by Ivan Levkivskyi, with additional contributions from Emma Smith and Jukka
Lehtosalo.
Recent related changes since the last release:
Freeze garbage collection in parallel workers for 4-5% speedup (Ivan Levkivskyi, PR 21302)
Expose --num-workers and --native-parser (Ivan Levkivskyi, PR 21387)
Split type checking into interface and implementation in parallel workers (Ivan Levkivskyi, PR 21119)
Batch module groups for parallel processing (Ivan Levkivskyi, PR 21287)
Parse files in parallel when possible (Ivan Levkivskyi, PR 21175)
Use parallel parsing at all stages (Ivan Levkivskyi, PR 21266)
Fix sequential bottleneck in parallel parsing (Jukka Lehtosalo, PR 21291)
Fail fast when a user tries to generate reports with parallel workers (Ivan Levkivskyi, PR 21341)
Partially support old NumPy plugin in parallel type checking (Ivan Levkivskyi, PR 21324)
Handle reachability consistently in parallel type checking (Ivan Levkivskyi, PR 21322)
Always respect @no_type_check in parallel type checking (Ivan Levkivskyi, PR 21320)
Minor fixes in parallel checking (Ivan Levkivskyi, PR 21319)
Fix plugin logic in parallel type checking (Ivan Levkivskyi, PR 21252)
Fix Windows IPC race condition when using parallel checking (Jukka Lehtosalo, PR 21228)
Report parallel worker exit status on receive failure (Jukka Lehtosalo, PR 21224)
Drop Support for Targeting Python 3.9
Mypy no longer supports type checking code with --python-version 3.9.
Use --python-version 3.10 or newer.
Contributed by Shantanu, Marc Mueller in PR 21243.
Remove Special Casing of Legacy Bundled Stubs
Mypy used to bundle stubs for a few packages in versions 0.812 and earlier. To navigate the
transition, mypy used to report missing types for these packages even if --ignore-missing-imports
was set. Mypy now consistently respects --ignore-missing-imports for all packages.
Prevent Assignment to None for Non-Optional Class Variables with Type Comments
Mypy used to allow assignment to None for class variables when using type comments. This was a
common idiom in Python 3.5 and earlier, prior to the introduction of variable annotations.
However, this was a soundness hole and has now been removed.
librt.strings: String and Bytes Primitives for Mypyc
In mypy 1.20, we introduced librt as a standard library
for mypyc that fills in some gaps in the Python standard library and the C API.
This release adds the new module librt.strings, which contains utilities for building
string and bytes objects, and for accessing and generating binary data:
StringWriter and BytesWriter classes allow quickly building str and bytes objects from parts.
read_* and write_* functions provide fast reading and writing of binary-encoded data.