Skip to content

Commit e23527b

Browse files
committed
Use TypeHint annotation to indicate correct return type when sip code returns SIP_PYOBJECT
Provides correct return type hints to IDEs (and hopefully PyQGIS docs too!)
1 parent c6efa74 commit e23527b

10 files changed

+25
-16
lines changed

python/core/auto_generated/geometry/qgsabstractgeometry.sip.in

+3-3
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ Returns next vertex of the geometry (undefined behavior if hasNext() returns fal
816816
sipRes = sipCpp;
817817
%End
818818

819-
SIP_PYOBJECT __next__();
819+
SIP_PYOBJECT __next__() /TypeHint="QgsPoint"/;
820820
%MethodCode
821821
if ( sipCpp->hasNext() )
822822
sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None );
@@ -863,7 +863,7 @@ Returns next part of the geometry (undefined behavior if hasNext() returns false
863863
sipRes = sipCpp;
864864
%End
865865

866-
SIP_PYOBJECT __next__();
866+
SIP_PYOBJECT __next__() /TypeHint="QgsAbstractGeometry"/;
867867
%MethodCode
868868
if ( sipCpp->hasNext() )
869869
sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL );
@@ -911,7 +911,7 @@ Returns next part of the geometry (undefined behavior if hasNext() returns false
911911
sipRes = sipCpp;
912912
%End
913913

914-
SIP_PYOBJECT __next__();
914+
SIP_PYOBJECT __next__() /TypeHint="QgsAbstractGeometry"/;
915915
%MethodCode
916916
if ( sipCpp->hasNext() )
917917
sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL );

python/core/auto_generated/geometry/qgsgeometrycollection.sip.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Returns the number of geometries within the collection.
5858

5959

6060

61-
SIP_PYOBJECT geometryN( int n );
61+
SIP_PYOBJECT geometryN( int n ) /TypeHint="QgsAbstractGeometry"/;
6262
%Docstring
6363
Returns a geometry from within the collection.
6464

@@ -222,7 +222,7 @@ Returns a geometry without curves. Caller takes ownership
222222

223223

224224

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

python/core/auto_generated/geometry/qgslinestring.sip.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Construct a linestring from a single 2d line segment.
8282

8383

8484

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

@@ -505,7 +505,7 @@ of the curve.
505505
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
506506
%End
507507

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

python/core/auto_generated/qgsfeatureiterator.sip.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Wrapper for iterator of features from vector data provider or vector layer
206206
sipRes = sipCpp;
207207
%End
208208

209-
SIP_PYOBJECT __next__();
209+
SIP_PYOBJECT __next__() /TypeHint="QgsFeature"/;
210210
%MethodCode
211211
std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >();
212212
bool result = false;

scripts/sipify.pl

+1
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ sub fix_annotations {
393393
$line =~ s/\bSIP_TRANSFERTHIS\b/\/TransferThis\//;
394394

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

src/core/geometry/qgsabstractgeometry.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ class CORE_EXPORT QgsVertexIterator
10391039
sipRes = sipCpp;
10401040
% End
10411041

1042-
SIP_PYOBJECT __next__();
1042+
SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsPoint );
10431043
% MethodCode
10441044
if ( sipCpp->hasNext() )
10451045
sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None );
@@ -1088,7 +1088,7 @@ class CORE_EXPORT QgsGeometryPartIterator
10881088
sipRes = sipCpp;
10891089
% End
10901090

1091-
SIP_PYOBJECT __next__();
1091+
SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
10921092
% MethodCode
10931093
if ( sipCpp->hasNext() )
10941094
sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL );
@@ -1138,7 +1138,7 @@ class CORE_EXPORT QgsGeometryConstPartIterator
11381138
sipRes = sipCpp;
11391139
% End
11401140

1141-
SIP_PYOBJECT __next__();
1141+
SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
11421142
% MethodCode
11431143
if ( sipCpp->hasNext() )
11441144
sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL );

src/core/geometry/qgsgeometrycollection.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
8888
#ifndef SIP_RUN
8989
QgsAbstractGeometry *geometryN( int n );
9090
#else
91-
SIP_PYOBJECT geometryN( int n );
91+
SIP_PYOBJECT geometryN( int n ) SIP_TYPEHINT( QgsAbstractGeometry );
9292
% MethodCode
9393
if ( a0 < 0 || a0 >= sipCpp->numGeometries() )
9494
{
@@ -238,7 +238,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
238238
*
239239
* \since QGIS 3.6
240240
*/
241-
SIP_PYOBJECT __getitem__( int index );
241+
SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsAbstractGeometry );
242242
% MethodCode
243243
const int count = sipCpp->numGeometries();
244244
if ( a0 < -count || a0 >= count )

src/core/geometry/qgslinestring.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
109109
* 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
110110
* corresponds to the last point in the line.
111111
*/
112-
SIP_PYOBJECT pointN( int i ) const;
112+
SIP_PYOBJECT pointN( int i ) const SIP_TYPEHINT( QgsPoint );
113113
% MethodCode
114114
const int count = sipCpp->numPoints();
115115
if ( a0 < -count || a0 >= count )
@@ -647,7 +647,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
647647
*
648648
* \since QGIS 3.6
649649
*/
650-
SIP_PYOBJECT __getitem__( int index );
650+
SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsPoint );
651651
% MethodCode
652652
const int count = sipCpp->numPoints();
653653
if ( a0 < -count || a0 >= count )

src/core/qgis_sip.h

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@
197197
*/
198198
#define SIP_DOC_TEMPLATE
199199

200+
/*
201+
* Specifies the type of the value returned by the function as it will appear in any
202+
* generated docstrings and PEP 484 type hints. It is usually used with results of type
203+
* SIP_PYOBJECT to provide a more specific type.
204+
* Available for SIP 4.18+
205+
*/
206+
#define SIP_TYPEHINT(type)
207+
200208
/*
201209
* Sip supports the final keyword since version 4.19.0, earlier than that
202210
* we will have build issues because it tries to override final methods.

src/core/qgsfeatureiterator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class CORE_EXPORT QgsFeatureIterator
270270
sipRes = sipCpp;
271271
% End
272272

273-
SIP_PYOBJECT __next__();
273+
SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
274274
% MethodCode
275275
std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >();
276276
bool result = false;

0 commit comments

Comments
 (0)