|
2 | 2 |
|
3 | 3 | ## Next Release |
4 | 4 |
|
| 5 | +### Better narrowing |
| 6 | + |
| 7 | +Mypy's implementation of narrowing has been substantially reworked. Mypy will now narrow more |
| 8 | +aggressively, more consistently and more correctly. In particular, you are likely to notice new |
| 9 | +narrowing behaviour in equality expressions (`==`), containment expressions (`in`), |
| 10 | +match statements, and additional expressions providing type guards. |
| 11 | + |
| 12 | +Note that mypy (and other Python type checkers) do not model the potential for various non-local |
| 13 | +operations to invalidate narrowing assumptions. This means mypy may conclude that some of your code |
| 14 | +is [unreachable](https://mypy.readthedocs.io/en/stable/common_issues.html#unreachable-code) and |
| 15 | +avoid further checking of it. The `--warn-unreachable` flag is useful for highlighting these cases. |
| 16 | +To reset narrowing, you can insert dummy reassignments, for instance `var = var` will reset |
| 17 | +all narrowing of `var.attr` and `var = cast(Any, var)` will reset all narrowing of `var`. |
| 18 | + |
| 19 | +Future work includes better narrowing on initial assignments, more narrowing to `Literal` types, |
| 20 | +and better checking of unreachable code. |
| 21 | + |
| 22 | +Contributed by Shantanu Jain. |
| 23 | + |
| 24 | +- Rework narrowing logic for equality and identity (Shantanu, PR [20492](https://github.com/python/mypy/pull/20492)) |
| 25 | +- Refactor equality and identity narrowing for clarity (Shantanu, PR [20595](https://github.com/python/mypy/pull/20595)) |
| 26 | +- Treat NotImplemented as a singleton type (Shantanu, PR [20601](https://github.com/python/mypy/pull/20601)) |
| 27 | +- Improve narrowing logic for Enum int and str subclasses (Shantanu, PR [20609](https://github.com/python/mypy/pull/20609)) |
| 28 | +- Narrow types based on collection containment (Shantanu, PR [20602](https://github.com/python/mypy/pull/20602)) |
| 29 | +- Refactor and improve narrowing for type(x) == t checks (Shantanu, PR [20634](https://github.com/python/mypy/pull/20634)) |
| 30 | +- Narrow for type expr comparisons to type exprs (Shantanu, PR [20639](https://github.com/python/mypy/pull/20639)) |
| 31 | +- Narrowing for comparisons against x.__class__ (Shantanu, PR [20642](https://github.com/python/mypy/pull/20642)) |
| 32 | +- Better narrowing with custom equality (Shantanu, PR [20643](https://github.com/python/mypy/pull/20643)) |
| 33 | +- Use a single pass for core narrowing logic, add comments (Shantanu, PR [20659](https://github.com/python/mypy/pull/20659)) |
| 34 | +- Narrowing for final type objects (Shantanu, PR [20661](https://github.com/python/mypy/pull/20661)) |
| 35 | +- Avoid narrowing type[T] (Shantanu, PR [20662](https://github.com/python/mypy/pull/20662)) |
| 36 | +- Avoid widening to Any for checks like `type(x) is type(y: Any)` (Shantanu, PR [20663](https://github.com/python/mypy/pull/20663)) |
| 37 | +- Preserve some lost narrowing, cleanup (Shantanu, PR [20674](https://github.com/python/mypy/pull/20674)) |
| 38 | +- Fix narrowing related code for types with overloaded __new__ (Shantanu, PR [20676](https://github.com/python/mypy/pull/20676)) |
| 39 | +- Fix isinstance with unions of tuples (Shantanu, PR [20677](https://github.com/python/mypy/pull/20677)) |
| 40 | +- Fix regression to chained containment (Shantanu, PR [20688](https://github.com/python/mypy/pull/20688)) |
| 41 | +- Improve else handling with custom equality (Shantanu, PR [20692](https://github.com/python/mypy/pull/20692)) |
| 42 | +- Better model runtime in isinstance and type checks (Shantanu, PR [20675](https://github.com/python/mypy/pull/20675)) |
| 43 | +- Use --warn-unreachable and --strict-equality in more tests (Shantanu, PR [20707](https://github.com/python/mypy/pull/20707)) |
| 44 | +- Model exact narrowing with type(x) checks (Shantanu, PR [20703](https://github.com/python/mypy/pull/20703)) |
| 45 | +- Short term fix for bytes narrowing (Shantanu, PR [20704](https://github.com/python/mypy/pull/20704)) |
| 46 | +- Preserve narrowing in unreachable code (Shantanu, PR [20710](https://github.com/python/mypy/pull/20710)) |
| 47 | +- Fix bug when narrowing union containing custom eq against custom eq (Shantanu, PR [20754](https://github.com/python/mypy/pull/20754)) |
| 48 | +- Fix narrowing for unions (Shantanu, PR [20728](https://github.com/python/mypy/pull/20728)) |
| 49 | +- Unsoundly narrow away from None with custom eq (Shantanu, PR [20756](https://github.com/python/mypy/pull/20756)) |
| 50 | +- Improve narrowing with numeric types (Shantanu, PR [20727](https://github.com/python/mypy/pull/20727)) |
| 51 | +- Fix narrowing with final type objects (Shantanu, PR [20743](https://github.com/python/mypy/pull/20743)) |
| 52 | +- Further improve match statement narrowing against unions (Shantanu, PR [20744](https://github.com/python/mypy/pull/20744)) |
| 53 | +- Avoid narrowing to NewType (Shantanu, PR [20766](https://github.com/python/mypy/pull/20766)) |
| 54 | +- Better match narrowing for irrefutable sequence patterns (Shantanu, PR [20782](https://github.com/python/mypy/pull/20782)) |
| 55 | +- Remove prohibit_none_typevar_overlap (Shantanu, PR [20864](https://github.com/python/mypy/pull/20864)) |
| 56 | +- Fix match statement narrowing reachability for tuples (Shantanu, PR [20896](https://github.com/python/mypy/pull/20896)) |
| 57 | +- Better handling of generics when narrowing (Shantanu, PR [20863](https://github.com/python/mypy/pull/20863)) |
| 58 | +- Better match narrowing for type objects (Shantanu, PR [20872](https://github.com/python/mypy/pull/20872)) |
| 59 | +- Narrow Callable generic return types (Shantanu, PR [20868](https://github.com/python/mypy/pull/20868)) |
| 60 | +- Better match narrowing for unions of type objects (Shantanu, PR [20905](https://github.com/python/mypy/pull/20905)) |
| 61 | +- Improve reachability in narrowing logic (Shantanu, PR [20660](https://github.com/python/mypy/pull/20660)) |
| 62 | +- Better match narrowing for irrefutable mapping patterns (Shantanu, PR [20906](https://github.com/python/mypy/pull/20906)) |
| 63 | +- Fix match statement semantic reachability (Shantanu, PR [20968](https://github.com/python/mypy/pull/20968)) |
| 64 | +- Add some additional narrowing test cases (Shantanu, PR [20598](https://github.com/python/mypy/pull/20598)) |
| 65 | +- Move tests to check-narrowing , improve them slightly (Shantanu, PR [20637](https://github.com/python/mypy/pull/20637)) |
| 66 | +- Add more tests for narrowing logic (Shantanu, PR [20672](https://github.com/python/mypy/pull/20672)) |
| 67 | +- More testing related improvements and updates (Shantanu, PR [20709](https://github.com/python/mypy/pull/20709)) |
| 68 | +- Add --warn-unreachable to more tests (Shantanu, PR [20977](https://github.com/python/mypy/pull/20977)) |
| 69 | + |
5 | 70 | ### Drop Support for Python 3.9 |
6 | 71 |
|
7 | 72 | Mypy no longer supports running with Python 3.9, which has reached end-of-life. |
|
0 commit comments