Skip to content

Commit 5e91317

Browse files
author
Rafal Mucha
committed
Issue-374 Extend docs
1 parent ba0caad commit 5e91317

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CONTRIBUTING.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ suite across the supported python versions.
130130
131131
# install test requirements
132132
cd py4j-python
133-
pip install -r requirements.txt
133+
pip install -r requirements-test.txt
134134
135135
# Run the full test suite
136136
nosetests

py4j-python/src/py4j/protocol.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
PYTHON_PROXY_TYPE = "f"
7272

7373
class TypeHint:
74+
"""Enables users to provide a hint to the Python to Java converter specifying the accurate data type for a given value.
75+
Essential to enforce i.e. correct number type, like Long."""
7476
def __init__(self, value, java_type):
7577
self.value = value
7678
self.java_type = java_type

py4j-web/advanced_topics.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,27 @@ Java methods slightly less efficient because in the worst case, Py4J needs to
726726
go through all registered converters for all parameters. This is why automatic
727727
conversion is disabled by default.
728728

729+
.. _explicit_conversion:
730+
731+
Explicit converting Python objects to Java primitives
732+
-----------------------------------------------------
733+
734+
Sometimes, especially when ``auto_convert=True`` it is difficult to enforce correct type
735+
passed from Python to Java. Then, ``TypeHint`` from ``py4j.protocol`` may be used.
736+
``java_type`` argument of constructor should be one of Java types defined in ``py4j.protocol``.
737+
738+
So if you have method in Java like:
739+
740+
.. code-block:: java
741+
742+
void method(HashSet<Long> longs) {}
743+
744+
Then you can pass arguments with correct type to this method with ``TypeHint``
745+
746+
::
747+
748+
>>> set_with_longs = { TypeHint(1, LONG_TYPE), TypeHint(2, LONG_TYPE) }
749+
>>> gateway.jvm.my.Class().method(set_with_longs)
729750

730751
.. _py4j_exceptions:
731752

0 commit comments

Comments
 (0)