diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2023-04-21 08:57:34 +0200 |
---|---|---|
committer | scoder <stefan_ml@behnel.de> | 2023-04-24 16:05:24 +0200 |
commit | eb79e600256440895561fcb8404e486c77b2842c (patch) | |
tree | 76bd498089a758f0abb835ba072818d236b9b2ad | |
parent | 1183af2c99c62af93bf6fe89bc13a5772fb4f10b (diff) | |
download | cython-eb79e600256440895561fcb8404e486c77b2842c.tar.gz |
Ignore (and warn about) simple type-tuples in annotations (like "(int, int)") and require "tuple[int, int]" instead.
Closes https://github.com/cython/cython/issues/5397
-rw-r--r-- | Cython/Compiler/ExprNodes.py | 4 | ||||
-rw-r--r-- | tests/run/annotation_typing.pyx | 24 | ||||
-rw-r--r-- | tests/run/ctuple.pyx | 11 |
3 files changed, 33 insertions, 6 deletions
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 6469000f7..242a97d6a 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -14549,6 +14549,10 @@ class AnnotationNode(ExprNode): annotation = value if explicit_pytype and explicit_ctype: warning(annotation.pos, "Duplicate type declarations found in signature annotation", level=1) + elif isinstance(annotation, TupleNode): + warning(annotation.pos, + "Tuples cannot be declared as simple tuples of types. Use 'tuple[type1, type2, ...]'.", level=1) + return [], None with env.new_c_type_context(in_c_type_context=explicit_ctype): arg_type = annotation.analyse_as_type(env) diff --git a/tests/run/annotation_typing.pyx b/tests/run/annotation_typing.pyx index f2086789d..7c49fea40 100644 --- a/tests/run/annotation_typing.pyx +++ b/tests/run/annotation_typing.pyx @@ -149,6 +149,15 @@ def return_tuple_for_carray() -> tuple: return x +def invalid_ctuple_syntax(a: (cython.int, cython.int), b: (int, int)): + """ + >>> invalid_ctuple_syntax([1, 2], [3, 4]) + [1, 2, 3, 4] + """ + result: (cython.int, cython.int, cython.int, cython.int) = a + b + return result + + MyStruct = cython.struct(x=cython.int, y=cython.int, data=cython.double) @@ -394,14 +403,17 @@ _WARNINGS = """ 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? +152:30: Tuples cannot be declared as simple tuples of types. Use 'tuple[type1, type2, ...]'. +152:59: Tuples cannot be declared as simple tuples of types. Use 'tuple[type1, type2, ...]'. +157:13: Tuples cannot be declared as simple tuples of types. Use 'tuple[type1, type2, ...]'. +292:44: Unknown type declaration in annotation, ignoring +320: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 -155:0: 'struct_io' redeclared -190:0: 'struct_convert' redeclared -209:0: 'exception_default' redeclared -240:0: 'exception_default_uint' redeclared +164:0: 'struct_io' redeclared +199:0: 'struct_convert' redeclared +218:0: 'exception_default' redeclared +249:0: 'exception_default_uint' redeclared """ diff --git a/tests/run/ctuple.pyx b/tests/run/ctuple.pyx index 4053df42b..2d92f8bad 100644 --- a/tests/run/ctuple.pyx +++ b/tests/run/ctuple.pyx @@ -225,6 +225,17 @@ def test_mul((int, int) ab, int c): """ return ab * c +def test_mul_to_ctuple((int, int) ab, int c): + """ + >>> test_mul_to_ctuple((1, 2), 2) + (1, 2, 1, 2) + >>> test_mul_to_ctuple((1, 2), 3) + Traceback (most recent call last): + TypeError: Expected a tuple of size 4, got tuple + """ + result: tuple[cython.int, cython.int, cython.int, cython.int] = ab * c + return result + def test_unop((int, int) ab): """ >>> test_unop((1, 2)) |