From 9dee90bdad3dac00a1caff5c9a1e58fb284ee19d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 8 Jun 2023 10:57:48 -0400 Subject: [PATCH] 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/10240 Reviewed-by: Philippe Proulx --- src/bindings/python/bt2/bt2/clock_class.py | 10 +++- src/bindings/python/bt2/bt2/component.py | 57 +++++++++++++++---- src/bindings/python/bt2/bt2/connection.py | 9 ++- src/bindings/python/bt2/bt2/event_class.py | 10 +++- src/bindings/python/bt2/bt2/field_class.py | 10 +++- src/bindings/python/bt2/bt2/field_path.py | 9 ++- src/bindings/python/bt2/bt2/graph.py | 9 ++- .../python/bt2/bt2/integer_range_set.py | 20 +++++-- src/bindings/python/bt2/bt2/interrupter.py | 9 ++- src/bindings/python/bt2/bt2/message.py | 9 ++- .../python/bt2/bt2/message_iterator.py | 9 ++- src/bindings/python/bt2/bt2/packet.py | 10 +++- src/bindings/python/bt2/bt2/plugin.py | 18 ++++-- src/bindings/python/bt2/bt2/query_executor.py | 9 ++- src/bindings/python/bt2/bt2/stream.py | 10 +++- src/bindings/python/bt2/bt2/stream_class.py | 20 +++++-- src/bindings/python/bt2/bt2/trace.py | 10 +++- src/bindings/python/bt2/bt2/trace_class.py | 10 +++- src/bindings/python/bt2/bt2/value.py | 10 +++- 19 files changed, 204 insertions(+), 54 deletions(-) diff --git a/src/bindings/python/bt2/bt2/clock_class.py b/src/bindings/python/bt2/bt2/clock_class.py index 4cdea396..27e05f17 100644 --- a/src/bindings/python/bt2/bt2/clock_class.py +++ b/src/bindings/python/bt2/bt2/clock_class.py @@ -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 ) diff --git a/src/bindings/python/bt2/bt2/component.py b/src/bindings/python/bt2/bt2/component.py index 7ae6fe25..7669a60d 100644 --- a/src/bindings/python/bt2/bt2/component.py +++ b/src/bindings/python/bt2/bt2/component.py @@ -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): diff --git a/src/bindings/python/bt2/bt2/connection.py b/src/bindings/python/bt2/bt2/connection.py index 0f1fce65..71066ac6 100644 --- a/src/bindings/python/bt2/bt2/connection.py +++ b/src/bindings/python/bt2/bt2/connection.py @@ -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): diff --git a/src/bindings/python/bt2/bt2/event_class.py b/src/bindings/python/bt2/bt2/event_class.py index cbbd807c..408af70b 100644 --- a/src/bindings/python/bt2/bt2/event_class.py +++ b/src/bindings/python/bt2/bt2/event_class.py @@ -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 ) diff --git a/src/bindings/python/bt2/bt2/field_class.py b/src/bindings/python/bt2/bt2/field_class.py index eed50140..2cd30c52 100644 --- a/src/bindings/python/bt2/bt2/field_class.py +++ b/src/bindings/python/bt2/bt2/field_class.py @@ -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 ) diff --git a/src/bindings/python/bt2/bt2/field_path.py b/src/bindings/python/bt2/bt2/field_path.py index a2d39629..9dbb2d8f 100644 --- a/src/bindings/python/bt2/bt2/field_path.py +++ b/src/bindings/python/bt2/bt2/field_path.py @@ -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): diff --git a/src/bindings/python/bt2/bt2/graph.py b/src/bindings/python/bt2/bt2/graph.py index ed99ef07..3b0ce036 100644 --- a/src/bindings/python/bt2/bt2/graph.py +++ b/src/bindings/python/bt2/bt2/graph.py @@ -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) diff --git a/src/bindings/python/bt2/bt2/integer_range_set.py b/src/bindings/python/bt2/bt2/integer_range_set.py index 79006f33..68031314 100644 --- a/src/bindings/python/bt2/bt2/integer_range_set.py +++ b/src/bindings/python/bt2/bt2/integer_range_set.py @@ -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 ) diff --git a/src/bindings/python/bt2/bt2/interrupter.py b/src/bindings/python/bt2/bt2/interrupter.py index 39c22883..6026097c 100644 --- a/src/bindings/python/bt2/bt2/interrupter.py +++ b/src/bindings/python/bt2/bt2/interrupter.py @@ -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() diff --git a/src/bindings/python/bt2/bt2/message.py b/src/bindings/python/bt2/bt2/message.py index 5039d923..e2d30a45 100644 --- a/src/bindings/python/bt2/bt2/message.py +++ b/src/bindings/python/bt2/bt2/message.py @@ -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): diff --git a/src/bindings/python/bt2/bt2/message_iterator.py b/src/bindings/python/bt2/bt2/message_iterator.py index e2efa732..670eaf80 100644 --- a/src/bindings/python/bt2/bt2/message_iterator.py +++ b/src/bindings/python/bt2/bt2/message_iterator.py @@ -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 = [] diff --git a/src/bindings/python/bt2/bt2/packet.py b/src/bindings/python/bt2/bt2/packet.py index 9325a7d6..097614fe 100644 --- a/src/bindings/python/bt2/bt2/packet.py +++ b/src/bindings/python/bt2/bt2/packet.py @@ -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 diff --git a/src/bindings/python/bt2/bt2/plugin.py b/src/bindings/python/bt2/bt2/plugin.py index 6888cb12..7d3fff91 100644 --- a/src/bindings/python/bt2/bt2/plugin.py +++ b/src/bindings/python/bt2/bt2/plugin.py @@ -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): diff --git a/src/bindings/python/bt2/bt2/query_executor.py b/src/bindings/python/bt2/bt2/query_executor.py index 354404a6..840e0fdd 100644 --- a/src/bindings/python/bt2/bt2/query_executor.py +++ b/src/bindings/python/bt2/bt2/query_executor.py @@ -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 diff --git a/src/bindings/python/bt2/bt2/stream.py b/src/bindings/python/bt2/bt2/stream.py index f1329623..cd456e9e 100644 --- a/src/bindings/python/bt2/bt2/stream.py +++ b/src/bindings/python/bt2/bt2/stream.py @@ -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 diff --git a/src/bindings/python/bt2/bt2/stream_class.py b/src/bindings/python/bt2/bt2/stream_class.py index 885ba746..c189a6eb 100644 --- a/src/bindings/python/bt2/bt2/stream_class.py +++ b/src/bindings/python/bt2/bt2/stream_class.py @@ -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 ) diff --git a/src/bindings/python/bt2/bt2/trace.py b/src/bindings/python/bt2/bt2/trace.py index 1c56b034..37355529 100644 --- a/src/bindings/python/bt2/bt2/trace.py +++ b/src/bindings/python/bt2/bt2/trace.py @@ -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 diff --git a/src/bindings/python/bt2/bt2/trace_class.py b/src/bindings/python/bt2/bt2/trace_class.py index f66f3855..7fa8fa45 100644 --- a/src/bindings/python/bt2/bt2/trace_class.py +++ b/src/bindings/python/bt2/bt2/trace_class.py @@ -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 ) diff --git a/src/bindings/python/bt2/bt2/value.py b/src/bindings/python/bt2/bt2/value.py index 04016dec..dcc0a2f3 100644 --- a/src/bindings/python/bt2/bt2/value.py +++ b/src/bindings/python/bt2/bt2/value.py @@ -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 -- 2.34.1