python: make all _get_ref/_put_ref proper static methods
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 8 Jun 2023 14:57:48 +0000 (10:57 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 8 Jun 2023 19:13:01 +0000 (15:13 -0400)
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>
19 files changed:
src/bindings/python/bt2/bt2/clock_class.py
src/bindings/python/bt2/bt2/component.py
src/bindings/python/bt2/bt2/connection.py
src/bindings/python/bt2/bt2/event_class.py
src/bindings/python/bt2/bt2/field_class.py
src/bindings/python/bt2/bt2/field_path.py
src/bindings/python/bt2/bt2/graph.py
src/bindings/python/bt2/bt2/integer_range_set.py
src/bindings/python/bt2/bt2/interrupter.py
src/bindings/python/bt2/bt2/message.py
src/bindings/python/bt2/bt2/message_iterator.py
src/bindings/python/bt2/bt2/packet.py
src/bindings/python/bt2/bt2/plugin.py
src/bindings/python/bt2/bt2/query_executor.py
src/bindings/python/bt2/bt2/stream.py
src/bindings/python/bt2/bt2/stream_class.py
src/bindings/python/bt2/bt2/trace.py
src/bindings/python/bt2/bt2/trace_class.py
src/bindings/python/bt2/bt2/value.py

index 4cdea396be2b77939470090b2308fa5d7c47568c..27e05f17d5adc02e9f3793a7d4acf87df400b791 100644 (file)
@@ -31,8 +31,14 @@ class ClockClassOffset:
 
 
 class _ClockClassConst(object._SharedObject):
-    _get_ref = staticmethod(native_bt.clock_class_get_ref)
-    _put_ref = staticmethod(native_bt.clock_class_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.clock_class_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.clock_class_put_ref(ptr)
+
     _create_value_from_ptr_and_get_ref = staticmethod(
         bt2_value._create_from_const_ptr_and_get_ref
     )
index 7ae6fe25cee9124a017fc72485009d1a87e38254..7669a60dee72227fb4e7c0dae1e20979410c4b6e 100644 (file)
@@ -58,24 +58,42 @@ class _ComponentClassConst(object._SharedObject):
 
 
 class _SourceComponentClassConst(_ComponentClassConst):
-    _get_ref = staticmethod(native_bt.component_class_source_get_ref)
-    _put_ref = staticmethod(native_bt.component_class_source_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.component_class_source_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.component_class_source_put_ref(ptr)
+
     _bt_as_component_class_ptr = staticmethod(
         native_bt.component_class_source_as_component_class
     )
 
 
 class _FilterComponentClassConst(_ComponentClassConst):
-    _get_ref = staticmethod(native_bt.component_class_filter_get_ref)
-    _put_ref = staticmethod(native_bt.component_class_filter_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.component_class_filter_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.component_class_filter_put_ref(ptr)
+
     _bt_as_component_class_ptr = staticmethod(
         native_bt.component_class_filter_as_component_class
     )
 
 
 class _SinkComponentClassConst(_ComponentClassConst):
-    _get_ref = staticmethod(native_bt.component_class_sink_get_ref)
-    _put_ref = staticmethod(native_bt.component_class_sink_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.component_class_sink_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.component_class_sink_put_ref(ptr)
+
     _bt_as_component_class_ptr = staticmethod(
         native_bt.component_class_sink_as_component_class
     )
@@ -219,8 +237,13 @@ class _SinkComponentConst(_ComponentConst):
 # This is analogous to _SourceComponentClassConst, but for source
 # component objects.
 class _GenericSourceComponentConst(object._SharedObject, _SourceComponentConst):
-    _get_ref = staticmethod(native_bt.component_source_get_ref)
-    _put_ref = staticmethod(native_bt.component_source_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.component_source_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.component_source_put_ref(ptr)
 
     @property
     def output_ports(self):
@@ -236,8 +259,13 @@ class _GenericSourceComponentConst(object._SharedObject, _SourceComponentConst):
 # This is analogous to _FilterComponentClassConst, but for filter
 # component objects.
 class _GenericFilterComponentConst(object._SharedObject, _FilterComponentConst):
-    _get_ref = staticmethod(native_bt.component_filter_get_ref)
-    _put_ref = staticmethod(native_bt.component_filter_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.component_filter_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.component_filter_put_ref(ptr)
 
     @property
     def output_ports(self):
@@ -263,8 +291,13 @@ class _GenericFilterComponentConst(object._SharedObject, _FilterComponentConst):
 # This is analogous to _SinkComponentClassConst, but for sink
 # component objects.
 class _GenericSinkComponentConst(object._SharedObject, _SinkComponentConst):
-    _get_ref = staticmethod(native_bt.component_sink_get_ref)
-    _put_ref = staticmethod(native_bt.component_sink_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.component_sink_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.component_sink_put_ref(ptr)
 
     @property
     def input_ports(self):
index 0f1fce65c8d5c2529b9bfd066b6fb494ce1dbdf6..71066ac61e19b7443c7ff7d6c27fc976a2b53e78 100644 (file)
@@ -8,8 +8,13 @@ from bt2 import object as bt2_object
 
 
 class _ConnectionConst(bt2_object._SharedObject):
-    _get_ref = staticmethod(native_bt.connection_get_ref)
-    _put_ref = staticmethod(native_bt.connection_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.connection_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.connection_put_ref(ptr)
 
     @property
     def downstream_port(self):
index cbbd807cb4ab78941c6d854cc2709014c3b87eaf..408af70b729abdf48b6356a31c9d2513a36b3f95 100644 (file)
@@ -32,8 +32,14 @@ class EventClassLogLevel:
 
 
 class _EventClassConst(object._SharedObject):
-    _get_ref = staticmethod(native_bt.event_class_get_ref)
-    _put_ref = staticmethod(native_bt.event_class_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.event_class_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.event_class_put_ref(ptr)
+
     _borrow_stream_class_ptr = staticmethod(
         native_bt.event_class_borrow_stream_class_const
     )
index eed50140ea3edd878b7efc4205b1a9078263a0bb..2cd30c52205afbbc5a471eda8ff5e8eadf946b3c 100644 (file)
@@ -45,8 +45,14 @@ class IntegerDisplayBase:
 
 
 class _FieldClassConst(object._SharedObject):
-    _get_ref = staticmethod(native_bt.field_class_get_ref)
-    _put_ref = staticmethod(native_bt.field_class_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.field_class_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.field_class_put_ref(ptr)
+
     _borrow_user_attributes_ptr = staticmethod(
         native_bt.field_class_borrow_user_attributes_const
     )
index a2d39629bf38ac7edc9a97b2e3f01fdfd9be3cc0..9dbb2d8fc410e8624d38d5dac4aba69496fe66b7 100644 (file)
@@ -35,8 +35,13 @@ class _CurrentOptionContentFieldPathItem(_FieldPathItem):
 
 
 class _FieldPathConst(object._SharedObject, collections.abc.Iterable):
-    _get_ref = staticmethod(native_bt.field_path_get_ref)
-    _put_ref = staticmethod(native_bt.field_path_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.field_path_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.field_path_put_ref(ptr)
 
     @property
     def root_scope(self):
index ed99ef079c924c1e046770ae33ee0bffd381159b..3b0ce036984be36a374355383a41c1ad44c4c091 100644 (file)
@@ -23,8 +23,13 @@ def _graph_port_added_listener_from_native(
 
 
 class Graph(object._SharedObject):
-    _get_ref = staticmethod(native_bt.graph_get_ref)
-    _put_ref = staticmethod(native_bt.graph_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.graph_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.graph_put_ref(ptr)
 
     def __init__(self, mip_version=0):
         utils._check_uint64(mip_version)
index 79006f33c49cfe3edf0c05b2d056185c6c42453f..6803131470c06c180436d255e4491e71eb3c14aa 100644 (file)
@@ -135,8 +135,14 @@ class _IntegerRangeSet(_IntegerRangeSetConst, collections.abc.MutableSet):
 
 
 class _SignedIntegerRangeSetConst(_IntegerRangeSetConst):
-    _get_ref = staticmethod(native_bt.integer_range_set_signed_get_ref)
-    _put_ref = staticmethod(native_bt.integer_range_set_signed_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.integer_range_set_signed_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.integer_range_set_signed_put_ref(ptr)
+
     _as_range_set_ptr = staticmethod(
         native_bt.integer_range_set_signed_as_range_set_const
     )
@@ -156,8 +162,14 @@ class SignedIntegerRangeSet(_SignedIntegerRangeSetConst, _IntegerRangeSet):
 
 
 class _UnsignedIntegerRangeSetConst(_IntegerRangeSetConst):
-    _get_ref = staticmethod(native_bt.integer_range_set_unsigned_get_ref)
-    _put_ref = staticmethod(native_bt.integer_range_set_unsigned_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.integer_range_set_unsigned_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.integer_range_set_unsigned_put_ref(ptr)
+
     _as_range_set_ptr = staticmethod(
         native_bt.integer_range_set_unsigned_as_range_set_const
     )
index 39c22883d2d0304abfdc49b161e8af6efe2d142a..6026097cb78ce96b5afa32400f0f0c143f013c82 100644 (file)
@@ -7,8 +7,13 @@ import bt2
 
 
 class Interrupter(object._SharedObject):
-    _get_ref = staticmethod(native_bt.interrupter_get_ref)
-    _put_ref = staticmethod(native_bt.interrupter_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.interrupter_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.interrupter_put_ref(ptr)
 
     def __init__(self):
         ptr = native_bt.interrupter_create()
index 5039d923b59208c1b057f78256ec85083f99e96e..e2d30a452762c484249855996728a066aa73f07e 100644 (file)
@@ -15,8 +15,13 @@ def _create_from_ptr(ptr):
 
 
 class _MessageConst(object._SharedObject):
-    _get_ref = staticmethod(native_bt.message_get_ref)
-    _put_ref = staticmethod(native_bt.message_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.message_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.message_put_ref(ptr)
 
     @staticmethod
     def _check_has_default_clock_class(clock_class):
index e2efa732baba9729f2635456341d11121717b009..670eaf80eabf2af9a6c1a9d4bd580c433564e5ff 100644 (file)
@@ -19,8 +19,13 @@ class _MessageIterator(collections.abc.Iterator):
 
 
 class _UserComponentInputPortMessageIterator(object._SharedObject, _MessageIterator):
-    _get_ref = staticmethod(native_bt.message_iterator_get_ref)
-    _put_ref = staticmethod(native_bt.message_iterator_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.message_iterator_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.message_iterator_put_ref(ptr)
 
     def __init__(self, ptr):
         self._current_msgs = []
index 9325a7d6fc29cbb0168c9651761d72f1fa2d0642..097614fe444900865d19cff39a13cdbc176cb282 100644 (file)
@@ -13,8 +13,14 @@ def _bt2_stream():
 
 
 class _PacketConst(object._SharedObject):
-    _get_ref = staticmethod(native_bt.packet_get_ref)
-    _put_ref = staticmethod(native_bt.packet_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.packet_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.packet_put_ref(ptr)
+
     _borrow_stream_ptr = staticmethod(native_bt.packet_borrow_stream_const)
     _borrow_context_field_ptr = staticmethod(
         native_bt.packet_borrow_context_field_const
index 6888cb12f33b1352d56b567f941e9800b352c59f..7d3fff91403792f1d93358a36d552eba80666906 100644 (file)
@@ -91,8 +91,13 @@ def find_plugin(
 
 
 class _PluginSet(object._SharedObject, collections.abc.Sequence):
-    _put_ref = staticmethod(native_bt.plugin_set_put_ref)
-    _get_ref = staticmethod(native_bt.plugin_set_get_ref)
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.plugin_set_put_ref(ptr)
+
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.plugin_set_get_ref(ptr)
 
     def __len__(self):
         count = native_bt.plugin_set_get_plugin_count(self._ptr)
@@ -232,8 +237,13 @@ class _PluginSinkComponentClasses(_PluginComponentClasses):
 
 
 class _Plugin(object._SharedObject):
-    _put_ref = staticmethod(native_bt.plugin_put_ref)
-    _get_ref = staticmethod(native_bt.plugin_get_ref)
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.plugin_put_ref(ptr)
+
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.plugin_get_ref(ptr)
 
     @property
     def name(self):
index 354404a6e3ee199d7fe7aff4c52eff621d476b76..840e0fdd867135bb4e00c06487494bb067c7b297 100644 (file)
@@ -30,8 +30,13 @@ class _QueryExecutorCommon:
 
 
 class QueryExecutor(object._SharedObject, _QueryExecutorCommon):
-    _get_ref = staticmethod(native_bt.query_executor_get_ref)
-    _put_ref = staticmethod(native_bt.query_executor_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.query_executor_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.query_executor_put_ref(ptr)
 
     def _as_query_executor_ptr(self):
         return self._ptr
index f1329623c2633c780c5bc06f2035c70ef6f6c0cf..cd456e9e210eb984f66b8442428d55fee0af7ba9 100644 (file)
@@ -17,8 +17,14 @@ def _bt2_trace():
 
 
 class _StreamConst(bt2_object._SharedObject):
-    _get_ref = staticmethod(native_bt.stream_get_ref)
-    _put_ref = staticmethod(native_bt.stream_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.stream_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.stream_put_ref(ptr)
+
     _borrow_class_ptr = staticmethod(native_bt.stream_borrow_class_const)
     _borrow_user_attributes_ptr = staticmethod(
         native_bt.stream_borrow_user_attributes_const
index 885ba746dae01ae37b9db9ae5a0873453fe58804..c189a6eb32cf9e6864ecc582b78039a303b83161 100644 (file)
@@ -17,8 +17,14 @@ def _bt2_trace_class():
 
 
 class _StreamClassConst(object._SharedObject, collections.abc.Mapping):
-    _get_ref = staticmethod(native_bt.stream_class_get_ref)
-    _put_ref = staticmethod(native_bt.stream_class_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.stream_class_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.stream_class_put_ref(ptr)
+
     _borrow_event_class_ptr_by_id = staticmethod(
         native_bt.stream_class_borrow_event_class_by_id_const
     )
@@ -165,8 +171,14 @@ class _StreamClassConst(object._SharedObject, collections.abc.Mapping):
 
 
 class _StreamClass(_StreamClassConst):
-    _get_ref = staticmethod(native_bt.stream_class_get_ref)
-    _put_ref = staticmethod(native_bt.stream_class_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.stream_class_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.stream_class_put_ref(ptr)
+
     _borrow_event_class_ptr_by_id = staticmethod(
         native_bt.stream_class_borrow_event_class_by_id
     )
index 1c56b034c400789beade4d3aff1f1fa55bcb2678..373555298c1059d147db4ca2996e529e227cfb7e 100644 (file)
@@ -73,8 +73,14 @@ class _TraceEnvironment(_TraceEnvironmentConst, collections.abc.MutableMapping):
 
 
 class _TraceConst(object._SharedObject, collections.abc.Mapping):
-    _get_ref = staticmethod(native_bt.trace_get_ref)
-    _put_ref = staticmethod(native_bt.trace_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.trace_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.trace_put_ref(ptr)
+
     _borrow_stream_ptr_by_id = staticmethod(native_bt.trace_borrow_stream_by_id_const)
     _borrow_stream_ptr_by_index = staticmethod(
         native_bt.trace_borrow_stream_by_index_const
index f66f385527934d5348e8e83ed9aa3730396db293..7fa8fa455be1046934545276b1163282157b43bb 100644 (file)
@@ -24,8 +24,14 @@ def _trace_class_destruction_listener_from_native(
 
 
 class _TraceClassConst(object._SharedObject, collections.abc.Mapping):
-    _get_ref = staticmethod(native_bt.trace_class_get_ref)
-    _put_ref = staticmethod(native_bt.trace_class_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.trace_class_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.trace_class_put_ref(ptr)
+
     _borrow_stream_class_ptr_by_index = staticmethod(
         native_bt.trace_class_borrow_stream_class_by_index_const
     )
index 04016dec1a404d69003558861cc4a5d47a982717..dcc0a2f350c4d573c5ca87c7cd634b3da5b697ab 100644 (file)
@@ -82,8 +82,14 @@ def create_value(value):
 
 
 class _ValueConst(object._SharedObject, metaclass=abc.ABCMeta):
-    _get_ref = staticmethod(native_bt.value_get_ref)
-    _put_ref = staticmethod(native_bt.value_put_ref)
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.value_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.value_put_ref(ptr)
+
     _create_value_from_ptr = staticmethod(_create_from_const_ptr)
     _create_value_from_ptr_and_get_ref = staticmethod(
         _create_from_const_ptr_and_get_ref
This page took 0.033009 seconds and 4 git commands to generate.