Skip to content

Commit

Permalink
Use TypeHint annotation to indicate correct return type when sip code…
Browse files Browse the repository at this point in the history
… returns SIP_PYOBJECT

Provides correct return type hints to IDEs (and hopefully PyQGIS docs too!)

(cherry picked from commit e23527b)
  • Loading branch information
nyalldawson committed Jan 6, 2019
1 parent 2edb434 commit 3dc7665
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 16 deletions.
Expand Up @@ -816,7 +816,7 @@ Returns next vertex of the geometry (undefined behavior if hasNext() returns fal
sipRes = sipCpp; sipRes = sipCpp;
%End %End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() /TypeHint="QgsPoint"/;
%MethodCode %MethodCode
if ( sipCpp->hasNext() ) if ( sipCpp->hasNext() )
sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None ); sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None );
Expand Down Expand Up @@ -863,7 +863,7 @@ Returns next part of the geometry (undefined behavior if hasNext() returns false
sipRes = sipCpp; sipRes = sipCpp;
%End %End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() /TypeHint="QgsAbstractGeometry"/;
%MethodCode %MethodCode
if ( sipCpp->hasNext() ) if ( sipCpp->hasNext() )
sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL ); sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL );
Expand Down Expand Up @@ -911,7 +911,7 @@ Returns next part of the geometry (undefined behavior if hasNext() returns false
sipRes = sipCpp; sipRes = sipCpp;
%End %End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() /TypeHint="QgsAbstractGeometry"/;
%MethodCode %MethodCode
if ( sipCpp->hasNext() ) if ( sipCpp->hasNext() )
sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL ); sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL );
Expand Down
Expand Up @@ -58,7 +58,7 @@ Returns the number of geometries within the collection.






SIP_PYOBJECT geometryN( int n ); SIP_PYOBJECT geometryN( int n ) /TypeHint="QgsAbstractGeometry"/;
%Docstring %Docstring
Returns a geometry from within the collection. Returns a geometry from within the collection.


Expand Down Expand Up @@ -222,7 +222,7 @@ Returns a geometry without curves. Caller takes ownership






SIP_PYOBJECT __getitem__( int index ); SIP_PYOBJECT __getitem__( int index ) /TypeHint="QgsAbstractGeometry"/;
%Docstring %Docstring
Returns the geometry at the specified ``index``. An IndexError will be raised if no geometry with the specified ``index`` exists. Returns the geometry at the specified ``index``. An IndexError will be raised if no geometry with the specified ``index`` exists.


Expand Down
4 changes: 2 additions & 2 deletions python/core/auto_generated/geometry/qgslinestring.sip.in
Expand Up @@ -82,7 +82,7 @@ Construct a linestring from a single 2d line segment.






SIP_PYOBJECT pointN( int i ) const; SIP_PYOBJECT pointN( int i ) const /TypeHint="QgsPoint"/;
%Docstring %Docstring
Returns the point at the specified index. An IndexError will be raised if no point with the specified index exists. Returns the point at the specified index. An IndexError will be raised if no point with the specified index exists.


Expand Down Expand Up @@ -508,7 +508,7 @@ of the curve.
sipRes = PyUnicode_FromString( str.toUtf8().constData() ); sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End %End


SIP_PYOBJECT __getitem__( int index ); SIP_PYOBJECT __getitem__( int index ) /TypeHint="QgsPoint"/;
%Docstring %Docstring
Returns the point at the specified ``index``. An IndexError will be raised if no point with the specified ``index`` exists. Returns the point at the specified ``index``. An IndexError will be raised if no point with the specified ``index`` exists.


Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsfeatureiterator.sip.in
Expand Up @@ -206,7 +206,7 @@ Wrapper for iterator of features from vector data provider or vector layer
sipRes = sipCpp; sipRes = sipCpp;
%End %End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() /TypeHint="QgsFeature"/;
%MethodCode %MethodCode
std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >(); std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >();
bool result = false; bool result = false;
Expand Down
1 change: 1 addition & 0 deletions scripts/sipify.pl
Expand Up @@ -393,6 +393,7 @@ sub fix_annotations {
$line =~ s/\bSIP_TRANSFERTHIS\b/\/TransferThis\//; $line =~ s/\bSIP_TRANSFERTHIS\b/\/TransferThis\//;


$line =~ s/SIP_PYNAME\(\s*(\w+)\s*\)/\/PyName=$1\//; $line =~ s/SIP_PYNAME\(\s*(\w+)\s*\)/\/PyName=$1\//;
$line =~ s/SIP_TYPEHINT\(\s*(\w+)\s*\)/\/TypeHint="$1"\//;
$line =~ s/SIP_VIRTUALERRORHANDLER\(\s*(\w+)\s*\)/\/VirtualErrorHandler=$1\//; $line =~ s/SIP_VIRTUALERRORHANDLER\(\s*(\w+)\s*\)/\/VirtualErrorHandler=$1\//;
$line =~ s/SIP_THROW\(\s*(\w+)\s*\)/throw\( $1 \)/; $line =~ s/SIP_THROW\(\s*(\w+)\s*\)/throw\( $1 \)/;


Expand Down
6 changes: 3 additions & 3 deletions src/core/geometry/qgsabstractgeometry.h
Expand Up @@ -1039,7 +1039,7 @@ class CORE_EXPORT QgsVertexIterator
sipRes = sipCpp; sipRes = sipCpp;
% End % End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsPoint );
% MethodCode % MethodCode
if ( sipCpp->hasNext() ) if ( sipCpp->hasNext() )
sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None ); sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None );
Expand Down Expand Up @@ -1088,7 +1088,7 @@ class CORE_EXPORT QgsGeometryPartIterator
sipRes = sipCpp; sipRes = sipCpp;
% End % End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
% MethodCode % MethodCode
if ( sipCpp->hasNext() ) if ( sipCpp->hasNext() )
sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL ); sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL );
Expand Down Expand Up @@ -1138,7 +1138,7 @@ class CORE_EXPORT QgsGeometryConstPartIterator
sipRes = sipCpp; sipRes = sipCpp;
% End % End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
% MethodCode % MethodCode
if ( sipCpp->hasNext() ) if ( sipCpp->hasNext() )
sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL ); sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL );
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeometrycollection.h
Expand Up @@ -88,7 +88,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
#ifndef SIP_RUN #ifndef SIP_RUN
QgsAbstractGeometry *geometryN( int n ); QgsAbstractGeometry *geometryN( int n );
#else #else
SIP_PYOBJECT geometryN( int n ); SIP_PYOBJECT geometryN( int n ) SIP_TYPEHINT( QgsAbstractGeometry );
% MethodCode % MethodCode
if ( a0 < 0 || a0 >= sipCpp->numGeometries() ) if ( a0 < 0 || a0 >= sipCpp->numGeometries() )
{ {
Expand Down Expand Up @@ -238,7 +238,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
* *
* \since QGIS 3.6 * \since QGIS 3.6
*/ */
SIP_PYOBJECT __getitem__( int index ); SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsAbstractGeometry );
% MethodCode % MethodCode
const int count = sipCpp->numGeometries(); const int count = sipCpp->numGeometries();
if ( a0 < -count || a0 >= count ) if ( a0 < -count || a0 >= count )
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgslinestring.h
Expand Up @@ -109,7 +109,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
* Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 * Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1
* corresponds to the last point in the line. * corresponds to the last point in the line.
*/ */
SIP_PYOBJECT pointN( int i ) const; SIP_PYOBJECT pointN( int i ) const SIP_TYPEHINT( QgsPoint );
% MethodCode % MethodCode
const int count = sipCpp->numPoints(); const int count = sipCpp->numPoints();
if ( a0 < -count || a0 >= count ) if ( a0 < -count || a0 >= count )
Expand Down Expand Up @@ -650,7 +650,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
* *
* \since QGIS 3.6 * \since QGIS 3.6
*/ */
SIP_PYOBJECT __getitem__( int index ); SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsPoint );
% MethodCode % MethodCode
const int count = sipCpp->numPoints(); const int count = sipCpp->numPoints();
if ( a0 < -count || a0 >= count ) if ( a0 < -count || a0 >= count )
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgis_sip.h
Expand Up @@ -197,6 +197,14 @@
*/ */
#define SIP_DOC_TEMPLATE #define SIP_DOC_TEMPLATE


/*
* Specifies the type of the value returned by the function as it will appear in any
* generated docstrings and PEP 484 type hints. It is usually used with results of type
* SIP_PYOBJECT to provide a more specific type.
* Available for SIP 4.18+
*/
#define SIP_TYPEHINT(type)

/* /*
* Sip supports the final keyword since version 4.19.0, earlier than that * Sip supports the final keyword since version 4.19.0, earlier than that
* we will have build issues because it tries to override final methods. * we will have build issues because it tries to override final methods.
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsfeatureiterator.h
Expand Up @@ -270,7 +270,7 @@ class CORE_EXPORT QgsFeatureIterator
sipRes = sipCpp; sipRes = sipCpp;
% End % End


SIP_PYOBJECT __next__(); SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
% MethodCode % MethodCode
std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >(); std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >();
bool result = false; bool result = false;
Expand Down

0 comments on commit 3dc7665

Please sign in to comment.