Skip to content

Commit

Permalink
Clarify "Using types...but not at runtime" docs (#15029)
Browse files Browse the repository at this point in the history
The section "Using classes that are generic in stubs but not at runtime"
on the runtime_troubles.html page is very helpful, but naive readers who
follow its instructions will almost inevitably create a runtime
`NameError`. This PR updates the example to include an `annotations`
import that will avert such a `NameError`.
  • Loading branch information
jonshea committed Apr 11, 2023
1 parent a9ee618 commit d328c22
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/source/runtime_troubles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,18 @@ sections, these can be dealt with by using :ref:`typing.TYPE_CHECKING

.. code-block:: python
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from _typeshed import SupportsRichComparison
def f(x: SupportsRichComparison) -> None
The ``from __future__ import annotations`` is required to avoid
a ``NameError`` when using the imported symbol.
For more information and caveats, see the section on
:ref:`future annotations <future-annotations>`.
.. _generic-builtins:

Using generic builtins
Expand Down

0 comments on commit d328c22

Please sign in to comment.