Skip to content

Cython: add more Pythonic way of making type annotations #340

Done

Description

@ashwinvis

Use Pure Python versions of Cython

We can try to teach the Pure Python syntax of Cython instead of the cdef syntax.

Pure Python syntax which allows static Cython type declarations in pure Python code, following PEP-484 type hints and PEP 526 variable annotations.

https://cython.readthedocs.io/en/stable/src/userguide/language_basics.html

Typed memoryviews syntax for Numpy

Also for Numpy arrays we can type declare using data: cython.int[:,:] instead of cdef cnp.ndarray[cnp.int32_t, ndim=2] data. Yes it is not equivalent, but it is elegant and easier to teach.

Typed memoryviews allow efficient access to memory buffers, such as those underlying NumPy arrays, without incurring any Python overhead. Memoryviews are similar to the current NumPy array buffer support (np.ndarray[np.float64_t, ndim=2]), but they have more features and cleaner syntax.

https://cython.readthedocs.io/en/stable/src/userguide/memoryviews.html#memoryviews

Activity

  1. ashwinvis commented on Nov 27, 2025

    @ashwinvis
    CollaboratorAuthor
  2. self-assigned this
    on Nov 9, 2026
  3. niemilau commented on Nov 24, 2026

    @niemilau
    Contributor

    The Pure Python syntax seems considerably more restricted in what you can do with it. From their docs:

    Although it is not typically recommended over writing straight Cython code in a .pyx file, there are legitimate reasons to do this - easier testing and debugging, collaboration with pure Python developers, etc. In pure mode, you are more or less restricted to code that can be expressed (or at least emulated) in Python, plus static type declarations. Anything beyond that can only be done in .pyx files with extended language syntax, because it depends on features of the Cython compiler.

    I can see it being good for getting started with Cython though, and since this is an introductory course it could make sense to translate all current examples to it (no biggie, we only have a few). I would add some form of the above quotation as a caveat though, and recommend the .pyx approach for "real" projects. Perhaps do this for 2027?

    Memoryviews are definitely nice. I'll see if I can incorporate them in the current material already now. Incorporated now in #347

  4. ashwinvis commented on Nov 25, 2026

    @ashwinvis
    CollaboratorAuthor

    Agree with you. Unfortunately Cython is so complex that it cannot optimize itself, and these different annotations give different performances.

  5. ashwinvis commented on Nov 25, 2026

    @ashwinvis
    CollaboratorAuthor

    More or less fixed in #347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions