Skip to content

Commit 1d9054c

Browse files
committed
Treat foreign executables as builtins which allows them to have __text_signature__
1 parent 15a2521 commit 1d9054c

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ language runtime. The main focus is on user-observable behavior of the engine.
55

66
## Version 25.0.0
77
* `dir(foreign_object)` now returns both foreign methods and Python methods (it used to return only foreign methods).
8+
* Support `__name__`, `__doc__`, `__text_signature__` fields on foreign executables to serve as their proper counterparts on the Python side. This is useful to, for example, use Java functional interfaces in lieu of Python functions for things like LangChain's `@tool` annotation that want to inspect the underlying function.
89

910
## Version 24.2.0
1011
* Updated developer metadata of Maven artifacts.

graalpython/com.oracle.graal.python.test.integration/src/com/oracle/graal/python/test/integration/interop/JavaInteropTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,46 @@ class Bar(Foo):
839839
assertEquals(object.getMetaSimpleName(), "object");
840840
assertFalse(object.hasMetaParents());
841841
}
842+
843+
public static class MyFunction implements java.util.function.Function<String, String> {
844+
public String __text_signature__ = "(string) -> str";
845+
846+
@Override
847+
public String apply(String s) {
848+
return s;
849+
}
850+
}
851+
852+
public static class MyFunctionWithCustomName implements java.util.function.Function<String, String> {
853+
public String __text_signature__ = "(string) -> str";
854+
public String __name__ = "myfunc";
855+
856+
@Override
857+
public String apply(String s) {
858+
return s;
859+
}
860+
}
861+
862+
public static class MyFunctionWithIncorrectSignature implements java.util.function.Function<String, String> {
863+
public String __text_signature__ = "[I;java.lang.String;";
864+
865+
@Override
866+
public String apply(String s) {
867+
return s;
868+
}
869+
}
870+
871+
@Test
872+
public void javaExecutablesAsPythonFunctions() throws IOException {
873+
Value inspectSignature = context.eval("python", "import inspect; inspect.signature");
874+
assertEquals("<Signature (string)>", inspectSignature.execute(new MyFunction()).toString());
875+
876+
Value signature = context.eval("python", "lambda f: f.__text_signature__");
877+
assertEquals(new MyFunctionWithIncorrectSignature().__text_signature__, signature.execute(new MyFunctionWithIncorrectSignature()).asString());
878+
879+
Value name = context.eval("python", "lambda f: f.__name__");
880+
assertEquals(new MyFunctionWithCustomName().__name__, name.execute(new MyFunctionWithCustomName()).asString());
881+
}
842882
}
843883

844884
@RunWith(Parameterized.class)

graalpython/lib-python/3/inspect.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,9 @@ def _signature_is_builtin(obj):
20692069
return (isbuiltin(obj) or
20702070
ismethoddescriptor(obj) or
20712071
isinstance(obj, _NonUserDefinedCallables) or
2072+
# Truffle change: treat foreign executables as builtin
2073+
(isinstance(obj, __graalpython__.ForeignType) and callable(obj)) or
2074+
# End Truffle change
20722075
# Can't test 'isinstance(type)' here, as it would
20732076
# also be True for regular python classes
20742077
obj in (type, object))

0 commit comments

Comments
 (0)