From 2eb13496fa9cc3c325c6e7ac434cae1cb9aceb5a Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Sun, 2 Jan 2022 21:45:06 -0500 Subject: [PATCH] mypy: function return types marked as optional Only methods can have optional return types, but it has to be applied to function as well as method, otherwise mypy complains. I considered making a special return type for constructors/destructors, but it seemed to just make things too messy. Time will tell if this was the right decision. --- cxxheaderparser/parser.py | 7 ++++++- cxxheaderparser/types.py | 7 +++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cxxheaderparser/parser.py b/cxxheaderparser/parser.py index f6ece2b..1ec3197 100644 --- a/cxxheaderparser/parser.py +++ b/cxxheaderparser/parser.py @@ -1749,6 +1749,7 @@ class CxxParser: return method.has_body or method.has_trailing_return else: + assert return_type is not None fn = Function( return_type, pqname, @@ -1778,8 +1779,12 @@ class CxxParser: if fn.template: raise CxxParseError("typedef function may not have a template") + return_type = fn.return_type + if return_type is None: + raise CxxParseError("typedef function must have return type") + fntype = FunctionType( - fn.return_type, + return_type, fn.parameters, fn.vararg, fn.has_trailing_return, diff --git a/cxxheaderparser/types.py b/cxxheaderparser/types.py index 8354427..7973b69 100644 --- a/cxxheaderparser/types.py +++ b/cxxheaderparser/types.py @@ -468,7 +468,9 @@ class Function: A function declaration, potentially with the function body """ - return_type: DecoratedType + #: Only constructors and destructors don't have a return type + return_type: typing.Optional[DecoratedType] + name: PQName parameters: typing.List[Parameter] @@ -510,9 +512,6 @@ class Method(Function): A method declaration, potentially with the method body """ - #: constructors and destructors don't have a return type - return_type: typing.Optional[DecoratedType] - access: str = "" const: bool = False