From 1b1da7a4d604b1fbf834c6944d2ea66fdad19a4b Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Thu, 1 May 2025 00:16:01 +0200 Subject: [PATCH 1/3] Replace typing_extensions -> typing in examples --- docs/guides/unreachable.rst | 2 +- docs/reference/generics.rst | 12 ++++-------- docs/reference/protocols.rst | 14 +++++--------- docs/reference/quality.rst | 2 +- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/docs/guides/unreachable.rst b/docs/guides/unreachable.rst index d14e3cf0..8d32d655 100644 --- a/docs/guides/unreachable.rst +++ b/docs/guides/unreachable.rst @@ -54,7 +54,7 @@ As an example, consider this simple calculator: .. code:: python import enum - from typing_extensions import Never + from typing import Never def assert_never(arg: Never) -> Never: raise AssertionError("Expected code to be unreachable") diff --git a/docs/reference/generics.rst b/docs/reference/generics.rst index 12012456..cf723e5e 100644 --- a/docs/reference/generics.rst +++ b/docs/reference/generics.rst @@ -651,8 +651,7 @@ for a more faithful type annotation: .. code-block:: python - from typing import Callable, TypeVar - from typing_extensions import ParamSpec + from typing import Callable, ParamSpec, TypeVar P = ParamSpec('P') T = TypeVar('T') @@ -668,8 +667,7 @@ alter the signature of the input function: .. code-block:: python - from typing import Callable, TypeVar - from typing_extensions import ParamSpec + from typing import Callable, ParamSpec, TypeVar P = ParamSpec('P') T = TypeVar('T') @@ -692,8 +690,7 @@ Or insert an argument: .. code-block:: python - from typing import Callable, TypeVar - from typing_extensions import Concatenate, ParamSpec + from typing import Callable, Concatenate, ParamSpec, TypeVar P = ParamSpec('P') T = TypeVar('T') @@ -774,8 +771,7 @@ Example: .. code-block:: python - from typing import TypeVar - from typing_extensions import Protocol + from typing import Protocol, TypeVar T = TypeVar('T') diff --git a/docs/reference/protocols.rst b/docs/reference/protocols.rst index 13b2e9ca..72d1a3e8 100644 --- a/docs/reference/protocols.rst +++ b/docs/reference/protocols.rst @@ -71,8 +71,7 @@ You can define your own protocol class by inheriting from the special .. code-block:: python - from typing import Iterable - from typing_extensions import Protocol + from typing import Iterable, Protocol class SupportsClose(Protocol): # Empty method body (explicit '...') @@ -231,8 +230,7 @@ such as trees and linked lists: .. code-block:: python - from typing import TypeVar, Optional - from typing_extensions import Protocol + from typing import TypeVar, Optional, Protocol class TreeLike(Protocol): value: int @@ -260,7 +258,7 @@ rudimentary support for runtime structural checks: .. code-block:: python - from typing_extensions import Protocol, runtime_checkable + from typing import Protocol, runtime_checkable @runtime_checkable class Portable(Protocol): @@ -303,8 +301,7 @@ member: .. code-block:: python - from typing import Optional, Iterable - from typing_extensions import Protocol + from typing import Optional, Iterable, Protocol class Combiner(Protocol): def __call__(self, *vals: bytes, maxlen: Optional[int] = None) -> list[bytes]: ... @@ -328,8 +325,7 @@ a double underscore prefix is used. For example: .. code-block:: python - from typing import Callable, TypeVar - from typing_extensions import Protocol + from typing import Callable, TypeVar, Protocol T = TypeVar('T') diff --git a/docs/reference/quality.rst b/docs/reference/quality.rst index 8ec08aed..2507c866 100644 --- a/docs/reference/quality.rst +++ b/docs/reference/quality.rst @@ -40,7 +40,7 @@ then the following file tests ``foo.py``: .. code-block:: python - from typing_extensions import assert_type + from typing import assert_type assert_type(bar(42), str) From d14195d7f27d433629fe070d1af7b13a02b8903d Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Thu, 1 May 2025 00:55:13 +0200 Subject: [PATCH 2/3] Add infobox re: assert_type prior to Python 3.11 --- docs/reference/quality.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/reference/quality.rst b/docs/reference/quality.rst index 2507c866..12b66536 100644 --- a/docs/reference/quality.rst +++ b/docs/reference/quality.rst @@ -44,6 +44,11 @@ then the following file tests ``foo.py``: assert_type(bar(42), str) +.. note:: + + To use this feature on Python versions earlier than 3.11, you will need to + import ``assert_type`` from ``typing_extensions`` (version 4.2 or newer). + Clever use of ``mypy --warn-unused-ignores`` can be used to check that certain expressions are or are not well-typed. The idea is to have valid expressions along with invalid expressions annotated with ``type: ignore`` comments. When From 16e7648add6c9226769f18833b640914c263b344 Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Thu, 1 May 2025 01:02:47 +0200 Subject: [PATCH 3/3] Add infobox re: ParamSpec prior to Python 3.10 --- docs/reference/generics.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/reference/generics.rst b/docs/reference/generics.rst index cf723e5e..4dc1378f 100644 --- a/docs/reference/generics.rst +++ b/docs/reference/generics.rst @@ -662,6 +662,11 @@ for a more faithful type annotation: return func(*args, **kwds) return wrapper +.. note:: + + To use this feature on Python versions earlier than 3.10, you will need to + import ``ParamSpec`` from ``typing_extensions``. + Parameter specifications also allow you to describe decorators that alter the signature of the input function: