Commit 0b3ccd7f authored by da-woods's avatar da-woods Committed by GitHub

Allow None to be passed into arguments annotated as type "object" (GH-4669)

parent 23eb27c6
...@@ -973,6 +973,9 @@ class CArgDeclNode(Node): ...@@ -973,6 +973,9 @@ class CArgDeclNode(Node):
error(annotation.pos, "Only Python type arguments can use typing.Optional[...]") error(annotation.pos, "Only Python type arguments can use typing.Optional[...]")
else: else:
self.or_none = True self.or_none = True
elif arg_type is py_object_type:
# exclude ": object" from the None check - None is a generic object.
self.or_none = True
elif arg_type and arg_type.is_pyobject and self.default and self.default.is_none: elif arg_type and arg_type.is_pyobject and self.default and self.default.is_none:
# "x: ... = None" => implicitly allow 'None', but warn about it. # "x: ... = None" => implicitly allow 'None', but warn about it.
if not self.or_none: if not self.or_none:
......
...@@ -205,6 +205,30 @@ def object_default(object o): # always behaves like 'or None' ...@@ -205,6 +205,30 @@ def object_default(object o): # always behaves like 'or None'
""" """
return type(o).__name__ return type(o).__name__
@cython.allow_none_for_extension_args(False)
def object_default_annotation(o : object):
"""
>>> object_default_annotation(object())
'object'
>>> object_default_annotation([])
'list'
>>> object_default_annotation(None)
'NoneType'
"""
return type(o).__name__
# no decorator
def object_default_annotation2(o : object):
"""
>>> object_default_annotation2(object())
'object'
>>> object_default_annotation2([])
'list'
>>> object_default_annotation2(None)
'NoneType'
"""
return type(o).__name__
@cython.allow_none_for_extension_args(False) @cython.allow_none_for_extension_args(False)
def object_default_none(object o=None): # behaves like 'or None' def object_default_none(object o=None): # behaves like 'or None'
""" """
......
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