Skip to content

Commit

Permalink
Warn about unknown memoryview types in annotations (GH-5664)
Browse files Browse the repository at this point in the history
Closes #5650
  • Loading branch information
matusvalo committed Aug 29, 2023
1 parent 4df7e67 commit df4c9a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cython/Compiler/ExprNodes.py
Expand Up @@ -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):
Expand Down
28 changes: 19 additions & 9 deletions tests/errors/pure_warnings.py
Expand Up @@ -18,23 +18,28 @@ 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
foo10: cython.bar = 1


@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 = """
Expand All @@ -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
"""

0 comments on commit df4c9a4

Please sign in to comment.