-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Labels
Milestone
Description
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 supporttyping.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