diff --git a/dev/environment-dev.yml b/dev/environment-dev.yml index f4b576b82..0a7c7c8db 100644 --- a/dev/environment-dev.yml +++ b/dev/environment-dev.yml @@ -47,7 +47,7 @@ dependencies: - scikit-build # libmambapy dependencies - python - - pybind11<3.0.0 + - pybind11>=3.0.0 # libmambapy-stubs build dependencies - mypy # For stubgen - setuptools diff --git a/libmambapy/src/libmambapy/bindings/bind_utils.hpp b/libmambapy/src/libmambapy/bindings/bind_utils.hpp index 2f3e41b6f..a4c1a6853 100644 --- a/libmambapy/src/libmambapy/bindings/bind_utils.hpp +++ b/libmambapy/src/libmambapy/bindings/bind_utils.hpp @@ -15,10 +15,9 @@ namespace mambapy { template - auto enum_from_str(const pybind11::str& name) + auto enum_from_str(const pybind11::str& name, pybind11::handle enum_class) { - auto pyenum = pybind11::type::of(); - return pyenum.attr("__members__")[name].template cast(); + return enum_class.attr("__members__")[name].template cast(); } template diff --git a/libmambapy/src/libmambapy/bindings/legacy.cpp b/libmambapy/src/libmambapy/bindings/legacy.cpp index a7234e6cc..3d4993abb 100644 --- a/libmambapy/src/libmambapy/bindings/legacy.cpp +++ b/libmambapy/src/libmambapy/bindings/legacy.cpp @@ -1484,21 +1484,26 @@ bind_submodule_impl(pybind11::module_ m) //////////////////////////////////////////// - py::enum_(m, "QueryType") - .value("Search", QueryType::Search) - .value("Depends", QueryType::Depends) - .value("WhoNeeds", QueryType::WhoNeeds) - .def(py::init(&mambapy::enum_from_str)) - .def_static("parse", &query_type_parse); + auto query_type_enum = py::enum_(m, "QueryType") + .value("Search", QueryType::Search) + .value("Depends", QueryType::Depends) + .value("WhoNeeds", QueryType::WhoNeeds) + .def_static("parse", &query_type_parse); + query_type_enum.def(py::init([&query_type_enum](const py::str& name) + { return mambapy::enum_from_str(name, query_type_enum); } + )); py::implicitly_convertible(); - py::enum_(m, "QueryResultFormat") - .value("Json", QueryResultFormat::Json) - .value("Tree", QueryResultFormat::Tree) - .value("Table", QueryResultFormat::Table) - .value("Pretty", QueryResultFormat::Pretty) - .value("RecursiveTable", QueryResultFormat::RecursiveTable) - .def(py::init(&mambapy::enum_from_str)); + auto query_result_format_enum = py::enum_(m, "QueryResultFormat") + .value("Json", QueryResultFormat::Json) + .value("Tree", QueryResultFormat::Tree) + .value("Table", QueryResultFormat::Table) + .value("Pretty", QueryResultFormat::Pretty) + .value("RecursiveTable", QueryResultFormat::RecursiveTable); + query_result_format_enum.def(py::init( + [&query_result_format_enum](const py::str& name) + { return mambapy::enum_from_str(name, query_result_format_enum); } + )); py::implicitly_convertible(); py::class_(m, "QueryResult") diff --git a/libmambapy/src/libmambapy/bindings/solver_libsolv.cpp b/libmambapy/src/libmambapy/bindings/solver_libsolv.cpp index 39e469559..a77ef132c 100644 --- a/libmambapy/src/libmambapy/bindings/solver_libsolv.cpp +++ b/libmambapy/src/libmambapy/bindings/solver_libsolv.cpp @@ -29,17 +29,23 @@ namespace mambapy using namespace mamba; using namespace mamba::solver::libsolv; - py::enum_(m, "RepodataParser") - .value("Mamba", RepodataParser::Mamba) - .value("Libsolv", RepodataParser::Libsolv) - .def(py::init(&enum_from_str)); + auto repodata_parser_enum = py::enum_(m, "RepodataParser") + .value("Mamba", RepodataParser::Mamba) + .value("Libsolv", RepodataParser::Libsolv); + repodata_parser_enum.def( + py::init([&repodata_parser_enum](const py::str& name) + { return enum_from_str(name, repodata_parser_enum); }) + ); py::implicitly_convertible(); - py::enum_(m, "MatchSpecParser") - .value("Mixed", MatchSpecParser::Mixed) - .value("Mamba", MatchSpecParser::Mamba) - .value("Libsolv", MatchSpecParser::Libsolv) - .def(py::init(&enum_from_str)); + auto match_spec_parser_enum = py::enum_(m, "MatchSpecParser") + .value("Mixed", MatchSpecParser::Mixed) + .value("Mamba", MatchSpecParser::Mamba) + .value("Libsolv", MatchSpecParser::Libsolv); + match_spec_parser_enum.def( + py::init([&match_spec_parser_enum](const py::str& name) + { return enum_from_str(name, match_spec_parser_enum); }) + ); py::implicitly_convertible(); py::enum_(m, "PipAsPythonDependency") @@ -48,12 +54,15 @@ namespace mambapy .def(py::init([](bool val) { return static_cast(val); })); py::implicitly_convertible(); - py::enum_(m, "PackageTypes") - .value("CondaOnly", PackageTypes::CondaOnly) - .value("TarBz2Only", PackageTypes::TarBz2Only) - .value("CondaAndTarBz2", PackageTypes::CondaAndTarBz2) - .value("CondaOrElseTarBz2", PackageTypes::CondaOrElseTarBz2) - .def(py::init(&enum_from_str)); + auto package_types_enum = py::enum_(m, "PackageTypes") + .value("CondaOnly", PackageTypes::CondaOnly) + .value("TarBz2Only", PackageTypes::TarBz2Only) + .value("CondaAndTarBz2", PackageTypes::CondaAndTarBz2) + .value("CondaOrElseTarBz2", PackageTypes::CondaOrElseTarBz2); + package_types_enum.def( + py::init([&package_types_enum](const py::str& name) + { return enum_from_str(name, package_types_enum); }) + ); py::implicitly_convertible(); py::enum_(m, "VerifyPackages") @@ -62,12 +71,13 @@ namespace mambapy .def(py::init([](bool val) { return static_cast(val); })); py::implicitly_convertible(); - py::enum_(m, "LogLevel") - .value("Debug", LogLevel::Debug) - .value("Warning", LogLevel::Warning) - .value("Error", LogLevel::Error) - .value("Fatal", LogLevel::Fatal) - .def(py::init(&enum_from_str)); + auto log_level_enum = py::enum_(m, "LogLevel") + .value("Debug", LogLevel::Debug) + .value("Warning", LogLevel::Warning) + .value("Error", LogLevel::Error) + .value("Fatal", LogLevel::Fatal); + log_level_enum.def(py::init([&log_level_enum](const py::str& name) + { return enum_from_str(name, log_level_enum); })); py::implicitly_convertible(); py::class_(m, "Priorities") diff --git a/libmambapy/src/libmambapy/bindings/specs.cpp b/libmambapy/src/libmambapy/bindings/specs.cpp index 34f02101b..e71b14195 100644 --- a/libmambapy/src/libmambapy/bindings/specs.cpp +++ b/libmambapy/src/libmambapy/bindings/specs.cpp @@ -63,46 +63,51 @@ namespace mambapy [](const mamba::fs::u8path& p) { return strip_archive_extension(p); } ); - py::enum_(m, "KnownPlatform") - .value("noarch", KnownPlatform::noarch) - .value("linux_32", KnownPlatform::linux_32) - .value("linux_64", KnownPlatform::linux_64) - .value("linux_armv6l", KnownPlatform::linux_armv6l) - .value("linux_armv7l", KnownPlatform::linux_armv7l) - .value("linux_aarch64", KnownPlatform::linux_aarch64) - .value("linux_ppc64le", KnownPlatform::linux_ppc64le) - .value("linux_ppc64", KnownPlatform::linux_ppc64) - .value("linux_s390x", KnownPlatform::linux_s390x) - .value("linux_riscv32", KnownPlatform::linux_riscv32) - .value("linux_riscv64", KnownPlatform::linux_riscv64) - .value("osx_64", KnownPlatform::osx_64) - .value("osx_arm64", KnownPlatform::osx_arm64) - .value("win_32", KnownPlatform::win_32) - .value("win_64", KnownPlatform::win_64) - .value("win_arm64", KnownPlatform::win_arm64) - .value("zos_z", KnownPlatform::zos_z) - .def(py::init(&enum_from_str)) - .def_static("parse", &platform_parse) - .def_static("count", &known_platforms_count) - .def_static("build_platform", &build_platform); + auto known_platform_enum = py::enum_(m, "KnownPlatform") + .value("noarch", KnownPlatform::noarch) + .value("linux_32", KnownPlatform::linux_32) + .value("linux_64", KnownPlatform::linux_64) + .value("linux_aarch64", KnownPlatform::linux_aarch64) + .value("linux_armv6l", KnownPlatform::linux_armv6l) + .value("linux_armv7l", KnownPlatform::linux_armv7l) + .value("linux_ppc64", KnownPlatform::linux_ppc64) + .value("linux_ppc64le", KnownPlatform::linux_ppc64le) + .value("linux_s390x", KnownPlatform::linux_s390x) + .value("osx_64", KnownPlatform::osx_64) + .value("osx_arm64", KnownPlatform::osx_arm64) + .value("win_32", KnownPlatform::win_32) + .value("win_64", KnownPlatform::win_64) + .value("win_arm64", KnownPlatform::win_arm64) + .value("zos_z", KnownPlatform::zos_z) + .def_static("parse", &platform_parse) + .def_static("count", &known_platforms_count) + .def_static("build_platform", &build_platform); + known_platform_enum.def( + py::init([&known_platform_enum](const py::str& name) + { return enum_from_str(name, known_platform_enum); }) + ); py::implicitly_convertible(); - py::enum_(m, "NoArchType") - .value("No", NoArchType::No) - .value("Generic", NoArchType::Generic) - .value("Python", NoArchType::Python) - .def(py::init(&enum_from_str)) - .def_static("parse", &noarch_parse) - .def_static("count", &known_noarch_count); + auto noarch_type_enum = py::enum_(m, "NoArchType") + .value("No", NoArchType::No) + .value("Generic", NoArchType::Generic) + .value("Python", NoArchType::Python) + .def_static("parse", &noarch_parse) + .def_static("count", &known_noarch_count); + noarch_type_enum.def(py::init([&noarch_type_enum](const py::str& name) + { return enum_from_str(name, noarch_type_enum); })); py::implicitly_convertible(); auto py_conda_url = py::class_(m, "CondaURL"); - py::enum_(py_conda_url, "Credentials") - .value("Hide", CondaURL::Credentials::Hide) - .value("Show", CondaURL::Credentials::Show) - .value("Remove", CondaURL::Credentials::Remove) - .def(py::init(&enum_from_str)); + auto conda_url_credentials_enum = py::enum_(py_conda_url, "Credentials") + .value("Hide", CondaURL::Credentials::Hide) + .value("Show", CondaURL::Credentials::Show) + .value("Remove", CondaURL::Credentials::Remove); + conda_url_credentials_enum.def(py::init( + [&conda_url_credentials_enum](const py::str& name) + { return enum_from_str(name, conda_url_credentials_enum); } + )); py::implicitly_convertible(); py_conda_url // @@ -315,14 +320,20 @@ namespace mambapy auto py_unresolved_channel = py::class_(m, "UnresolvedChannel"); - py::enum_(py_unresolved_channel, "Type") - .value("URL", UnresolvedChannel::Type::URL) - .value("PackageURL", UnresolvedChannel::Type::PackageURL) - .value("Path", UnresolvedChannel::Type::Path) - .value("PackagePath", UnresolvedChannel::Type::PackagePath) - .value("Name", UnresolvedChannel::Type::Name) - .value("Unknown", UnresolvedChannel::Type::Unknown) - .def(py::init(&enum_from_str)); + auto unresolved_channel_type_enum = py::enum_( + py_unresolved_channel, + "Type" + ) + .value("URL", UnresolvedChannel::Type::URL) + .value("PackageURL", UnresolvedChannel::Type::PackageURL) + .value("Path", UnresolvedChannel::Type::Path) + .value("PackagePath", UnresolvedChannel::Type::PackagePath) + .value("Name", UnresolvedChannel::Type::Name) + .value("Unknown", UnresolvedChannel::Type::Unknown); + unresolved_channel_type_enum.def(py::init( + [&unresolved_channel_type_enum](const py::str& name) + { return enum_from_str(name, unresolved_channel_type_enum); } + )); py::implicitly_convertible(); py_unresolved_channel // @@ -436,11 +447,14 @@ namespace mambapy .def("__copy__", ©) .def("__deepcopy__", &deepcopy, py::arg("memo")); - py::enum_(py_channel, "Match") - .value("No", Channel::Match::No) - .value("InOtherPlatform", Channel::Match::InOtherPlatform) - .value("Full", Channel::Match::Full) - .def(py::init(&enum_from_str)); + auto channel_match_enum = py::enum_(py_channel, "Match") + .value("No", Channel::Match::No) + .value("InOtherPlatform", Channel::Match::InOtherPlatform) + .value("Full", Channel::Match::Full); + channel_match_enum.def( + py::init([&channel_match_enum](const py::str& name) + { return enum_from_str(name, channel_match_enum); }) + ); py::implicitly_convertible(); py_channel // diff --git a/libmambapy/src/libmambapy/bindings/utils.cpp b/libmambapy/src/libmambapy/bindings/utils.cpp index e7aa324da..0ae1fe9fa 100644 --- a/libmambapy/src/libmambapy/bindings/utils.cpp +++ b/libmambapy/src/libmambapy/bindings/utils.cpp @@ -57,36 +57,42 @@ namespace mambapy { namespace py = pybind11; - py::enum_(m, "TextEmphasis") - .value("Bold", fmt::emphasis::bold) - .value("Faint", fmt::emphasis::faint) - .value("Italic", fmt::emphasis::italic) - .value("Underline", fmt::emphasis::underline) - .value("Blink", fmt::emphasis::blink) - .value("Reverse", fmt::emphasis::reverse) - .value("Conceal", fmt::emphasis::conceal) - .value("Strikethrough", fmt::emphasis::strikethrough) - .def(py::init(&enum_from_str)); + auto text_emphasis_enum = py::enum_(m, "TextEmphasis") + .value("Bold", fmt::emphasis::bold) + .value("Faint", fmt::emphasis::faint) + .value("Italic", fmt::emphasis::italic) + .value("Underline", fmt::emphasis::underline) + .value("Blink", fmt::emphasis::blink) + .value("Reverse", fmt::emphasis::reverse) + .value("Conceal", fmt::emphasis::conceal) + .value("Strikethrough", fmt::emphasis::strikethrough); + text_emphasis_enum.def( + py::init([&text_emphasis_enum](const py::str& name) + { return enum_from_str(name, text_emphasis_enum); }) + ); py::implicitly_convertible(); - py::enum_(m, "TextTerminalColor") - .value("Black", fmt::terminal_color::black) - .value("Red", fmt::terminal_color::red) - .value("Green", fmt::terminal_color::green) - .value("Yellow", fmt::terminal_color::yellow) - .value("Blue", fmt::terminal_color::blue) - .value("Magenta", fmt::terminal_color::magenta) - .value("Cyan", fmt::terminal_color::cyan) - .value("White", fmt::terminal_color::white) - .value("BrightBlack", fmt::terminal_color::bright_black) - .value("BrightRed", fmt::terminal_color::bright_red) - .value("BrightGreen", fmt::terminal_color::bright_green) - .value("BrightYellow", fmt::terminal_color::bright_yellow) - .value("BrightBlue", fmt::terminal_color::bright_blue) - .value("BrightMagenta", fmt::terminal_color::bright_magenta) - .value("BrightCyan", fmt::terminal_color::bright_cyan) - .value("BrightWhite", fmt::terminal_color::bright_white) - .def(py::init(&enum_from_str)); + auto text_terminal_color_enum = py::enum_(m, "TextTerminalColor") + .value("Black", fmt::terminal_color::black) + .value("Red", fmt::terminal_color::red) + .value("Green", fmt::terminal_color::green) + .value("Yellow", fmt::terminal_color::yellow) + .value("Blue", fmt::terminal_color::blue) + .value("Magenta", fmt::terminal_color::magenta) + .value("Cyan", fmt::terminal_color::cyan) + .value("White", fmt::terminal_color::white) + .value("BrightBlack", fmt::terminal_color::bright_black) + .value("BrightRed", fmt::terminal_color::bright_red) + .value("BrightGreen", fmt::terminal_color::bright_green) + .value("BrightYellow", fmt::terminal_color::bright_yellow) + .value("BrightBlue", fmt::terminal_color::bright_blue) + .value("BrightMagenta", fmt::terminal_color::bright_magenta) + .value("BrightCyan", fmt::terminal_color::bright_cyan) + .value("BrightWhite", fmt::terminal_color::bright_white); + text_terminal_color_enum.def( + py::init([&text_terminal_color_enum](const py::str& name) + { return enum_from_str(name, text_terminal_color_enum); }) + ); py::implicitly_convertible(); py::class_(m, "TextRGBColor")