ninia / jep

Embed Python in Java
Other
1.28k stars 145 forks source link

Declaring Type Hint Variables In Python While Still Being Able to Access Java Variable #478

Closed RevolvingMadness closed 1 year ago

RevolvingMadness commented 1 year ago

Describe the problem I wrote a simple JEP program that sets a variable that references a variable in java, so you can access it from python and I also wrote a python file that declares that variable with its type hint but it doesn't work now because when I import the type hint file, it overrides the variable that i declared in java. If that doesnt make any sense let me show you: My java code:

interpreter.set("myVariable", myJavaVariable);

My python type hint code:

myVariable: object

My python code:

from pyPythonTypeHintFile import *

x = myVariable

Once again, the type hint file overrides the declared variable in java, so I cannot use the variable declared in java.

Environment (please complete the following information):

bsteffensmeier commented 1 year ago

I am not very familiar with type hints but when I run your example it produces no errors. Here is the entire java application I tested with:

public static void main(String[] args) throws JepException {
    SharedInterpreter.setConfig(new JepConfig().addIncludePaths("."));
    try (SharedInterpreter interpreter = new SharedInterpreter()) {
        interpreter.set("myVariable", new Object());
        interpreter.exec("from pyPythonTypeHintFile import *");
        interpreter.exec("x = myVariable");
        interpreter.exec("print(x)");
    }
}

And my pyPythonTypeHintFile.py contains only one line:

myVariable: object

The output from the java application is java.lang.Object@7aec35a, which is what I would expect for a Java Object.

RevolvingMadness commented 1 year ago

Well, I guess this is just an issue on my end. Sorry about that