~lttng/babeltrace/trunk

« back to all changes in this revision

Viewing changes to src/bindings/python/bt2/bt2/component.py

  • Committer: Simon Marchi
  • Date: 2023-06-08 19:13:01 UTC
  • Revision ID: git-v1:9dee90bdad3dac00a1caff5c9a1e58fb284ee19d
python: make all _get_ref/_put_ref proper static methods

The pyright static type checker doesn't like for these methods in
_SharedObject:

    @staticmethod
    def _get_ref(ptr):
        raise NotImplementedError

    @staticmethod
    def _put_ref(ptr):
        raise NotImplementedError

... to be overriden by assignment like this:

    _get_ref = staticmethod(native_bt.field_class_get_ref)
    _put_ref = staticmethod(native_bt.field_class_put_ref)

The warnings it gives are:

    /home/smarchi/src/babeltrace/src/bindings/python/bt2/bt2/field_class.py

      /home/smarchi/src/babeltrace/src/bindings/python/bt2/bt2/field_class.py:48:16 - error: Expression of type "staticmethod[(field_class: Unknown), Unknown]" cannot be assigned to declared type "(ptr: Unknown) -> Unknown"
        Type "staticmethod[(field_class: Unknown), Unknown]" cannot be assigned to type "(ptr: Unknown) -> Unknown"
          Parameter name mismatch: "ptr" versus "field_class" (reportGeneralTypeIssues)

      /home/smarchi/src/babeltrace/src/bindings/python/bt2/bt2/field_class.py:49:16 - error: Expression of type "staticmethod[(field_class: Unknown), Unknown]" cannot be assigned to declared type "(ptr: Unknown) -> Unknown"
        Type "staticmethod[(field_class: Unknown), Unknown]" cannot be assigned to type "(ptr: Unknown) -> Unknown"
          Parameter name mismatch: "ptr" versus "field_class" (reportGeneralTypeIssues)

So, it's just the parameter name that it doesn't like.  The obvious
solution would be to rename the `ptr` parameters of
_SharedObject._{get,put}_ref to `field_class`, in _SharedObject, to
match the names in native_bt.py.  However, I don't want us to be tied to
the names in native_bt.py (which originally come from the C header
files), as in Python we often want to use `ptr` in the name to
differentiate the SWIG wrapper around the pointer and the higher-level.
Another solution might be to use SWIG code to rename function parameters
in the SWIG-generated code, but I'm not so well-versed in SWIG, I
couldn't find how to do that.

So, I propose to just use the standard syntax for methods instead, the
type checker seems to be happy with that.

Change-Id: Ife5ad007aea4fb315d3ff4f8da67c48f4bf3e96d
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10240
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
 
59
59
 
60
60
class _SourceComponentClassConst(_ComponentClassConst):
61
 
    _get_ref = staticmethod(native_bt.component_class_source_get_ref)
62
 
    _put_ref = staticmethod(native_bt.component_class_source_put_ref)
 
61
    @staticmethod
 
62
    def _get_ref(ptr):
 
63
        native_bt.component_class_source_get_ref(ptr)
 
64
 
 
65
    @staticmethod
 
66
    def _put_ref(ptr):
 
67
        native_bt.component_class_source_put_ref(ptr)
 
68
 
63
69
    _bt_as_component_class_ptr = staticmethod(
64
70
        native_bt.component_class_source_as_component_class
65
71
    )
66
72
 
67
73
 
68
74
class _FilterComponentClassConst(_ComponentClassConst):
69
 
    _get_ref = staticmethod(native_bt.component_class_filter_get_ref)
70
 
    _put_ref = staticmethod(native_bt.component_class_filter_put_ref)
 
75
    @staticmethod
 
76
    def _get_ref(ptr):
 
77
        native_bt.component_class_filter_get_ref(ptr)
 
78
 
 
79
    @staticmethod
 
80
    def _put_ref(ptr):
 
81
        native_bt.component_class_filter_put_ref(ptr)
 
82
 
71
83
    _bt_as_component_class_ptr = staticmethod(
72
84
        native_bt.component_class_filter_as_component_class
73
85
    )
74
86
 
75
87
 
76
88
class _SinkComponentClassConst(_ComponentClassConst):
77
 
    _get_ref = staticmethod(native_bt.component_class_sink_get_ref)
78
 
    _put_ref = staticmethod(native_bt.component_class_sink_put_ref)
 
89
    @staticmethod
 
90
    def _get_ref(ptr):
 
91
        native_bt.component_class_sink_get_ref(ptr)
 
92
 
 
93
    @staticmethod
 
94
    def _put_ref(ptr):
 
95
        native_bt.component_class_sink_put_ref(ptr)
 
96
 
79
97
    _bt_as_component_class_ptr = staticmethod(
80
98
        native_bt.component_class_sink_as_component_class
81
99
    )
219
237
# This is analogous to _SourceComponentClassConst, but for source
220
238
# component objects.
221
239
class _GenericSourceComponentConst(object._SharedObject, _SourceComponentConst):
222
 
    _get_ref = staticmethod(native_bt.component_source_get_ref)
223
 
    _put_ref = staticmethod(native_bt.component_source_put_ref)
 
240
    @staticmethod
 
241
    def _get_ref(ptr):
 
242
        native_bt.component_source_get_ref(ptr)
 
243
 
 
244
    @staticmethod
 
245
    def _put_ref(ptr):
 
246
        native_bt.component_source_put_ref(ptr)
224
247
 
225
248
    @property
226
249
    def output_ports(self):
236
259
# This is analogous to _FilterComponentClassConst, but for filter
237
260
# component objects.
238
261
class _GenericFilterComponentConst(object._SharedObject, _FilterComponentConst):
239
 
    _get_ref = staticmethod(native_bt.component_filter_get_ref)
240
 
    _put_ref = staticmethod(native_bt.component_filter_put_ref)
 
262
    @staticmethod
 
263
    def _get_ref(ptr):
 
264
        native_bt.component_filter_get_ref(ptr)
 
265
 
 
266
    @staticmethod
 
267
    def _put_ref(ptr):
 
268
        native_bt.component_filter_put_ref(ptr)
241
269
 
242
270
    @property
243
271
    def output_ports(self):
263
291
# This is analogous to _SinkComponentClassConst, but for sink
264
292
# component objects.
265
293
class _GenericSinkComponentConst(object._SharedObject, _SinkComponentConst):
266
 
    _get_ref = staticmethod(native_bt.component_sink_get_ref)
267
 
    _put_ref = staticmethod(native_bt.component_sink_put_ref)
 
294
    @staticmethod
 
295
    def _get_ref(ptr):
 
296
        native_bt.component_sink_get_ref(ptr)
 
297
 
 
298
    @staticmethod
 
299
    def _put_ref(ptr):
 
300
        native_bt.component_sink_put_ref(ptr)
268
301
 
269
302
    @property
270
303
    def input_ports(self):