Skip to content

[ENH] Pythonise the documentation! #4187

Description

@scoder

The current documentation stems mostly from a time when Pure Python Mode wasn't even being thought of and aims to present the special cdef syntax that Cython uses. The Pure Python Syntax is only presented on the pure Python tutorial page.

Today, more and more new users come from a Python background that uses type annotations. We should accommodate them by presenting both syntax variants hand in hand throughout the documentation.

If you want to help out, please do the following:

  1. Grab a documentation page and duplicate the code examples into separate files, inlcuding the Pure Python variant before the cdef variant with literalinclude. See
    This currently excludes the pages that refer to the integration of external C or C++ code, since some of the required language features are not available in Pure Python code/mode (and this should be mentioned in those places).

For example, a file add1.pyx:

cdef int add1(int x) except -1:
    return x + 1

should be accompanied by a file add1.py:

@cython.exceptval(-1)
def add1(x: cython.int):
    return x + 1
  1. Read the surrounding text to see if there is anything that refers to the specific syntax constructs. Make sure everything is well explained to the readers so that they can easily understand the two examples.

  2. Put a paragraph near the top of the page to show a reference to import cython and briefly explain the difference between the two syntax variants:

.. include::
    ../two-syntax-variants-used

This includes the following text:

This page uses two different syntax variants: the Cython specific ``cdef`` syntax
and static Cython type declarations in :ref:`pure Python code <pep484_type_annotations>`,
following `PEP-484 <https://www.python.org/dev/peps/pep-0484/>`_ type hints
and `PEP 526 <https://www.python.org/dev/peps/pep-0526/>`_ variable annotations.
To make good use of the latter, including C data types etc., you need
the special ``cython`` module, which you can import with

.. code-block: python

      import cython

in the Python module that you want to compile.

Don't forget to link back to this issue in any PRs that you submit.

EDIT: Decisions/approaches taken further down:

  • use different file extensions for the two variants: some_example.py and some_example.pyx
  • present both in tabs to make it easy to switch between them
  • present the Python syntax variant tab before (left of) the Cython syntax, as the first will eventually become preferable
  • keep both source files aligned line by line to highlight the differences on tab switches
  • run the .py variant also through Python for syntax/sanity checking (find a way to deal with pure Python cimports)
  • automatically add import cython to the .py test files in runtests.py to avoid having to repeat it in all examples that need it
  • explicitly write import cython in test examples that are long enough (and need it), especially if they use future imports or directive comments
  • see [ENH] Pythonise the documentation! #4187 (comment) for further conventions

List of files to consider:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions