diff options
Diffstat (limited to 'tests/run/annotation_typing.pyx')
-rw-r--r-- | tests/run/annotation_typing.pyx | 95 |
1 files changed, 71 insertions, 24 deletions
diff --git a/tests/run/annotation_typing.pyx b/tests/run/annotation_typing.pyx index 03900061a..4b6b2da16 100644 --- a/tests/run/annotation_typing.pyx +++ b/tests/run/annotation_typing.pyx @@ -11,14 +11,14 @@ except ImportError: pass -def old_dict_syntax(a: list, b: "int" = 2, c: {'ctype': 'long int'} = 3, d: {'type': 'float'} = 4) -> list: +def old_dict_syntax(a: list, b: "int" = 2, c: {'ctype': 'long int'} = 3, d: {'type': 'long int'} = 4) -> list: """ >>> old_dict_syntax([1]) - ('list object', 'int', 'long', 'float') - [1, 2, 3, 4.0] + ('list object', 'Python object', 'long', 'long') + [1, 2, 3, 4] >>> old_dict_syntax([1], 3) - ('list object', 'int', 'long', 'float') - [1, 3, 3, 4.0] + ('list object', 'Python object', 'long', 'long') + [1, 3, 3, 4] >>> old_dict_syntax(123) Traceback (most recent call last): TypeError: Argument 'a' has incorrect type (expected list, got int) @@ -33,7 +33,7 @@ def old_dict_syntax(a: list, b: "int" = 2, c: {'ctype': 'long int'} = 3, d: {'ty return a -def pytypes_def(a: list, b: int = 2, c: long = 3, d: float = 4, n: list = None, o: Optional[tuple] = ()) -> list: +def pytypes_def(a: list, b: int = 2, c: long = 3, d: float = 4.0, n: list = None, o: Optional[tuple] = ()) -> list: """ >>> pytypes_def([1]) ('list object', 'Python object', 'Python object', 'double', 'list object', 'tuple object') @@ -60,7 +60,7 @@ def pytypes_def(a: list, b: int = 2, c: long = 3, d: float = 4, n: list = None, return a -cpdef pytypes_cpdef(a: list, b: int = 2, c: long = 3, d: float = 4, n: list = None, o: Optional[tuple] = ()): +cpdef pytypes_cpdef(a: list, b: int = 2, c: long = 3, d: float = 4.0, n: list = None, o: Optional[tuple] = ()): """ >>> pytypes_cpdef([1]) ('list object', 'Python object', 'Python object', 'double', 'list object', 'tuple object') @@ -87,7 +87,7 @@ cpdef pytypes_cpdef(a: list, b: int = 2, c: long = 3, d: float = 4, n: list = No return a -cdef c_pytypes_cdef(a: list, b: int = 2, c: long = 3, d: float = 4, n: list = None): +cdef c_pytypes_cdef(a: list, b: int = 2, c: long = 3, d: float = 4.0, n: list = None): print(typeof(a), typeof(b), typeof(c), typeof(d), typeof(n)) a.append(b) a.append(c) @@ -111,6 +111,15 @@ def pytypes_cdef(a, b=2, c=3, d=4): return c_pytypes_cdef(a, b, c, d) +def pyint(a: int): + """ + >>> large_int = eval('0x'+'F'*64) # definitely bigger than C int64 + >>> pyint(large_int) == large_int + True + """ + return a + + def ctypes_def(a: list, b: cython.int = 2, c: cython.long = 3, d: cython.float = 4) -> list: """ >>> ctypes_def([1]) @@ -278,24 +287,28 @@ class LateClass(object): pass -def py_float_default(price : float=None, ndigits=4): +def py_float_default(price : Optional[float]=None, ndigits=4): """ Python default arguments should prevent C type inference. >>> py_float_default() (None, 4) - >>> py_float_default(2) - (2, 4) + >>> py_float_default(None) + (None, 4) + >>> py_float_default(2) # doctest: +ELLIPSIS + Traceback (most recent call last): + TypeError: ...float... >>> py_float_default(2.0) (2.0, 4) - >>> py_float_default(2, 3) - (2, 3) + >>> py_float_default(2, 3) # doctest: +ELLIPSIS + Traceback (most recent call last): + TypeError: ...float... """ return price, ndigits cdef class ClassAttribute: - cls_attr : float = 1. + cls_attr : cython.float = 1. @cython.cfunc @@ -325,6 +338,36 @@ class HasPtr: return f"HasPtr({self.a[0]}, {self.b})" +@cython.annotation_typing(False) +def turn_off_typing(x: float, d: dict): + """ + >>> turn_off_typing('not a float', []) # ignore the typing + ('Python object', 'Python object', 'not a float', []) + """ + return typeof(x), typeof(d), x, d + + +@cython.annotation_typing(False) +cdef class ClassTurnOffTyping: + x: float + d: dict + + def get_var_types(self, arg: float): + """ + >>> ClassTurnOffTyping().get_var_types(1.0) + ('Python object', 'Python object', 'Python object') + """ + return typeof(self.x), typeof(self.d), typeof(arg) + + @cython.annotation_typing(True) + def and_turn_it_back_on_again(self, arg: float): + """ + >>> ClassTurnOffTyping().and_turn_it_back_on_again(1.0) + ('Python object', 'Python object', 'double') + """ + return typeof(self.x), typeof(self.d), typeof(arg) + + _WARNINGS = """ 14:32: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly. 14:47: Dicts should no longer be used as type annotations. Use 'cython.int' etc. directly. @@ -332,16 +375,20 @@ _WARNINGS = """ 14:77: Dicts should no longer be used as type annotations. Use 'cython.int' etc. directly. 14:85: Python type declaration in signature annotation does not refer to a Python type 14:85: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly. -36:64: PEP-484 recommends 'typing.Optional[...]' for arguments that can be None. -63:68: PEP-484 recommends 'typing.Optional[...]' for arguments that can be None. -90:68: PEP-484 recommends 'typing.Optional[...]' for arguments that can be None. -274:44: Unknown type declaration in annotation, ignoring -281:29: Ambiguous types in annotation, ignoring -298:15: Annotation ignored since class-level attributes must be Python objects. Were you trying to set up an instance attribute? +36:40: Found Python 2.x type 'long' in a Python annotation. Did you mean to use 'cython.long'? +36:66: PEP-484 recommends 'typing.Optional[...]' for arguments that can be None. +63:44: Found Python 2.x type 'long' in a Python annotation. Did you mean to use 'cython.long'? +63:70: PEP-484 recommends 'typing.Optional[...]' for arguments that can be None. +90:44: Found Python 2.x type 'long' in a Python annotation. Did you mean to use 'cython.long'? +90:70: PEP-484 recommends 'typing.Optional[...]' for arguments that can be None. +283:44: Unknown type declaration in annotation, ignoring +311:15: Annotation ignored since class-level attributes must be Python objects. Were you trying to set up an instance attribute? +# DUPLICATE: +63:44: Found Python 2.x type 'long' in a Python annotation. Did you mean to use 'cython.long'? # BUG: 63:6: 'pytypes_cpdef' redeclared -146:0: 'struct_io' redeclared -181:0: 'struct_convert' redeclared -200:0: 'exception_default' redeclared -231:0: 'exception_default_uint' redeclared +155:0: 'struct_io' redeclared +190:0: 'struct_convert' redeclared +209:0: 'exception_default' redeclared +240:0: 'exception_default_uint' redeclared """ |