Skip to content

Commit

Permalink
Issue-374 Extend docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafal Mucha committed Feb 24, 2023
1 parent ba0caad commit 5e91317
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ suite across the supported python versions.
# install test requirements
cd py4j-python
pip install -r requirements.txt
pip install -r requirements-test.txt
# Run the full test suite
nosetests
Expand Down
2 changes: 2 additions & 0 deletions py4j-python/src/py4j/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
PYTHON_PROXY_TYPE = "f"

class TypeHint:
"""Enables users to provide a hint to the Python to Java converter specifying the accurate data type for a given value.
Essential to enforce i.e. correct number type, like Long."""
def __init__(self, value, java_type):
self.value = value
self.java_type = java_type
Expand Down
21 changes: 21 additions & 0 deletions py4j-web/advanced_topics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,27 @@ Java methods slightly less efficient because in the worst case, Py4J needs to
go through all registered converters for all parameters. This is why automatic
conversion is disabled by default.

.. _explicit_conversion:

Explicit converting Python objects to Java primitives
-----------------------------------------------------

Sometimes, especially when ``auto_convert=True`` it is difficult to enforce correct type
passed from Python to Java. Then, ``TypeHint`` from ``py4j.protocol`` may be used.
``java_type`` argument of constructor should be one of Java types defined in ``py4j.protocol``.

So if you have method in Java like:

.. code-block:: java
void method(HashSet<Long> longs) {}
Then you can pass arguments with correct type to this method with ``TypeHint``

::

>>> set_with_longs = { TypeHint(1, LONG_TYPE), TypeHint(2, LONG_TYPE) }
>>> gateway.jvm.my.Class().method(set_with_longs)

.. _py4j_exceptions:

Expand Down

0 comments on commit 5e91317

Please sign in to comment.