Skip to content

Commit

Permalink
Docs: Update TypedDict import statements (#16958)
Browse files Browse the repository at this point in the history
Since Python 3.8, `TypedDict` has been available from the `typing`
module.

As Python 3.8+ is needed to use mypy
(https://github.com/python/mypy/blob/master/setup.py#L12), then it's
best for the docs to reflect Python 3.8+ usage.

For previous versions, there's already a disclaimer on the page that
explains that `typing_extensions` must be used:
https://github.com/python/mypy/blob/master/docs/source/typed_dict.rst?plain=1#L102-L110


Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
rdimaio and AlexWaygood committed Feb 29, 2024
1 parent f19b5d3 commit 02c50bc
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ Consider this example:

.. code-block:: python
from typing_extensions import Protocol
from typing import Protocol
class P(Protocol):
x: float
Expand All @@ -561,7 +561,7 @@ the protocol definition:

.. code-block:: python
from typing_extensions import Protocol
from typing import Protocol
class P(Protocol):
@property
Expand Down
8 changes: 4 additions & 4 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ Example:

.. code-block:: python
from typing_extensions import TypedDict
from typing import TypedDict
class Point(TypedDict):
x: int
Expand All @@ -562,7 +562,7 @@ to have been validated at the point of construction. Example:

.. code-block:: python
from typing_extensions import TypedDict
from typing import TypedDict
class Point(TypedDict):
x: int
Expand Down Expand Up @@ -868,7 +868,7 @@ the return type affects which lines mypy thinks are reachable after a
``True`` may swallow exceptions. An imprecise return type can result
in mysterious errors reported near ``with`` statements.

To fix this, use either ``typing_extensions.Literal[False]`` or
To fix this, use either ``typing.Literal[False]`` or
``None`` as the return type. Returning ``None`` is equivalent to
returning ``False`` in this context, since both are treated as false
values.
Expand Down Expand Up @@ -897,7 +897,7 @@ You can use ``Literal[False]`` to fix the error:

.. code-block:: python
from typing_extensions import Literal
from typing import Literal
class MyContext:
...
Expand Down
3 changes: 1 addition & 2 deletions docs/source/generics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,7 @@ protocols mostly follow the normal rules for generic classes. 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/source/protocols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ class:

.. 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 @@ -226,8 +225,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 @@ -255,7 +253,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 @@ -298,8 +296,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 @@ -323,8 +320,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, Protocol, TypeVar
T = TypeVar('T')
Expand Down
2 changes: 1 addition & 1 deletion docs/source/stubs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ For example:

.. code-block:: python
from typing_extensions import Protocol
from typing import Protocol
class Resource(Protocol):
def ok_1(self, foo: list[str] = ...) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions docs/source/typed_dict.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dictionary value depends on the key:

.. code-block:: python
from typing_extensions import TypedDict
from typing import TypedDict
Movie = TypedDict('Movie', {'name': str, 'year': int})
Expand Down Expand Up @@ -189,7 +189,7 @@ in Python 3.6 and later:

.. code-block:: python
from typing_extensions import TypedDict
from typing import TypedDict # "from typing_extensions" in Python 3.7 and earlier
class Movie(TypedDict):
name: str
Expand Down

0 comments on commit 02c50bc

Please sign in to comment.