Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions pep_sphinx_extensions/lexers/pep813_lexer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This file is placed in the public domain or under the
# CC0-1.0-Universal license, whichever is more permissive.

"""Custom lexer for PEP 813."""

# This lexer is most useful while PEP 813 is being discussed.
# If it breaks in the future, it can be safely replaced by plain
# Python or text lexers.

from pygments.lexer import DelegatingLexer, inherit
from pygments.lexers.python import (
PythonLexer,
PythonTracebackLexer,
_PythonConsoleLexerBase,
)
from pygments.token import Other, String


class Py813Lexer(PythonLexer):
name = "py813"

tokens = {
"expr-inside-fstring": [
# The ``!p`` conversion, without a format specifier
(r"(=\s*)?(\!p)\}", String.Interpol, "#pop"),
# The ``!p`` conversion, with a format specifier
(r"(=\s*)?(\!p):", String.Interpol, "#pop"),
inherit,
],
}


class Py813ConsoleLexer(DelegatingLexer):
name = "py813-console"

def __init__(self, **options):
pylexer = Py813Lexer
tblexer = PythonTracebackLexer

class _ReplaceInnerCode(DelegatingLexer):
def __init__(self, **options):
super().__init__(
pylexer, _PythonConsoleLexerBase, Other.Code, **options
)

super().__init__(tblexer, _ReplaceInnerCode, Other.Traceback, **options)


def register():
return [Py813Lexer, Py813ConsoleLexer]
30 changes: 10 additions & 20 deletions peps/pep-0813.rst
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,7 @@ processes the template decides what to do with it:

For example:

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> from string.templatelib import convert
>>> basses = ['precision', 'jazz', 'stingray']
Expand Down Expand Up @@ -646,8 +645,7 @@ class:

Now let's create a couple of instances and pretty print them:

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> precision = Bass(4, 'split coil P', active=False)
>>> stingray = Bass(5, 'humbucker', active=True)
Expand All @@ -662,8 +660,7 @@ Now let's create a couple of instances and pretty print them:

For more complex objects, ``!p`` can help make debugging output more readable:

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> import os
>>> print(os.pathconf_names)
Expand Down Expand Up @@ -696,8 +693,7 @@ For more complex objects, ``!p`` can help make debugging output more readable:
In f-strings, the ``!p`` conversion specifier also accepts a printer, such as a ``pprint.Printer`` with its
own options:

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> narrow = pprint.Printer(width=30)
>>> print(f'{stingray!p:{narrow}}')
Expand All @@ -715,8 +711,7 @@ own options:

A printer can also be a plain function that takes the object and returns its lines:

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> def slappa(da: Bass) -> Iterator[str]:
... yield 'All about that bass'
Expand All @@ -726,8 +721,7 @@ A printer can also be a plain function that takes the object and returns its lin

Since tuples are read according to the current section, a positional tuple value needs nothing special:

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> class Things:
... def __pprint__(self):
Expand All @@ -753,8 +747,7 @@ Objects with a ``__pprint__()`` method wrap, however deeply they are nested:
yield pprint.KWARGS
yield 'children', self.children, []

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> tree = Node('root', [Node('left', [Node('leaf')]), Node('right')])
>>> print(f'{tree!p:{pprint.Printer(width=60)}}')
Expand All @@ -781,8 +774,7 @@ A ``Layout`` changes the brackets. This class prints in the angular form that R
yield 'pitch', self.pitch
yield 'beats', self.beats, 1

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> print(f"{Note('E', beats=2)!p}")
<Note pitch='E' beats=2>
Expand All @@ -808,8 +800,7 @@ for each row:

The default printer ignores the hint, so its output still agrees with ``repr()``:

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> print(f"{Table(['Bass', 'Strings'], [['Precision', 4], ['Stingray', 5]])!p}")
Table(headers=['Bass', 'Strings'], rows=[['Precision', 4], ['Stingray', 5]])
Expand Down Expand Up @@ -840,8 +831,7 @@ value it wraps degrades gracefully when it reaches a printer that doesn't know a
yield pprint.DELEGATE
yield self.value

.. code-block:: pycon
:force:
.. code-block:: py813-console

>>> notes = [Note('E'), Emphasis(Note('A')), Note('D')]
>>> print(f'{notes!p}')
Expand Down
Loading