summaryrefslogtreecommitdiff
path: root/tests/run/ext_type_none_arg.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'tests/run/ext_type_none_arg.pyx')
-rw-r--r--tests/run/ext_type_none_arg.pyx62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/run/ext_type_none_arg.pyx b/tests/run/ext_type_none_arg.pyx
index 86d70abc3..73caaa775 100644
--- a/tests/run/ext_type_none_arg.pyx
+++ b/tests/run/ext_type_none_arg.pyx
@@ -1,5 +1,10 @@
cimport cython
+try:
+ import typing
+ from typing import Optional
+except ImportError:
+ pass # Cython can still identify the use of "typing" even if the module doesn't exist
### extension types
@@ -79,6 +84,39 @@ def ext_not_none(MyExtType x not None):
"""
return attr(x)
+def ext_annotations(x: MyExtType):
+ """
+ Behaves the same as "MyExtType x not None"
+ >>> ext_annotations(MyExtType())
+ 123
+ >>> ext_annotations(None)
+ Traceback (most recent call last):
+ TypeError: Argument 'x' has incorrect type (expected ext_type_none_arg.MyExtType, got NoneType)
+ """
+ return attr(x)
+
+@cython.allow_none_for_extension_args(False)
+def ext_annotations_check_on(x: MyExtType):
+ """
+ >>> ext_annotations_check_on(MyExtType())
+ 123
+ >>> ext_annotations_check_on(None)
+ Traceback (most recent call last):
+ TypeError: Argument 'x' has incorrect type (expected ext_type_none_arg.MyExtType, got NoneType)
+ """
+ return attr(x)
+
+def ext_optional(x: typing.Optional[MyExtType], y: Optional[MyExtType]):
+ """
+ Behaves the same as "or None"
+ >>> ext_optional(MyExtType(), MyExtType())
+ 246
+ >>> ext_optional(MyExtType(), None)
+ 444
+ >>> ext_optional(None, MyExtType())
+ 444
+ """
+ return attr(x) + attr(y)
### builtin types (using list)
@@ -168,6 +206,30 @@ def object_default(object o): # always behaves like 'or None'
return type(o).__name__
@cython.allow_none_for_extension_args(False)
+def object_default_annotation(o : object):
+ """
+ >>> object_default_annotation(object())
+ 'object'
+ >>> object_default_annotation([])
+ 'list'
+ >>> object_default_annotation(None)
+ 'NoneType'
+ """
+ return type(o).__name__
+
+# no decorator
+def object_default_annotation2(o : object):
+ """
+ >>> object_default_annotation2(object())
+ 'object'
+ >>> object_default_annotation2([])
+ 'list'
+ >>> object_default_annotation2(None)
+ 'NoneType'
+ """
+ return type(o).__name__
+
+@cython.allow_none_for_extension_args(False)
def object_default_none(object o=None): # behaves like 'or None'
"""
>>> object_default_none(object())