Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/guides/unreachable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ As an example, consider this simple calculator:
.. code:: python

import enum
from typing_extensions import Never
from typing import Never
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss on the issue for now

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never needs an infobox as well. (Added in Python 3.11.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I didn't add it for that one because it's already explained in the prose at the start of the page; but I've added an additional infobox below the code snippet now for consistency (and perhaps easier discoverability for people who only skim the page) 👍


def assert_never(arg: Never) -> Never:
raise AssertionError("Expected code to be unreachable")
Expand All @@ -72,6 +72,11 @@ As an example, consider this simple calculator:
case _:
assert_never(op)

.. note::

To use this feature on Python versions earlier than 3.11, you will need to
import ``Never`` from ``typing_extensions`` (version 4.1 or newer).

The ``match`` statement covers all members of the ``Op`` enum,
so the ``assert_never()`` call is unreachable and the type checker
will accept this code. However, if you add another member to the
Expand Down
17 changes: 9 additions & 8 deletions docs/reference/generics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -663,13 +662,17 @@ 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`` and ``Concatenate`` from ``typing_extensions``.

Parameter specifications also allow you to describe decorators that
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')
Expand All @@ -692,8 +695,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')
Expand Down Expand Up @@ -774,8 +776,7 @@ Example:

.. code-block:: python

from typing import TypeVar
from typing_extensions import Protocol
from typing import Protocol, TypeVar

T = TypeVar('T')

Expand Down
14 changes: 5 additions & 9 deletions docs/reference/protocols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 '...')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]: ...
Expand All @@ -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')

Expand Down
7 changes: 6 additions & 1 deletion docs/reference/quality.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ 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)

.. 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
Expand Down