Skip to content

Commit 3295910

Browse files
bpo-45640: [docs] Tokens are now clickable (GH-29260)
Co-authored-by: Łukasz Langa <[email protected]>
1 parent 31b3a70 commit 3295910

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
@@ -450,21 +450,20 @@ functions are described separately in section
450450
:ref:`asynchronous-generator-functions`.
451451

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

469468
.. index:: single: coroutine
470469

@@ -514,8 +513,8 @@ on the right hand side of an assignment statement.
514513
usable as simple coroutines.
515514

516515
:pep:`380` - Syntax for Delegating to a Subgenerator
517-
The proposal to introduce the :token:`yield_from` syntax, making delegation
518-
to subgenerators easy.
516+
The proposal to introduce the :token:`~python-grammar:yield_from` syntax,
517+
making delegation to subgenerators easy.
519518

520519
:pep:`525` - Asynchronous Generators
521520
The proposal that expanded on :pep:`492` by adding generator capabilities to
@@ -543,9 +542,9 @@ is already executing raises a :exc:`ValueError` exception.
543542
:meth:`~generator.__next__` method, the current yield expression always
544543
evaluates to :const:`None`. The execution then continues to the next yield
545544
expression, where the generator is suspended again, and the value of the
546-
:token:`expression_list` is returned to :meth:`__next__`'s caller. If the
547-
generator exits without yielding another value, a :exc:`StopIteration`
548-
exception is raised.
545+
:token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s
546+
caller. If the generator exits without yielding another value, a
547+
:exc:`StopIteration` exception is raised.
549548

550549
This method is normally called implicitly, e.g. by a :keyword:`for` loop, or
551550
by the built-in :func:`next` function.
@@ -634,21 +633,20 @@ An asynchronous generator object is typically used in an
634633
:keyword:`async for` statement in a coroutine function analogously to
635634
how a generator object would be used in a :keyword:`for` statement.
636635

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

653651
If an asynchronous generator happens to exit early by :keyword:`break`, the caller
654652
task being cancelled, or other exceptions, the generator's async cleanup code
@@ -700,10 +698,10 @@ which are used to control the execution of a generator function.
700698
Returns an awaitable which when run starts to execute the asynchronous
701699
generator or resumes it at the last executed yield expression. When an
702700
asynchronous generator function is resumed with an :meth:`~agen.__anext__`
703-
method, the current yield expression always evaluates to :const:`None` in
704-
the returned awaitable, which when run will continue to the next yield
705-
expression. The value of the :token:`expression_list` of the yield
706-
expression is the value of the :exc:`StopIteration` exception raised by
701+
method, the current yield expression always evaluates to :const:`None` in the
702+
returned awaitable, which when run will continue to the next yield
703+
expression. The value of the :token:`~python-grammar:expression_list` of the
704+
yield expression is the value of the :exc:`StopIteration` exception raised by
707705
the completing coroutine. If the asynchronous generator exits without
708706
yielding another value, the awaitable instead raises a
709707
:exc:`StopAsyncIteration` exception, signalling that the asynchronous
@@ -1706,8 +1704,9 @@ Assignment expressions
17061704
assignment_expression: [`identifier` ":="] `expression`
17071705

17081706
An assignment expression (sometimes also called a "named expression" or
1709-
"walrus") assigns an :token:`expression` to an :token:`identifier`, while also
1710-
returning the value of the :token:`expression`.
1707+
"walrus") assigns an :token:`~python-grammar:expression` to an
1708+
:token:`~python-grammar:identifier`, while also returning the value of the
1709+
:token:`~python-grammar:expression`.
17111710

17121711
One common use case is when handling matched regular expressions:
17131712

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)