From df4c9a4c159af0ad30369501ec80392fcc8b6ab6 Mon Sep 17 00:00:00 2001 From: Matus Valo Date: Tue, 29 Aug 2023 09:00:38 +0200 Subject: [PATCH] Warn about unknown memoryview types in annotations (GH-5664) Closes https://github.com/cython/cython/issues/5650 --- Cython/Compiler/ExprNodes.py | 2 ++ tests/errors/pure_warnings.py | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index b73bb67341e..00cdb074c49 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -14514,6 +14514,8 @@ def analyse_as_type(self, env): def _warn_on_unknown_annotation(self, env, annotation): """Method checks for cases when user should be warned that annotation contains unknown types.""" + if isinstance(annotation, SliceIndexNode): + annotation = annotation.base if annotation.is_name: # Validate annotation in form `var: type` if not env.lookup(annotation.name): diff --git a/tests/errors/pure_warnings.py b/tests/errors/pure_warnings.py index e2ef7c9b2c5..086cb44f82f 100644 --- a/tests/errors/pure_warnings.py +++ b/tests/errors/pure_warnings.py @@ -18,6 +18,8 @@ def main(): foo8: (1 + x).b foo9: mod.a.b foo10: func().b + foo11: Bar[:, :, :] # warning + foo12: cython.int[:, ::1] with cython.annotation_typing(False): foo8: Bar = 1 foo9: stdint.bar = 5 @@ -25,16 +27,19 @@ def main(): @cython.cfunc -def bar() -> cython.bar: +def bar() -> cython.bar: # error pass @cython.cfunc -def bar2() -> Bar: +def bar2() -> Bar: # warning pass @cython.cfunc -def bar3() -> stdint.bar: +def bar3() -> stdint.bar: # error + pass + +def bar4(a: cython.foo[:]): # error pass _WARNINGS = """ @@ -43,21 +48,26 @@ def bar3() -> stdint.bar: 18:17: Unknown type declaration in annotation, ignoring 19:15: Unknown type declaration in annotation, ignoring 20:17: Unknown type declaration in annotation, ignoring -33:14: Unknown type declaration 'Bar' in annotation, ignoring -37:20: Unknown type declaration 'stdint.bar' in annotation, ignoring +21:14: Unknown type declaration in annotation, ignoring +35:14: Unknown type declaration 'Bar' in annotation, ignoring +39:20: Unknown type declaration 'stdint.bar' in annotation, ignoring # Spurious warnings from utility code - not part of the core test 25:10: 'cpdef_method' redeclared 36:10: 'cpdef_cname_method' redeclared 961:29: Ambiguous exception value, same as default return value: 0 +961:29: Ambiguous exception value, same as default return value: 0 1002:46: Ambiguous exception value, same as default return value: 0 +1002:46: Ambiguous exception value, same as default return value: 0 +1092:29: Ambiguous exception value, same as default return value: 0 1092:29: Ambiguous exception value, same as default return value: 0 """ _ERRORS = """ 17:16: Unknown type declaration 'cython.bar' in annotation -28:13: Not a type -28:19: Unknown type declaration 'cython.bar' in annotation -33:14: Not a type -37:14: Not a type +30:13: Not a type +30:19: Unknown type declaration 'cython.bar' in annotation +35:14: Not a type +39:14: Not a type +42:18: Unknown type declaration 'cython.foo[:]' in annotation """