pybind / pybind/pybind11

[BUG]: user can call other functions before calling super().__init__

Open
#3,652 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Required prerequisites
Problem description

Right now we have a check that warns a user that they forgot to call super().__init__() in their overridden __init__ function. This is really useful and handles most bugs that users might accidentally make if they're not super familiar with pybind11.

However, if they don't call super().__init__ first, then other kinds of potentially undefined behavior could occur, which are also going to be difficult to diagnose from pure python. I wonder if there's a clever way that we can redirect all python method calls / attribute accesses on an object IFF that object has an __init__ to some method that tells the user to call super().__init__ first? Then after the base __init__ is called that sets the methods up to point at the correct functions?

I don't think we can add checks at any method call because the performance hit would be too high. It's possible other ways to do this would also result in performance hits.

Reproducible example code

This doesn't actually break in all cases, but I'd like this to throw an exception if possible.


from pybound_library import CppClass

class PyClass(CppClass):
    def __init__(self):
        self.cppMethod()
        super().__init__()

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

Start by tracing the existing check that warns when an overridden init omits super().init, then reproduce the issue with the provided PyClass example. Done means calls or attribute access before super().init produce a clear exception without imposing the per-call performance cost described in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.