oracle / oracle/graalpython

Overriding constructors for classes inheriting from Java

Open
#367 5 comments 0 reactions 1 assignee View on GitHub

@timfel is already working on this.

Since Jan 20, 2025.

Dominant language
Python
Stars
1.6k
Forks
155
Avg merge
9h 42m
Merged PRs (30d)
36

Description

As of the latest version (23.1.0), GraalPy does not support overriding constructors for Python classes that inherit from Java.

The example below throws an error when we try to create a custom Exception wrapper in Python:

package my.pack;

import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.Context;

public class Main {

    static Context context = Context.newBuilder().allowAllAccess(true).build();
    static Value clz;

    public static void main(String[] args) {
        context.eval("python", """
            import java
            class ParseException(java.lang.Exception):
                def __init__(self, message):
                    self.__super__(message)
            """);

        clz = context.getBindings("python").getMember("ParseException");
        Value obj = clz.newInstance("Hello from Java");
    }
}
Result
// error
Exception in thread "main" TypeError: ParseException.__init__() missing 1 required positional argument: 'message'
        at org.graalvm.sdk/org.graalvm.polyglot.Value.newInstance(Value.java:933)
        at my.pack.Main.main(Main.java:18)

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.