SWIG 生成的 Python 函数注释不起作用
SWIG generated Python function annotations do not work
我最近开始使用 swig 来包装我的一个 C++ 库。我想在 Python 中使用这段代码,到目前为止它运行良好。问题是 Python 不知道包装函数 return 的对象是什么。当然,我仍然可以在 returned 对象上调用方法,因为我知道它的类型,但我不能那样使用智能感知。此 python 代码旨在供其他人使用,如果不知道 return 类型,这个问题会使编码变得有点困难。
这是其中一种方法:
def CreateJoinRequestMessage(self) -> "Hive::SC_Message":
return _Hive.SC_MessageHandler_CreateJoinRequestMessage(self)
它 return 是一个 SC_Message 对象。 SC_Message class 也在 SWIG 生成的 python 代码中定义如下:
class SC_Message(object):
thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag")
__repr__ = _swig_repr
def __init__(self, messageString: "std::string const &", messageType: "Hive::SC_MessageType const &"):
_Hive.SC_Message_swiginit(self, _Hive.new_SC_Message(messageString, messageType))
def GetContent(self) -> "std::string":
return _Hive.SC_Message_GetContent(self)
def GetMessageType(self) -> "Hive::SC_MessageType":
return _Hive.SC_Message_GetMessageType(self)
__swig_destroy__ = _Hive.delete_SC_Message
_Hive.SC_Message_swigregister(SC_Message)
所以我真的应该能够在使用智能感知时看到 GetContent() 方法。当我将生成的 Python 代码中的函数注释更改为 "SC_Message" 时,一切都按预期工作。这是 SWIG 的预期行为,还是 SWIG 也可以生成更有用的注释?如果没有 -py3
选项,就不会有注释。
我的 SWIG 命令如下:
swig -c++ -python -py3 hive.i
这是我的 SWIG 文件:
%module Hive
%{
#define SWIG_FILE_WITH_INIT
#include "include/hive/PieceType.hpp"
#include "include/hive/MoveType.hpp"
#include "include/hive/AxialPosition.hpp"
#include "include/hive/Color.hpp"
#include "include/hive/neighbourMap.hpp"
#include "include/hive/Piece.hpp"
#include "include/hive/Player.hpp"
#include "include/hive/PieceStack.hpp"
#include "include/hive/globals.hpp"
#include "include/hive/Move.hpp"
#include "include/hive/board.hpp"
#include "include/hive/gameState.hpp"
#include "include/communication/SC_MessageType.hpp"
#include "include/communication/SC_Message.hpp"
#include "include/communication/SC_MessageHandler.hpp"
#include "include/hive/benchmark/benchmark.hpp"
%}
%include "std_string.i"
%include "std_array.i"
%include "std_pair.i"
%include "std_vector.i"
%include "include/hive/PieceType.hpp"
%include "include/hive/MoveType.hpp"
%include "include/hive/AxialPosition.hpp"
%include "include/hive/Color.hpp"
%include "include/hive/neighbourMap.hpp"
%include "include/hive/Piece.hpp"
%include "include/hive/Player.hpp"
%include "include/hive/PieceStack.hpp"
%include "include/hive/globals.hpp"
%include "include/hive/Move.hpp"
%include "include/hive/board.hpp"
%include "include/hive/gameState.hpp"
%include "include/communication/SC_MessageType.hpp"
%include "include/communication/SC_Message.hpp"
%include "include/communication/SC_MessageHandler.hpp"
%include "include/hive/benchmark/benchmark.hpp"
提前致谢。
SWIG 似乎通常会生成 c 和 c++ 类型的注释。据我所知,这将在 2.0 版中得到解决。如需更多信息,请查看此处 https://github.com/SimpleITK/SimpleITK/issues/809
我最近开始使用 swig 来包装我的一个 C++ 库。我想在 Python 中使用这段代码,到目前为止它运行良好。问题是 Python 不知道包装函数 return 的对象是什么。当然,我仍然可以在 returned 对象上调用方法,因为我知道它的类型,但我不能那样使用智能感知。此 python 代码旨在供其他人使用,如果不知道 return 类型,这个问题会使编码变得有点困难。
这是其中一种方法:
def CreateJoinRequestMessage(self) -> "Hive::SC_Message":
return _Hive.SC_MessageHandler_CreateJoinRequestMessage(self)
它 return 是一个 SC_Message 对象。 SC_Message class 也在 SWIG 生成的 python 代码中定义如下:
class SC_Message(object):
thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag")
__repr__ = _swig_repr
def __init__(self, messageString: "std::string const &", messageType: "Hive::SC_MessageType const &"):
_Hive.SC_Message_swiginit(self, _Hive.new_SC_Message(messageString, messageType))
def GetContent(self) -> "std::string":
return _Hive.SC_Message_GetContent(self)
def GetMessageType(self) -> "Hive::SC_MessageType":
return _Hive.SC_Message_GetMessageType(self)
__swig_destroy__ = _Hive.delete_SC_Message
_Hive.SC_Message_swigregister(SC_Message)
所以我真的应该能够在使用智能感知时看到 GetContent() 方法。当我将生成的 Python 代码中的函数注释更改为 "SC_Message" 时,一切都按预期工作。这是 SWIG 的预期行为,还是 SWIG 也可以生成更有用的注释?如果没有 -py3
选项,就不会有注释。
我的 SWIG 命令如下:
swig -c++ -python -py3 hive.i
这是我的 SWIG 文件:
%module Hive
%{
#define SWIG_FILE_WITH_INIT
#include "include/hive/PieceType.hpp"
#include "include/hive/MoveType.hpp"
#include "include/hive/AxialPosition.hpp"
#include "include/hive/Color.hpp"
#include "include/hive/neighbourMap.hpp"
#include "include/hive/Piece.hpp"
#include "include/hive/Player.hpp"
#include "include/hive/PieceStack.hpp"
#include "include/hive/globals.hpp"
#include "include/hive/Move.hpp"
#include "include/hive/board.hpp"
#include "include/hive/gameState.hpp"
#include "include/communication/SC_MessageType.hpp"
#include "include/communication/SC_Message.hpp"
#include "include/communication/SC_MessageHandler.hpp"
#include "include/hive/benchmark/benchmark.hpp"
%}
%include "std_string.i"
%include "std_array.i"
%include "std_pair.i"
%include "std_vector.i"
%include "include/hive/PieceType.hpp"
%include "include/hive/MoveType.hpp"
%include "include/hive/AxialPosition.hpp"
%include "include/hive/Color.hpp"
%include "include/hive/neighbourMap.hpp"
%include "include/hive/Piece.hpp"
%include "include/hive/Player.hpp"
%include "include/hive/PieceStack.hpp"
%include "include/hive/globals.hpp"
%include "include/hive/Move.hpp"
%include "include/hive/board.hpp"
%include "include/hive/gameState.hpp"
%include "include/communication/SC_MessageType.hpp"
%include "include/communication/SC_Message.hpp"
%include "include/communication/SC_MessageHandler.hpp"
%include "include/hive/benchmark/benchmark.hpp"
提前致谢。
SWIG 似乎通常会生成 c 和 c++ 类型的注释。据我所知,这将在 2.0 版中得到解决。如需更多信息,请查看此处 https://github.com/SimpleITK/SimpleITK/issues/809