From 88737085e9398c5bab2552310a0d9f49773f4ed3 Mon Sep 17 00:00:00 2001 From: Valentin Stanciu <250871+svalentin@users.noreply.github.com> Date: Wed, 24 Apr 2024 12:48:50 +0100 Subject: [PATCH 1/3] Update CHANGELOG.md to point out PEP 695 initial support --- CHANGELOG.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a90997f6cc3ac..513a6e2592996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,21 @@ can be used with earlier Python releases by importing `TypeVar` from This feature was contributed by Marc Mueller (PR [16878](https://github.com/python/mypy/pull/16878) and PR [16925](https://github.com/python/mypy/pull/16925)). +#### Support TypeAliasType (PEP 695) +As part of initial steps towards implementing [PEP 695](https://peps.python.org/pep-0695/), mypy now supports TypeAliasType. + +```python +from typing import Union +from typing_extensions import TypeAliasType + +NewUnionType = TypeAliasType("NewUnionType", Union[int, str]) +x: NewUnionType = 42 # OK +y: NewUnionType = 'a' # OK +z: NewUnionType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]") +``` + +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)) + #### Detect Additional Unsafe Uses of super() Mypy will reject unsafe uses of `super()` more consistently, when the target has a @@ -98,7 +113,6 @@ This feature was contributed by Shantanu (PR [16756](https://github.com/python/m - Optimize TYPE_CHECKING to False at Runtime (Srinivas Lade, PR [16263](https://github.com/python/mypy/pull/16263)) - Fix compilation of unreachable comprehensions (Richard Si, PR [15721](https://github.com/python/mypy/pull/15721)) - Don't crash on non-inlinable final local reads (Richard Si, PR [15719](https://github.com/python/mypy/pull/15719)) -- Support `TypeAliasType` (Ali Hamdan, PR [16926](https://github.com/python/mypy/pull/16926)) #### Documentation Improvements - Import `TypedDict` from `typing` instead of `typing_extensions` (Riccardo Di Maio, PR [16958](https://github.com/python/mypy/pull/16958)) @@ -106,7 +120,6 @@ This feature was contributed by Shantanu (PR [16756](https://github.com/python/m #### Error Reporting Improvements -- Improve error message for bound TypeVar in TypeAliasType (Ali Hamdan, PR [17053](https://github.com/python/mypy/pull/17053)) - Use lower-case generics more consistently in error messages (Jukka Lehtosalo, PR [17035](https://github.com/python/mypy/pull/17035)) #### Other Notable Changes and Fixes @@ -116,7 +129,6 @@ This feature was contributed by Shantanu (PR [16756](https://github.com/python/m - Narrow individual items when matching a tuple to a sequence pattern (Loïc Simon, PR [16905](https://github.com/python/mypy/pull/16905)) - Fix false positive from type variable within TypeGuard or TypeIs (Evgeniy Slobodkin, PR [17071](https://github.com/python/mypy/pull/17071)) - Improve `yield from` inference for unions of generators (Shantanu, PR [16717](https://github.com/python/mypy/pull/16717)) -- Support `TypeAliasType` in a class scope (Ali Hamdan, PR [17038](https://github.com/python/mypy/pull/17038)) - Fix emulating hash method logic in `attrs` classes (Hashem, PR [17016](https://github.com/python/mypy/pull/17016)) - Add reverted typeshed commit that uses `ParamSpec` for `functools.wraps` (Tamir Duberstein, PR [16942](https://github.com/python/mypy/pull/16942)) - Fix type narrowing for `types.EllipsisType` (Shantanu, PR [17003](https://github.com/python/mypy/pull/17003)) From eedb48908bace46c1a0e1a21904e31bd793ef286 Mon Sep 17 00:00:00 2001 From: Valentin Stanciu <250871+svalentin@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:13:43 +0100 Subject: [PATCH 2/3] make it clearer --- CHANGELOG.md | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 513a6e2592996..255231bc56051 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,18 +67,39 @@ This feature was contributed by Marc Mueller (PR [16878](https://github.com/pyth and PR [16925](https://github.com/python/mypy/pull/16925)). #### Support TypeAliasType (PEP 695) -As part of initial steps towards implementing [PEP 695](https://peps.python.org/pep-0695/), mypy now supports TypeAliasType. +As part of the initial steps towards implementing [PEP 695](https://peps.python.org/pep-0695/), mypy now supports `TypeAliasType`. +`TypeAliasType` provides a backport of the new `type` statement in Python 3.12. ```python -from typing import Union -from typing_extensions import TypeAliasType +type ListOrSet[T] = list[T] | set[T] +``` + +is equivalent to: + +```python +T = TypeVar("T") +ListOrSet = TypeAliasType("ListOrSet", list[T] | set[T], type_params=(T,)) +``` + +Example of use in mypy: -NewUnionType = TypeAliasType("NewUnionType", Union[int, str]) -x: NewUnionType = 42 # OK -y: NewUnionType = 'a' # OK -z: NewUnionType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]") +```python +from typing_extensions import TypeAliasType, TypeVar + +NewUnionType = TypeAliasType("NewUnionType", int | str) +x: NewUnionType = 42 +y: NewUnionType = 'a' +z: NewUnionType = object() # error: Incompatible types in assignment (expression has type "object", variable has type "int | str") [assignment] + +T = TypeVar("T") +ListOrSet = TypeAliasType("ListOrSet", list[T] | set[T], type_params=(T,)) +a: ListOrSet = [1, 2] +b: ListOrSet = {'a', 'b'} +c: ListOrSet = 'test' # error: Incompatible types in assignment (expression has type "str", variable has type "list[Any] | set[Any]") [assignment] ``` +`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`. + 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)) #### Detect Additional Unsafe Uses of super() From e330c915cb9b931d87c07d952a78353a4a010be5 Mon Sep 17 00:00:00 2001 From: Valentin Stanciu <250871+svalentin@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:21:26 +0100 Subject: [PATCH 3/3] address comment --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 255231bc56051..d0ea19866892c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,9 +93,9 @@ z: NewUnionType = object() # error: Incompatible types in assignment (expressio T = TypeVar("T") ListOrSet = TypeAliasType("ListOrSet", list[T] | set[T], type_params=(T,)) -a: ListOrSet = [1, 2] -b: ListOrSet = {'a', 'b'} -c: ListOrSet = 'test' # error: Incompatible types in assignment (expression has type "str", variable has type "list[Any] | set[Any]") [assignment] +a: ListOrSet[int] = [1, 2] +b: ListOrSet[str] = {'a', 'b'} +c: ListOrSet[str] = 'test' # error: Incompatible types in assignment (expression has type "str", variable has type "list[str] | set[str]") [assignment] ``` `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`.