diff --git a/cpp/src/arrow/python/extension_type.cc b/cpp/src/arrow/python/extension_type.cc index 8c69003d298..3ccc171c871 100644 --- a/cpp/src/arrow/python/extension_type.cc +++ b/cpp/src/arrow/python/extension_type.cc @@ -16,6 +16,7 @@ // under the License. #include +#include #include #include "arrow/python/extension_type.h" @@ -71,6 +72,16 @@ PyObject* DeserializeExtInstance(PyObject* type_class, static const char* kExtensionName = "arrow.py_extension_type"; +std::string PyExtensionType::ToString() const { + PyAcquireGIL lock; + + std::stringstream ss; + OwnedRef instance(GetInstance()); + ss << "extension<" << this->extension_name() << "<" << Py_TYPE(instance.obj())->tp_name + << ">>"; + return ss.str(); +} + PyExtensionType::PyExtensionType(std::shared_ptr storage_type, PyObject* typ, PyObject* inst) : ExtensionType(storage_type), diff --git a/cpp/src/arrow/python/extension_type.h b/cpp/src/arrow/python/extension_type.h index 0041c8af6a4..5fa61336cab 100644 --- a/cpp/src/arrow/python/extension_type.h +++ b/cpp/src/arrow/python/extension_type.h @@ -33,6 +33,8 @@ class ARROW_PYTHON_EXPORT PyExtensionType : public ExtensionType { // Implement extensionType API std::string extension_name() const override { return extension_name_; } + std::string ToString() const override; + bool ExtensionEquals(const ExtensionType& other) const override; std::shared_ptr MakeArray(std::shared_ptr data) const override; diff --git a/docs/source/developers/python.rst b/docs/source/developers/python.rst index 311128ac986..d1fe086cb15 100644 --- a/docs/source/developers/python.rst +++ b/docs/source/developers/python.rst @@ -32,6 +32,11 @@ We follow a similar PEP8-like coding style to the `pandas project `_. To check style issues, use the :ref:`Archery ` subcommand ``lint``: +.. code-block:: shell + + pip install -e arrow/dev/archery + pip install -r arrow/dev/archery/requirements-lint.txt + .. code-block:: shell archery lint --python diff --git a/python/pyarrow/tests/test_extension_type.py b/python/pyarrow/tests/test_extension_type.py index 3757b8cdd82..98e169c83a6 100644 --- a/python/pyarrow/tests/test_extension_type.py +++ b/python/pyarrow/tests/test_extension_type.py @@ -95,6 +95,18 @@ def test_ext_type_basics(): assert ty.extension_name == "arrow.py_extension_type" +def test_ext_type_str(): + ty = IntegerType() + expected = "extension>" + assert str(ty) == expected + assert pa.DataType.__str__(ty) == expected + + +def test_ext_type_repr(): + ty = IntegerType() + assert repr(ty) == "IntegerType(DataType(int64))" + + def test_ext_type__lifetime(): ty = UuidType() wr = weakref.ref(ty) diff --git a/python/pyarrow/types.pxi b/python/pyarrow/types.pxi index 466e25a908d..ecfdaadff06 100644 --- a/python/pyarrow/types.pxi +++ b/python/pyarrow/types.pxi @@ -697,6 +697,10 @@ cdef class ExtensionType(BaseExtensionType): else: return NotImplemented + def __repr__(self): + fmt = '{0.__class__.__name__}({1})' + return fmt.format(self, repr(self.storage_type)) + def __arrow_ext_serialize__(self): """ Serialized representation of metadata to reconstruct the type object.