Skip to content

Commit d328c22

Browse files
authored
Clarify "Using types...but not at runtime" docs (#15029)
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`.
1 parent a9ee618 commit d328c22

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

docs/source/runtime_troubles.rst

+8
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,18 @@ sections, these can be dealt with by using :ref:`typing.TYPE_CHECKING
277277

278278
.. code-block:: python
279279
280+
from __future__ import annotations
280281
from typing import TYPE_CHECKING
281282
if TYPE_CHECKING:
282283
from _typeshed import SupportsRichComparison
283284
285+
def f(x: SupportsRichComparison) -> None
286+
287+
The ``from __future__ import annotations`` is required to avoid
288+
a ``NameError`` when using the imported symbol.
289+
For more information and caveats, see the section on
290+
:ref:`future annotations <future-annotations>`.
291+
284292
.. _generic-builtins:
285293

286294
Using generic builtins

0 commit comments

Comments
 (0)