|
1 | 1 | # Mypy Release Notes |
2 | 2 |
|
| 3 | +## Next release |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Mypy 1.10 |
| 8 | + |
| 9 | +We’ve just uploaded mypy 1.10 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)). Mypy is a static type checker for Python. This release includes new features, performance improvements and bug fixes. You can install it as follows: |
| 10 | + |
| 11 | + python3 -m pip install -U mypy |
| 12 | + |
| 13 | +You can read the full documentation for this release on [Read the Docs](http://mypy.readthedocs.io). |
| 14 | + |
| 15 | +#### Support TypeIs (PEP 742) |
| 16 | + |
| 17 | +Mypy now supports `TypeIs` ([PEP 742](https://peps.python.org/pep-0742/)), which allows |
| 18 | +functions to narrow the type of a value, similar to `isinstance()`. Unlike `TypeGuard`, |
| 19 | +`TypeIs` can narrow in both the `if` and `else` branches of an if statement: |
| 20 | + |
| 21 | +```python |
| 22 | +from typing_extensions import TypeIs |
| 23 | + |
| 24 | +def is_str(s: object) -> TypeIs[str]: |
| 25 | + return isinstance(s, str) |
| 26 | + |
| 27 | +def f(o: str | int) -> None: |
| 28 | + if is_str(o): |
| 29 | + # Type of o is 'str' |
| 30 | + ... |
| 31 | + else: |
| 32 | + # Type of o is 'int' |
| 33 | + ... |
| 34 | +``` |
| 35 | + |
| 36 | +`TypeIs` will be added to the `typing` module in Python 3.13, but it |
| 37 | +can be used on earlier Python versions by importing it from |
| 38 | +`typing_extensions`. |
| 39 | + |
| 40 | +This feature was contributed by Jelle Zijlstra (PR [16898](https://github.com/python/mypy/pull/16898)). |
| 41 | + |
| 42 | +#### Support TypeVar Defaults (PEP 696) |
| 43 | + |
| 44 | +[PEP 696](https://peps.python.org/pep-0696/) adds support for type parameter defaults. |
| 45 | +Example: |
| 46 | + |
| 47 | +```python |
| 48 | +from typing import Generic |
| 49 | +from typing_extensions import TypeVar |
| 50 | + |
| 51 | +T = TypeVar("T", default=int) |
| 52 | + |
| 53 | +class C(Generic[T]): |
| 54 | + ... |
| 55 | + |
| 56 | +x: C = ... |
| 57 | +y: C[str] = ... |
| 58 | +reveal_type(x) # C[int], because of the default |
| 59 | +reveal_type(y) # C[str] |
| 60 | +``` |
| 61 | + |
| 62 | +TypeVar defaults will be added to the `typing` module in Python 3.13, but they |
| 63 | +can be used with earlier Python releases by importing `TypeVar` from |
| 64 | +`typing_extensions`. |
| 65 | + |
| 66 | +This feature was contributed by Marc Mueller (PR [16878](https://github.com/python/mypy/pull/16878) |
| 67 | +and PR [16925](https://github.com/python/mypy/pull/16925)). |
| 68 | + |
| 69 | +#### Support TypeAliasType (PEP 695) |
| 70 | +As part of the initial steps towards implementing [PEP 695](https://peps.python.org/pep-0695/), mypy now supports `TypeAliasType`. |
| 71 | +`TypeAliasType` provides a backport of the new `type` statement in Python 3.12. |
| 72 | + |
| 73 | +```python |
| 74 | +type ListOrSet[T] = list[T] | set[T] |
| 75 | +``` |
| 76 | + |
| 77 | +is equivalent to: |
| 78 | + |
| 79 | +```python |
| 80 | +T = TypeVar("T") |
| 81 | +ListOrSet = TypeAliasType("ListOrSet", list[T] | set[T], type_params=(T,)) |
| 82 | +``` |
| 83 | + |
| 84 | +Example of use in mypy: |
| 85 | + |
| 86 | +```python |
| 87 | +from typing_extensions import TypeAliasType, TypeVar |
| 88 | + |
| 89 | +NewUnionType = TypeAliasType("NewUnionType", int | str) |
| 90 | +x: NewUnionType = 42 |
| 91 | +y: NewUnionType = 'a' |
| 92 | +z: NewUnionType = object() # error: Incompatible types in assignment (expression has type "object", variable has type "int | str") [assignment] |
| 93 | + |
| 94 | +T = TypeVar("T") |
| 95 | +ListOrSet = TypeAliasType("ListOrSet", list[T] | set[T], type_params=(T,)) |
| 96 | +a: ListOrSet[int] = [1, 2] |
| 97 | +b: ListOrSet[str] = {'a', 'b'} |
| 98 | +c: ListOrSet[str] = 'test' # error: Incompatible types in assignment (expression has type "str", variable has type "list[str] | set[str]") [assignment] |
| 99 | +``` |
| 100 | + |
| 101 | +`TypeAliasType` was added to the `typing` module in Python 3.12, but it can be used with earlier Python releases by importing from `typing_extensions`. |
| 102 | + |
| 103 | +This feature was contributed by Ali Hamdan (PR [16926](https://github.com/python/mypy/pull/16926), PR [17038](https://github.com/python/mypy/pull/17038) and PR [17053](https://github.com/python/mypy/pull/17053)) |
| 104 | + |
| 105 | +#### Detect Additional Unsafe Uses of super() |
| 106 | + |
| 107 | +Mypy will reject unsafe uses of `super()` more consistently, when the target has a |
| 108 | +trivial (empty) body. Example: |
| 109 | + |
| 110 | +```python |
| 111 | +class Proto(Protocol): |
| 112 | + def method(self) -> int: ... |
| 113 | + |
| 114 | +class Sub(Proto): |
| 115 | + def method(self) -> int: |
| 116 | + return super().meth() # Error (unsafe) |
| 117 | +``` |
| 118 | + |
| 119 | +This feature was contributed by Shantanu (PR [16756](https://github.com/python/mypy/pull/16756)). |
| 120 | + |
| 121 | +#### Stubgen Improvements |
| 122 | +- Preserve empty tuple annotation (Ali Hamdan, PR [16907](https://github.com/python/mypy/pull/16907)) |
| 123 | +- Add support for PEP 570 positional-only parameters (Ali Hamdan, PR [16904](https://github.com/python/mypy/pull/16904)) |
| 124 | +- Replace obsolete typing aliases with builtin containers (Ali Hamdan, PR [16780](https://github.com/python/mypy/pull/16780)) |
| 125 | +- Fix generated dataclass `__init__` signature (Ali Hamdan, PR [16906](https://github.com/python/mypy/pull/16906)) |
| 126 | + |
| 127 | +#### Mypyc Improvements |
| 128 | + |
| 129 | +- Provide an easier way to define IR-to-IR transforms (Jukka Lehtosalo, PR [16998](https://github.com/python/mypy/pull/16998)) |
| 130 | +- Implement lowering pass and add primitives for int (in)equality (Jukka Lehtosalo, PR [17027](https://github.com/python/mypy/pull/17027)) |
| 131 | +- Implement lowering for remaining tagged integer comparisons (Jukka Lehtosalo, PR [17040](https://github.com/python/mypy/pull/17040)) |
| 132 | +- Optimize away some bool/bit registers (Jukka Lehtosalo, PR [17022](https://github.com/python/mypy/pull/17022)) |
| 133 | +- Remangle redefined names produced by async with (Richard Si, PR [16408](https://github.com/python/mypy/pull/16408)) |
| 134 | +- Optimize TYPE_CHECKING to False at Runtime (Srinivas Lade, PR [16263](https://github.com/python/mypy/pull/16263)) |
| 135 | +- Fix compilation of unreachable comprehensions (Richard Si, PR [15721](https://github.com/python/mypy/pull/15721)) |
| 136 | +- Don't crash on non-inlinable final local reads (Richard Si, PR [15719](https://github.com/python/mypy/pull/15719)) |
| 137 | + |
| 138 | +#### Documentation Improvements |
| 139 | +- Import `TypedDict` from `typing` instead of `typing_extensions` (Riccardo Di Maio, PR [16958](https://github.com/python/mypy/pull/16958)) |
| 140 | +- Add missing `mutable-override` to section title (James Braza, PR [16886](https://github.com/python/mypy/pull/16886)) |
| 141 | + |
| 142 | +#### Error Reporting Improvements |
| 143 | + |
| 144 | +- Use lower-case generics more consistently in error messages (Jukka Lehtosalo, PR [17035](https://github.com/python/mypy/pull/17035)) |
| 145 | + |
| 146 | +#### Other Notable Changes and Fixes |
| 147 | +- Fix incorrect inferred type when accessing descriptor on union type (Matthieu Devlin, PR [16604](https://github.com/python/mypy/pull/16604)) |
| 148 | +- Fix crash when expanding invalid `Unpack` in a `Callable` alias (Ali Hamdan, PR [17028](https://github.com/python/mypy/pull/17028)) |
| 149 | +- Fix false positive when string formatting with string enum (roberfi, PR [16555](https://github.com/python/mypy/pull/16555)) |
| 150 | +- Narrow individual items when matching a tuple to a sequence pattern (Loïc Simon, PR [16905](https://github.com/python/mypy/pull/16905)) |
| 151 | +- Fix false positive from type variable within TypeGuard or TypeIs (Evgeniy Slobodkin, PR [17071](https://github.com/python/mypy/pull/17071)) |
| 152 | +- Improve `yield from` inference for unions of generators (Shantanu, PR [16717](https://github.com/python/mypy/pull/16717)) |
| 153 | +- Fix emulating hash method logic in `attrs` classes (Hashem, PR [17016](https://github.com/python/mypy/pull/17016)) |
| 154 | +- Add reverted typeshed commit that uses `ParamSpec` for `functools.wraps` (Tamir Duberstein, PR [16942](https://github.com/python/mypy/pull/16942)) |
| 155 | +- Fix type narrowing for `types.EllipsisType` (Shantanu, PR [17003](https://github.com/python/mypy/pull/17003)) |
| 156 | +- Fix single item enum match type exhaustion (Oskari Lehto, PR [16966](https://github.com/python/mypy/pull/16966)) |
| 157 | +- Improve type inference with empty collections (Marc Mueller, PR [16994](https://github.com/python/mypy/pull/16994)) |
| 158 | +- Fix override checking for decorated property (Shantanu, PR [16856](https://github.com/python/mypy/pull/16856)) |
| 159 | +- Fix narrowing on match with function subject (Edward Paget, PR [16503](https://github.com/python/mypy/pull/16503)) |
| 160 | +- Allow `+N` within `Literal[...]` (Spencer Brown, PR [16910](https://github.com/python/mypy/pull/16910)) |
| 161 | +- Experimental: Support TypedDict within `type[...]` (Marc Mueller, PR [16963](https://github.com/python/mypy/pull/16963)) |
| 162 | +- Experimtental: Fix issue with TypedDict with optional keys in `type[...]` (Marc Mueller, PR [17068](https://github.com/python/mypy/pull/17068)) |
| 163 | + |
| 164 | +#### Typeshed Updates |
| 165 | + |
| 166 | +Please see [git log](https://github.com/python/typeshed/commits/main?after=7c8e82fe483a40ec4cb0a2505cfdb0f3e7cc81d9+0&branch=main&path=stdlib) for full list of standard library typeshed stub changes. |
| 167 | + |
| 168 | + |
| 169 | +#### Acknowledgements |
| 170 | +Thanks to all mypy contributors who contributed to this release: |
| 171 | + |
| 172 | +- Alex Waygood |
| 173 | +- Ali Hamdan |
| 174 | +- Edward Paget |
| 175 | +- Evgeniy Slobodkin |
| 176 | +- Hashem |
| 177 | +- hesam |
| 178 | +- Hugo van Kemenade |
| 179 | +- Ihor |
| 180 | +- James Braza |
| 181 | +- Jelle Zijlstra |
| 182 | +- jhance |
| 183 | +- Jukka Lehtosalo |
| 184 | +- Loïc Simon |
| 185 | +- Marc Mueller |
| 186 | +- Matthieu Devlin |
| 187 | +- Michael R. Crusoe |
| 188 | +- Nikita Sobolev |
| 189 | +- Oskari Lehto |
| 190 | +- Riccardo Di Maio |
| 191 | +- Richard Si |
| 192 | +- roberfi |
| 193 | +- Roman Solomatin |
| 194 | +- Sam Xifaras |
| 195 | +- Shantanu |
| 196 | +- Spencer Brown |
| 197 | +- Srinivas Lade |
| 198 | +- Tamir Duberstein |
| 199 | +- youkaichao |
| 200 | + |
| 201 | +I’d also like to thank my employer, Dropbox, for supporting mypy development. |
| 202 | + |
| 203 | + |
3 | 204 | ## Mypy 1.9 |
4 | 205 |
|
5 | 206 | We’ve just uploaded mypy 1.9 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)). Mypy is a static type checker for Python. This release includes new features, performance improvements and bug fixes. You can install it as follows: |
|
0 commit comments