pybind / pybind/pybind11

__qualname__ for methods

Open
#2,059 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

The __qualname__ attribute for methods created using pybind11 seems to be not consistent with that created by cpython.
Testing Repo: https://github.com/m-chaturvedi/reproduce_issues/tree/master/pybind_qualname
Related to #1171 via @jagerman .
PEP: https://www.python.org/dev/peps/pep-3155/

$  /bin/cat main.py
#!/usr/bin/python3
# Tested with python 3.7

import python_module
import pybind_module
from pprint import pprint

if __name__ == "__main__":
    assert python_module.Pet.get_data.__qualname__ == "Pet.get_data"
    assert python_module.Pet.__init__.__qualname__ == "Pet.__init__"

    assert pybind_module.Pet.get_data.__qualname__ == "PyCapsule.get_data"
    assert pybind_module.Pet.__init__.__qualname__ == "PyCapsule.__init__"
$  /bin/cat main.cpp
#include <pybind11/pybind11.h>
#include <iostream>
namespace py = pybind11;

class Pet {
 public:
        Pet() {
            c = 10;
        }
        ~Pet() {}
        int get_data() {
            return c;
        }

 private:
        int c;
};


PYBIND11_MODULE(pybind_module, m) {
    using rvp = pybind11::return_value_policy;
    py::class_<Pet>(m, "Pet")
        .def(py::init<>())
        .def("get_data", &Pet::get_data);
}
$  /bin/cat python_module.py

class Pet(object):

    def __init__(self):
        pass

    def get_data(self):
        return 10
$  /bin/cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8.5)
project(test_pybind11 CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Tested with python 3.7
set(PATH_TO_PYBIND /home/chaturvedi/workspace/pybind11/build_master_ninja/install/share/cmake/pybind11)
# IMPORT pybind11
find_package(pybind11 REQUIRED CONFIG PATHS ${PATH_TO_PYBIND})

configure_file(main.py ${CMAKE_BINARY_DIR} COPYONLY)
configure_file(python_module.py ${CMAKE_BINARY_DIR} COPYONLY)
pybind11_add_module(pybind_module main.cpp)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the reproduction from the linked pybind_qualname repository with main.py, main.cpp, python_module.py, and CMakeLists.txt, then compare the reported qualname values with CPython. Read PEP 3155 and related issue #1171 first; done means methods created through pybind11 report the same qualified names as their CPython counterparts.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.