Commit 4225d7cc authored by Stefan Behnel's avatar Stefan Behnel

Improve the error message for "Optional[ctype]". Previously, it failed...

Improve the error message for "Optional[ctype]". Previously, it failed complaining about "or None", which wasn't the actual syntax construct on user side.

See https://github.com/cython/cython/issues/3883
parent a32e3fc4
......@@ -968,8 +968,11 @@ class CArgDeclNode(Node):
if arg_type and arg_type.python_type_constructor_name == "typing.Optional":
# "x: Optional[...]" => explicitly allow 'None'
self.or_none = True
arg_type = arg_type.resolve()
if arg_type and not arg_type.is_pyobject:
error(annotation.pos, "Only Python type arguments can use typing.Optional[...]")
else:
self.or_none = True
elif arg_type and arg_type.is_pyobject and self.default and self.default.is_none:
# "x: ... = None" => implicitly allow 'None'
self.or_none = True
......
# mode: error
import cython
try:
from typing import Optional
except ImportError:
pass
def optional_pytypes(i: Optional[int], f: Optional[float]):
pass
def optional_cython_types(i: Optional[cython.int], d: Optional[cython.double], f: Optional[cython.float]):
pass
MyStruct = cython.struct(a=cython.int, b=cython.double)
def optional_cstruct(x: Optional[MyStruct]):
pass
_ERRORS = """
15:29: Only Python type arguments can use typing.Optional[...]
15:54: Only Python type arguments can use typing.Optional[...]
15:82: Only Python type arguments can use typing.Optional[...]
21:24: Only Python type arguments can use typing.Optional[...]
# FIXME: these should be allowed!
11:24: Only Python type arguments can use typing.Optional[...]
11:42: Only Python type arguments can use typing.Optional[...]
"""
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment