Skip to content

Commit

Permalink
[pyqgis] __repr__ method for QgsExpression, QgsField (#7464)
Browse files Browse the repository at this point in the history
* [pyqgis] __repr__ method for QgsExpression, QgsField

* add tests for __repr__ methods

* Fix test
  • Loading branch information
3nids authored Jul 28, 2018
1 parent e19e1de commit c0dd006
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions python/core/auto_generated/expression/qgsexpression.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ value. The value may be null.
.. versionadded:: 3.0
%End

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsExpression: '%1'>" ).arg( sipCpp->expression() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

};


Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgsfield.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ by manual field configuration.
:return: the value
%End

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

}; // class QgsField


Expand Down
8 changes: 8 additions & 0 deletions src/core/expression/qgsexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,14 @@ class CORE_EXPORT QgsExpression
*/
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsExpression: '%1'>" ).arg( sipCpp->expression() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

private:
void initGeomCalculator();

Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ class CORE_EXPORT QgsField
*/
QgsEditorWidgetSetup editorWidgetSetup() const;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

private:

QSharedDataPointer<QgsFieldPrivate> d;
Expand Down
11 changes: 10 additions & 1 deletion tests/src/python/test_python_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

import qgis # NOQA

from PyQt5.QtCore import QVariant
from qgis.testing import unittest, start_app
from qgis.core import QgsGeometry, QgsPoint, QgsPointXY, QgsCircle, QgsCircularString, QgsCompoundCurve,\
QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsRectangle
QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsRectangle, QgsExpression, QgsField

start_app()

Expand Down Expand Up @@ -112,6 +113,14 @@ def testQgsRectangleRepr(self):
r = QgsRectangle(1, 2, 3, 4)
self.assertEqual(r.__repr__(), '<QgsRectangle: 1 2, 3 4>')

def testQgsExpressionRepr(self):
e = QgsExpression('my expression')
self.assertEqual(e.__repr__(), "<QgsExpression: 'my expression'>")

def testQgsFieldRepr(self):
f = QgsField('field_name', QVariant.Double, 'double')
self.assertEqual(f.__repr__(), "<QgsField: field_name (double)>")


if __name__ == "__main__":
unittest.main()

0 comments on commit c0dd006

Please sign in to comment.