Skip to content

[BUG] not None semantics is not supported in Pure Python mode for memory views #5612

@matusvalo

Description

@matusvalo

Describe the bug

Pure python mode does not support not None when used with memory views. See examples below.

Code to reproduce the behaviour:

import cython
import typing
import numpy as np

def process_buffer(input_view: cython.int[:,:],
                   output_view: typing.Optional[cython.int[:,:]] = None):

    if output_view is None:
        # Creating a default view, e.g.
        output_view = np.empty_like(input_view)
    
    # process 'input_view' into 'output_view'
    return output_view

process_buffer(None, None)

Note
There is additional issue that memoryviews does not support typing.Optional(). Compiling the example fails with error: typing.Optional[...] cannot be applied to non-Python type int[:]

Expected behaviour

TypeError is raised in the same way as it is raised in cython version:

import numpy as np

def process_buffer(int[:,:] input_view not None,
                   int[:,:] output_view=None):

   if output_view is None:
       # Creating a default view, e.g.
       output_view = np.empty_like(input_view)

   # process 'input_view' into 'output_view'
   return output_view


process_buffer(None, None)

Also typing.Optional[] of memoryview succeeds without error.

OS

any

Python version

3.9

Cython version

master

Additional context

Similar semantics is already present in Extension types: https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#extension-types-and-none

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions