Skip to content

Commit 252d83c

Browse files
bpo-45640: [docs] Tokens are now clickable (GH-29260) (GH-29621)
Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit 3295910) Co-authored-by: Arthur Milchior <arthur@milchior.fr>
1 parent 02531f1 commit 252d83c

File tree

4 files changed

+56
-53
lines changed

4 files changed

+56
-53
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ usage patterns to be encapsulated for convenient reuse.
418418

419419
The execution of the :keyword:`with` statement with one "item" proceeds as follows:
420420

421-
#. The context expression (the expression given in the :token:`with_item`) is
422-
evaluated to obtain a context manager.
421+
#. The context expression (the expression given in the
422+
:token:`~python-grammar:with_item`) is evaluated to obtain a context manager.
423423

424424
#. The context manager's :meth:`__enter__` is loaded for later use.
425425

@@ -797,7 +797,8 @@ Syntax:
797797
capture_pattern: !'_' NAME
798798

799799
A single underscore ``_`` is not a capture pattern (this is what ``!'_'``
800-
expresses). It is instead treated as a :token:`wildcard_pattern`.
800+
expresses). It is instead treated as a
801+
:token:`~python-grammar:wildcard_pattern`.
801802

802803
In a given pattern, a given name can only be bound once. E.g.
803804
``case x, x: ...`` is invalid while ``case [x] | x: ...`` is allowed.
@@ -1182,9 +1183,9 @@ is roughly equivalent to ::
11821183
except that the original function is not temporarily bound to the name ``func``.
11831184

11841185
.. versionchanged:: 3.9
1185-
Functions may be decorated with any valid :token:`assignment_expression`.
1186-
Previously, the grammar was much more restrictive; see :pep:`614` for
1187-
details.
1186+
Functions may be decorated with any valid
1187+
:token:`~python-grammar:assignment_expression`. Previously, the grammar was
1188+
much more restrictive; see :pep:`614` for details.
11881189

11891190
.. index::
11901191
triple: default; parameter; value
@@ -1360,9 +1361,9 @@ The evaluation rules for the decorator expressions are the same as for function
13601361
decorators. The result is then bound to the class name.
13611362

13621363
.. versionchanged:: 3.9
1363-
Classes may be decorated with any valid :token:`assignment_expression`.
1364-
Previously, the grammar was much more restrictive; see :pep:`614` for
1365-
details.
1364+
Classes may be decorated with any valid
1365+
:token:`~python-grammar:assignment_expression`. Previously, the grammar was
1366+
much more restrictive; see :pep:`614` for details.
13661367

13671368
**Programmer's note:** Variables defined in the class definition are class
13681369
attributes; they are shared by instances. Instance attributes can be set in a

Doc/reference/expressions.rst

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -445,21 +445,20 @@ functions are described separately in section
445445
:ref:`asynchronous-generator-functions`.
446446

447447
When a generator function is called, it returns an iterator known as a
448-
generator. That generator then controls the execution of the generator function.
449-
The execution starts when one of the generator's methods is called. At that
450-
time, the execution proceeds to the first yield expression, where it is
451-
suspended again, returning the value of :token:`expression_list` to the generator's
452-
caller. By suspended, we mean that all local state is retained, including the
453-
current bindings of local variables, the instruction pointer, the internal
454-
evaluation stack, and the state of any exception handling. When the execution
455-
is resumed by calling one of the
456-
generator's methods, the function can proceed exactly as if the yield expression
457-
were just another external call. The value of the yield expression after
458-
resuming depends on the method which resumed the execution. If
459-
:meth:`~generator.__next__` is used (typically via either a :keyword:`for` or
460-
the :func:`next` builtin) then the result is :const:`None`. Otherwise, if
461-
:meth:`~generator.send` is used, then the result will be the value passed in to
462-
that method.
448+
generator. That generator then controls the execution of the generator
449+
function. The execution starts when one of the generator's methods is called.
450+
At that time, the execution proceeds to the first yield expression, where it is
451+
suspended again, returning the value of :token:`~python-grammar:expression_list`
452+
to the generator's caller. By suspended, we mean that all local state is
453+
retained, including the current bindings of local variables, the instruction
454+
pointer, the internal evaluation stack, and the state of any exception handling.
455+
When the execution is resumed by calling one of the generator's methods, the
456+
function can proceed exactly as if the yield expression were just another
457+
external call. The value of the yield expression after resuming depends on the
458+
method which resumed the execution. If :meth:`~generator.__next__` is used
459+
(typically via either a :keyword:`for` or the :func:`next` builtin) then the
460+
result is :const:`None`. Otherwise, if :meth:`~generator.send` is used, then
461+
the result will be the value passed in to that method.
463462

464463
.. index:: single: coroutine
465464

@@ -509,8 +508,8 @@ on the right hand side of an assignment statement.
509508
usable as simple coroutines.
510509

511510
:pep:`380` - Syntax for Delegating to a Subgenerator
512-
The proposal to introduce the :token:`yield_from` syntax, making delegation
513-
to subgenerators easy.
511+
The proposal to introduce the :token:`~python-grammar:yield_from` syntax,
512+
making delegation to subgenerators easy.
514513

515514
:pep:`525` - Asynchronous Generators
516515
The proposal that expanded on :pep:`492` by adding generator capabilities to
@@ -538,9 +537,9 @@ is already executing raises a :exc:`ValueError` exception.
538537
:meth:`~generator.__next__` method, the current yield expression always
539538
evaluates to :const:`None`. The execution then continues to the next yield
540539
expression, where the generator is suspended again, and the value of the
541-
:token:`expression_list` is returned to :meth:`__next__`'s caller. If the
542-
generator exits without yielding another value, a :exc:`StopIteration`
543-
exception is raised.
540+
:token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s
541+
caller. If the generator exits without yielding another value, a
542+
:exc:`StopIteration` exception is raised.
544543

545544
This method is normally called implicitly, e.g. by a :keyword:`for` loop, or
546545
by the built-in :func:`next` function.
@@ -629,21 +628,20 @@ An asynchronous generator object is typically used in an
629628
:keyword:`async for` statement in a coroutine function analogously to
630629
how a generator object would be used in a :keyword:`for` statement.
631630

632-
Calling one of the asynchronous generator's methods returns an
633-
:term:`awaitable` object, and the execution starts when this object
634-
is awaited on. At that time, the execution proceeds to the first yield
635-
expression, where it is suspended again, returning the value of
636-
:token:`expression_list` to the awaiting coroutine. As with a generator,
637-
suspension means that all local state is retained, including the
638-
current bindings of local variables, the instruction pointer, the internal
639-
evaluation stack, and the state of any exception handling. When the execution
640-
is resumed by awaiting on the next object returned by the asynchronous
641-
generator's methods, the function can proceed exactly as if the yield
642-
expression were just another external call. The value of the yield expression
643-
after resuming depends on the method which resumed the execution. If
631+
Calling one of the asynchronous generator's methods returns an :term:`awaitable`
632+
object, and the execution starts when this object is awaited on. At that time,
633+
the execution proceeds to the first yield expression, where it is suspended
634+
again, returning the value of :token:`~python-grammar:expression_list` to the
635+
awaiting coroutine. As with a generator, suspension means that all local state
636+
is retained, including the current bindings of local variables, the instruction
637+
pointer, the internal evaluation stack, and the state of any exception handling.
638+
When the execution is resumed by awaiting on the next object returned by the
639+
asynchronous generator's methods, the function can proceed exactly as if the
640+
yield expression were just another external call. The value of the yield
641+
expression after resuming depends on the method which resumed the execution. If
644642
:meth:`~agen.__anext__` is used then the result is :const:`None`. Otherwise, if
645-
:meth:`~agen.asend` is used, then the result will be the value passed in to
646-
that method.
643+
:meth:`~agen.asend` is used, then the result will be the value passed in to that
644+
method.
647645

648646
If an asynchronous generator happens to exit early by :keyword:`break`, the caller
649647
task being cancelled, or other exceptions, the generator's async cleanup code
@@ -695,10 +693,10 @@ which are used to control the execution of a generator function.
695693
Returns an awaitable which when run starts to execute the asynchronous
696694
generator or resumes it at the last executed yield expression. When an
697695
asynchronous generator function is resumed with an :meth:`~agen.__anext__`
698-
method, the current yield expression always evaluates to :const:`None` in
699-
the returned awaitable, which when run will continue to the next yield
700-
expression. The value of the :token:`expression_list` of the yield
701-
expression is the value of the :exc:`StopIteration` exception raised by
696+
method, the current yield expression always evaluates to :const:`None` in the
697+
returned awaitable, which when run will continue to the next yield
698+
expression. The value of the :token:`~python-grammar:expression_list` of the
699+
yield expression is the value of the :exc:`StopIteration` exception raised by
702700
the completing coroutine. If the asynchronous generator exits without
703701
yielding another value, the awaitable instead raises a
704702
:exc:`StopAsyncIteration` exception, signalling that the asynchronous
@@ -1699,8 +1697,9 @@ Assignment expressions
16991697
assignment_expression: [`identifier` ":="] `expression`
17001698

17011699
An assignment expression (sometimes also called a "named expression" or
1702-
"walrus") assigns an :token:`expression` to an :token:`identifier`, while also
1703-
returning the value of the :token:`expression`.
1700+
"walrus") assigns an :token:`~python-grammar:expression` to an
1701+
:token:`~python-grammar:identifier`, while also returning the value of the
1702+
:token:`~python-grammar:expression`.
17041703

17051704
One common use case is when handling matched regular expressions:
17061705

Doc/reference/lexical_analysis.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,10 @@ String literals are described by the following lexical definitions:
469469
bytesescapeseq: "\" <any ASCII character>
470470

471471
One syntactic restriction not indicated by these productions is that whitespace
472-
is not allowed between the :token:`stringprefix` or :token:`bytesprefix` and the
473-
rest of the literal. The source character set is defined by the encoding
474-
declaration; it is UTF-8 if no encoding declaration is given in the source file;
475-
see section :ref:`encodings`.
472+
is not allowed between the :token:`~python-grammar:stringprefix` or
473+
:token:`~python-grammar:bytesprefix` and the rest of the literal. The source
474+
character set is defined by the encoding declaration; it is UTF-8 if no encoding
475+
declaration is given in the source file; see section :ref:`encodings`.
476476

477477
.. index:: triple-quoted string, Unicode Consortium, raw string
478478
single: """; string literal
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Properly marked-up grammar tokens in the documentation are now clickable and
2+
take you to the definition of a given piece of grammar. Patch by Arthur
3+
Milchior.

0 commit comments

Comments
 (0)