Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Update TypedDict import statements #16958

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
10 changes: 5 additions & 5 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 All @@ -888,7 +888,7 @@ This produces the following output from mypy:
.. code-block:: text

example.py:3: error: "bool" is invalid as return type for "__exit__" that always returns False
example.py:3: note: Use "typing_extensions.Literal[False]" as the return type or change it to
example.py:3: note: Use "typing.Literal[False]" as the return type or change it to
Copy link
Member

Choose a reason for hiding this comment

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

We should update the error message mypy emits here, but until we do so, we shouldn't change the docs here: this copies exactly the error message mypy emits on the above snippet https://mypy-play.net/?mypy=latest&python=3.12&gist=aab6f1cc25003dd1573cb3d8b8df2396

"None"
example.py:3: note: If return type of "__exit__" implies that it may return True, the context
manager may swallow exceptions
Expand All @@ -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
2 changes: 1 addition & 1 deletion docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ Correct usage:
.. code-block:: python

# Use "mypy --enable-error-code unimported-reveal"
from typing import reveal_type # or `typing_extensions`
from typing import reveal_type # "from typing_extensions" in Python 3.10 and earlier
Copy link
Member

Choose a reason for hiding this comment

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

I think this one can be left as-is: the existing message is more concise, and isn't incorrect (you can still import it from typing_extensions on 3.11+)


x = 1
# This won't raise an error:
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
rdimaio marked this conversation as resolved.
Show resolved Hide resolved

class Movie(TypedDict):
name: str
Expand Down