Skip to content

Fix for "Type not registered yet" error when importing from Python #4

Description

@ratiotile

It appears there are declaration order problems in the generated code. It could be related to the version of pybind11 I used (2.2.0) or compiler version (msvc2015). But these errors can be fixed with a few simple changes.

There were 3 lines which gave an error while importing the python module: ImportError: arg(): could not convert default argument into a Python object (type not registered yet?). Compile in debug mode for more information.

.def("isVisible", &BulletInterface::isVisible, py::arg("player") = (Player) nullptr)

It is referring to Player in the default paramter but it has not been registered yet. This one I fixed by naming the PlayerInterface wrapper definition and moving up above the BulletInterface:

py::class_<BWAPI::PlayerInterface, std::unique_ptr<BWAPI::PlayerInterface, py::nodelete>> player(m, "Player");
py::class_<BWAPI::BulletInterface, std::unique_ptr<BWAPI::BulletInterface, py::nodelete>>(m, "Bullet")
...
.def("isVisible", &BulletInterface::isVisible, py::arg("player") = (Player) nullptr)
...
player
    .def("getID", &PlayerInterface::getID)

The next 2 problems are in the UnitInterface wrapper. The default parameters cast nullptr to Unit, but Unit has not been registered yet.

.def("useTech", (bool (UnitInterface::*)(TechType, Unit)) &UnitInterface::useTech, py::arg("tech"), py::arg("target") = (Unit) nullptr)

.def("canUseTech", (bool (UnitInterface::*)(TechType, Unit, bool, bool, bool, bool) const) &UnitInterface::canUseTech, py::arg("tech"), py::arg("target") = (Unit) nullptr, py::arg("checkCanTargetUnit") = true, py::arg("checkTargetsType") = true, py::arg("checkCanIssueCommandType") = true, py::arg("checkCommandibility") = true)

These I fixed by naming the class again, changing the first 2 lines to:

py::class_<BWAPI::UnitInterface, std::unique_ptr<BWAPI::UnitInterface, py::nodelete>> unit(m, "Unit");
unit
    .def("getID", &UnitInterface::getID)

After making these changes, I was able to import the python module without errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions