bt2: replace internal property setters with standard methods
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 12 Jun 2023 20:29:52 +0000 (16:29 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 4 Nov 2024 16:17:44 +0000 (11:17 -0500)
Pyright doesn't like this pattern:

    def _user_attributes(self, user_attributes):
        value = bt2_value.create_value(user_attributes)
        native_bt.field_class_variant_option_set_user_attributes(
            self._ptr, utils._check_type(value, bt2_value.MapValue)._ptr
        )

    _user_attributes = property(fset=_user_attributes)

The warning is:

    /home/smarchi/src/babeltrace/src/bindings/python/bt2/bt2/field_class.py
      /home/smarchi/src/babeltrace/src/bindings/python/bt2/bt2/field_class.py:102:24 - error: Expression of type "property" cannot be assigned to declared type "(self: Self@_FieldClass, user_attributes: Unknown) -> None"
        Type "property" cannot be assigned to type "(self: Self@_FieldClass, user_attributes: Unknown) -> None" (reportGeneralTypeIssues)

What happens is that we first define a method with the
"_user_attributes", and at some later point it in the initialization,
the "_user_attributes" name in the class gets assigned a property whose
setter is the original method.  The type checker doesn't like that the
type of "_user_attributes" changes like that.

Change all instances of this pattern to just use a "_set_*" method.  I
don't think it's important for these internal APIs to be properties, in
fact I think it just makes the code hard to grep.

Change-Id: Iea3cee470f55b28ec2ec798c3a24d6e880913343
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10250
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/bindings/python/bt2/bt2/clock_class.py
src/bindings/python/bt2/bt2/component.py
src/bindings/python/bt2/bt2/event_class.py
src/bindings/python/bt2/bt2/field_class.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/user_attributes.py

index e87e6825931fa4de0f55801801eb5a36014eea67..0c9daea5289948fb65d8b618fd053127cd1d7973 100644 (file)
@@ -98,50 +98,36 @@ class _ClockClass(bt2_user_attrs._WithUserAttrs, _ClockClassConst):
     def _set_user_attributes_ptr(obj_ptr, value_ptr):
         native_bt.clock_class_set_user_attributes(obj_ptr, value_ptr)
 
-    def _name(self, name):
+    def _set_name(self, name):
         bt2_utils._check_str(name)
         status = native_bt.clock_class_set_name(self._ptr, name)
         bt2_utils._handle_func_status(status, "cannot set clock class object's name")
 
-    _name = property(fset=_name)
-
-    def _description(self, description):
+    def _set_description(self, description):
         bt2_utils._check_str(description)
         status = native_bt.clock_class_set_description(self._ptr, description)
         bt2_utils._handle_func_status(
             status, "cannot set clock class object's description"
         )
 
-    _description = property(fset=_description)
-
-    def _frequency(self, frequency):
+    def _set_frequency(self, frequency):
         bt2_utils._check_uint64(frequency)
         native_bt.clock_class_set_frequency(self._ptr, frequency)
 
-    _frequency = property(fset=_frequency)
-
-    def _precision(self, precision):
+    def _set_precision(self, precision):
         bt2_utils._check_uint64(precision)
         native_bt.clock_class_set_precision(self._ptr, precision)
 
-    _precision = property(fset=_precision)
-
-    def _offset(self, offset):
+    def _set_offset(self, offset):
         bt2_utils._check_type(offset, ClockClassOffset)
         native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles)
 
-    _offset = property(fset=_offset)
-
-    def _origin_is_unix_epoch(self, origin_is_unix_epoch):
+    def _set_origin_is_unix_epoch(self, origin_is_unix_epoch):
         bt2_utils._check_bool(origin_is_unix_epoch)
         native_bt.clock_class_set_origin_is_unix_epoch(
             self._ptr, int(origin_is_unix_epoch)
         )
 
-    _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch)
-
-    def _uuid(self, uuid):
+    def _set_uuid(self, uuid):
         bt2_utils._check_type(uuid, uuidp.UUID)
         native_bt.clock_class_set_uuid(self._ptr, uuid.bytes)
-
-    _uuid = property(fset=_uuid)
index 30f0849c7c4d4ce066eb25cd60f2c651a44678ea..b0398eaa04b8e43a424216c8543b87a1a255115a 100644 (file)
@@ -796,10 +796,10 @@ class _UserComponent(metaclass=_UserComponentType):
             raise bt2_error._MemoryError("could not create trace class")
 
         tc = bt2_trace_class._TraceClass._create_from_ptr(tc_ptr)
-        tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id
+        tc._set_assigns_automatic_stream_class_id(assigns_automatic_stream_class_id)
 
         if user_attributes is not None:
-            tc._user_attributes = user_attributes
+            tc._set_user_attributes(user_attributes)
 
         return tc
 
@@ -823,27 +823,27 @@ class _UserComponent(metaclass=_UserComponentType):
         cc = bt2_clock_class._ClockClass._create_from_ptr(cc_ptr)
 
         if frequency is not None:
-            cc._frequency = frequency
+            cc._set_frequency(frequency)
 
         if name is not None:
-            cc._name = name
+            cc._set_name(name)
 
         if user_attributes is not None:
-            cc._user_attributes = user_attributes
+            cc._set_user_attributes(user_attributes)
 
         if description is not None:
-            cc._description = description
+            cc._set_description(description)
 
         if precision is not None:
-            cc._precision = precision
+            cc._set_precision(precision)
 
         if offset is not None:
-            cc._offset = offset
+            cc._set_offset(offset)
 
-        cc._origin_is_unix_epoch = origin_is_unix_epoch
+        cc._set_origin_is_unix_epoch(origin_is_unix_epoch)
 
         if uuid is not None:
-            cc._uuid = uuid
+            cc._set_uuid(uuid)
 
         return cc
 
index a6745c174ddc5d210b71480ef7093be372cbc76a..905a7fc27188e4618999320fdeaa1c6eeb81fcea 100644 (file)
@@ -128,23 +128,17 @@ class _EventClass(bt2_user_attrs._WithUserAttrs, _EventClassConst):
     def _set_user_attributes_ptr(obj_ptr, value_ptr):
         native_bt.event_class_set_user_attributes(obj_ptr, value_ptr)
 
-    def _name(self, name):
+    def _set_name(self, name):
         return native_bt.event_class_set_name(self._ptr, name)
 
-    _name = property(fset=_name)
-
-    def _log_level(self, log_level):
+    def _set_log_level(self, log_level):
         native_bt.event_class_set_log_level(self._ptr, log_level)
 
-    _log_level = property(fset=_log_level)
-
-    def _emf_uri(self, emf_uri):
+    def _set_emf_uri(self, emf_uri):
         status = native_bt.event_class_set_emf_uri(self._ptr, emf_uri)
         bt2_utils._handle_func_status(status, "cannot set event class object's EMF URI")
 
-    _emf_uri = property(fset=_emf_uri)
-
-    def _specific_context_field_class(self, context_field_class):
+    def _set_specific_context_field_class(self, context_field_class):
         status = native_bt.event_class_set_specific_context_field_class(
             self._ptr, context_field_class._ptr
         )
@@ -152,9 +146,7 @@ class _EventClass(bt2_user_attrs._WithUserAttrs, _EventClassConst):
             status, "cannot set event class object's context field class"
         )
 
-    _specific_context_field_class = property(fset=_specific_context_field_class)
-
-    def _payload_field_class(self, payload_field_class):
+    def _set_payload_field_class(self, payload_field_class):
         status = native_bt.event_class_set_payload_field_class(
             self._ptr, payload_field_class._ptr
         )
@@ -162,8 +154,6 @@ class _EventClass(bt2_user_attrs._WithUserAttrs, _EventClassConst):
             status, "cannot set event class object's payload field class"
         )
 
-    _payload_field_class = property(fset=_payload_field_class)
-
     @staticmethod
     def _validate_create_params(
         name,
index 7396535772d3ccd126b62f9a2d62b147400b68e6..b361828fc2d34a6336614a05c5a61437ce1686fa 100644 (file)
@@ -115,14 +115,12 @@ class _IntegerFieldClassConst(_FieldClassConst):
 
 
 class _IntegerFieldClass(_FieldClass, _IntegerFieldClassConst):
-    def _field_value_range(self, size):
+    def _set_field_value_range(self, size):
         if size < 1 or size > 64:
             raise ValueError("Value is outside valid range [1, 64] ({})".format(size))
         native_bt.field_class_integer_set_field_value_range(self._ptr, size)
 
-    _field_value_range = property(fset=_field_value_range)
-
-    def _preferred_display_base(self, base):
+    def _set_preferred_display_base(self, base):
         bt2_utils._check_uint64(base)
 
         if base not in (
@@ -135,8 +133,6 @@ class _IntegerFieldClass(_FieldClass, _IntegerFieldClassConst):
 
         native_bt.field_class_integer_set_preferred_display_base(self._ptr, base)
 
-    _preferred_display_base = property(fset=_preferred_display_base)
-
 
 class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst, _FieldClassConst):
     _NAME = "Const unsigned integer"
@@ -467,7 +463,7 @@ class _StructureFieldClass(_StructureFieldClassConst, _FieldClass):
         )
 
         if user_attributes is not None:
-            self[name]._user_attributes = user_attributes_value
+            self[name]._set_user_attributes(user_attributes_value)
 
     def __iadd__(self, members):
         for name, field_class in members:
@@ -568,14 +564,12 @@ class _OptionWithBoolSelectorFieldClass(
 ):
     _NAME = "Option (with boolean selector)"
 
-    def _selector_is_reversed(self, selector_is_reversed):
+    def _set_selector_is_reversed(self, selector_is_reversed):
         bt2_utils._check_bool(selector_is_reversed)
         native_bt.field_class_option_with_selector_field_bool_set_selector_is_reversed(
             self._ptr, selector_is_reversed
         )
 
-    _selector_is_reversed = property(fset=_selector_is_reversed)
-
 
 class _OptionWithIntegerSelectorFieldClass(
     _OptionWithIntegerSelectorFieldClassConst, _OptionWithSelectorFieldClass
@@ -801,7 +795,7 @@ class _VariantFieldClassWithoutSelector(
         )
 
         if user_attributes is not None:
-            self[name]._user_attributes = user_attributes_value
+            self[name]._set_user_attributes(user_attributes_value)
 
     def __iadd__(self, options):
         for name, field_class in options:
@@ -855,7 +849,7 @@ class _VariantFieldClassWithIntegerSelector(
         )
 
         if user_attributes is not None:
-            self[name]._user_attributes = user_attributes_value
+            self[name]._set_user_attributes(user_attributes_value)
 
     def __iadd__(self, options):
         for name, field_class, ranges in options:
index 1583645523731677462824c7cceab32096dbc1f2..c8c3e1bc125afdc70c346682a65bbe64aa528205 100644 (file)
@@ -86,8 +86,6 @@ class _Stream(bt2_user_attrs._WithUserAttrs, _StreamConst):
     def _set_user_attributes_ptr(obj_ptr, value_ptr):
         native_bt.stream_set_user_attributes(obj_ptr, value_ptr)
 
-    def _name(self, name):
+    def _set_name(self, name):
         bt2_utils._check_str(name)
         native_bt.stream_set_name(self._ptr, name)
-
-    _name = property(fget=_StreamConst.name.fget, fset=_name)
index 41fa12abf514628c1f2c5161a3c426af83a67ef4..00ba8b37dc337ce5ec2609b08286dd9a488954fb 100644 (file)
@@ -246,22 +246,22 @@ class _StreamClass(bt2_user_attrs._WithUserAttrs, _StreamClassConst):
         event_class = bt2_event_class._EventClass._create_from_ptr(ec_ptr)
 
         if name is not None:
-            event_class._name = name
+            event_class._set_name(name)
 
         if user_attributes is not None:
-            event_class._user_attributes = user_attributes
+            event_class._set_user_attributes(user_attributes)
 
         if log_level is not None:
-            event_class._log_level = log_level
+            event_class._set_log_level(log_level)
 
         if emf_uri is not None:
-            event_class._emf_uri = emf_uri
+            event_class._set_emf_uri(emf_uri)
 
         if specific_context_field_class is not None:
-            event_class._specific_context_field_class = specific_context_field_class
+            event_class._set_specific_context_field_class(specific_context_field_class)
 
         if payload_field_class is not None:
-            event_class._payload_field_class = payload_field_class
+            event_class._set_payload_field_class(payload_field_class)
 
         return event_class
 
@@ -269,22 +269,16 @@ class _StreamClass(bt2_user_attrs._WithUserAttrs, _StreamClassConst):
     def _set_user_attributes_ptr(obj_ptr, value_ptr):
         native_bt.stream_class_set_user_attributes(obj_ptr, value_ptr)
 
-    def _name(self, name):
+    def _set_name(self, name):
         status = native_bt.stream_class_set_name(self._ptr, name)
         bt2_utils._handle_func_status(status, "cannot set stream class object's name")
 
-    _name = property(fset=_name)
-
-    def _assigns_automatic_event_class_id(self, auto_id):
+    def _set_assigns_automatic_event_class_id(self, auto_id):
         native_bt.stream_class_set_assigns_automatic_event_class_id(self._ptr, auto_id)
 
-    _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id)
-
-    def _assigns_automatic_stream_id(self, auto_id):
+    def _set_assigns_automatic_stream_id(self, auto_id):
         native_bt.stream_class_set_assigns_automatic_stream_id(self._ptr, auto_id)
 
-    _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id)
-
     def _set_supports_packets(self, supports, with_begin_cs=False, with_end_cs=False):
         native_bt.stream_class_set_supports_packets(
             self._ptr, supports, with_begin_cs, with_end_cs
@@ -295,16 +289,12 @@ class _StreamClass(bt2_user_attrs._WithUserAttrs, _StreamClassConst):
             self._ptr, supports, with_cs
         )
 
-    _supports_discarded_events = property(fset=_set_supports_discarded_events)
-
     def _set_supports_discarded_packets(self, supports, with_cs):
         native_bt.stream_class_set_supports_discarded_packets(
             self._ptr, supports, with_cs
         )
 
-    _supports_discarded_packets = property(fset=_set_supports_discarded_packets)
-
-    def _packet_context_field_class(self, packet_context_field_class):
+    def _set_packet_context_field_class(self, packet_context_field_class):
         status = native_bt.stream_class_set_packet_context_field_class(
             self._ptr, packet_context_field_class._ptr
         )
@@ -312,22 +302,16 @@ class _StreamClass(bt2_user_attrs._WithUserAttrs, _StreamClassConst):
             status, "cannot set stream class' packet context field class"
         )
 
-    _packet_context_field_class = property(fset=_packet_context_field_class)
-
-    def _event_common_context_field_class(self, event_common_context_field_class):
+    def _set_event_common_context_field_class(self, event_common_context_field_class):
         set_context_fn = native_bt.stream_class_set_event_common_context_field_class
         status = set_context_fn(self._ptr, event_common_context_field_class._ptr)
         bt2_utils._handle_func_status(
             status, "cannot set stream class' event context field type"
         )
 
-    _event_common_context_field_class = property(fset=_event_common_context_field_class)
-
-    def _default_clock_class(self, clock_class):
+    def _set_default_clock_class(self, clock_class):
         native_bt.stream_class_set_default_clock_class(self._ptr, clock_class._ptr)
 
-    _default_clock_class = property(fset=_default_clock_class)
-
     @classmethod
     def _validate_create_params(
         cls,
index 0f18e3a80993cbeaf85e1839d4a6d9e2710b760a..9b8e4cc45c1ed1ea7e93bfc4d3032601d4065df2 100644 (file)
@@ -204,23 +204,19 @@ class _Trace(bt2_user_attrs._WithUserAttrs, _TraceConst):
     _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClass)
     _trace_env_pycls = property(lambda _: _TraceEnvironment)
 
-    def _name(self, name):
+    def _set_name(self, name):
         bt2_utils._check_str(name)
         status = native_bt.trace_set_name(self._ptr, name)
         bt2_utils._handle_func_status(status, "cannot set trace object's name")
 
-    _name = property(fset=_name)
-
     @staticmethod
     def _set_user_attributes_ptr(obj_ptr, value_ptr):
         native_bt.trace_set_user_attributes(obj_ptr, value_ptr)
 
-    def _uuid(self, uuid):
+    def _set_uuid(self, uuid):
         bt2_utils._check_type(uuid, uuidp.UUID)
         native_bt.trace_set_uuid(self._ptr, uuid.bytes)
 
-    _uuid = property(fset=_uuid)
-
     def create_stream(self, stream_class, id=None, name=None, user_attributes=None):
         bt2_utils._check_type(stream_class, bt2_stream_class._StreamClass)
 
@@ -248,10 +244,10 @@ class _Trace(bt2_user_attrs._WithUserAttrs, _TraceConst):
         stream = bt2_stream._Stream._create_from_ptr(stream_ptr)
 
         if name is not None:
-            stream._name = name
+            stream._set_name(name)
 
         if user_attributes is not None:
-            stream._user_attributes = user_attributes
+            stream._set_user_attributes(user_attributes)
 
         return stream
 
index dc872eab419d5ad738703c8a363d3ad32d60819a..38ee59b27af7ee885b3a4791ff02f60e0da829ff 100644 (file)
@@ -151,13 +151,13 @@ class _TraceClass(bt2_user_attrs._WithUserAttrs, _TraceClassConst):
         trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
 
         if name is not None:
-            trace._name = name
+            trace._set_name(name)
 
         if user_attributes is not None:
-            trace._user_attributes = user_attributes
+            trace._set_user_attributes(user_attributes)
 
         if uuid is not None:
-            trace._uuid = uuid
+            trace._set_uuid(uuid)
 
         if environment is not None:
             for key, value in environment.items():
@@ -220,16 +220,16 @@ class _TraceClass(bt2_user_attrs._WithUserAttrs, _TraceClassConst):
         sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
 
         if name is not None:
-            sc._name = name
+            sc._set_name(name)
 
         if user_attributes is not None:
-            sc._user_attributes = user_attributes
+            sc._set_user_attributes(user_attributes)
 
         if event_common_context_field_class is not None:
-            sc._event_common_context_field_class = event_common_context_field_class
+            sc._set_event_common_context_field_class(event_common_context_field_class)
 
         if default_clock_class is not None:
-            sc._default_clock_class = default_clock_class
+            sc._set_default_clock_class(default_clock_class)
 
         # call after `sc._default_clock_class` because, if
         # `packets_have_beginning_default_clock_snapshot` or
@@ -245,10 +245,10 @@ class _TraceClass(bt2_user_attrs._WithUserAttrs, _TraceClassConst):
         # `packet_context_field_class` is not `None`, then this stream
         # class needs to support packets already.
         if packet_context_field_class is not None:
-            sc._packet_context_field_class = packet_context_field_class
+            sc._set_packet_context_field_class(packet_context_field_class)
 
-        sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
-        sc._assigns_automatic_stream_id = assigns_automatic_stream_id
+        sc._set_assigns_automatic_event_class_id(assigns_automatic_event_class_id)
+        sc._set_assigns_automatic_stream_id(assigns_automatic_stream_id)
         sc._set_supports_discarded_events(
             supports_discarded_events, discarded_events_have_default_clock_snapshots
         )
@@ -261,16 +261,12 @@ class _TraceClass(bt2_user_attrs._WithUserAttrs, _TraceClassConst):
     def _set_user_attributes_ptr(obj_ptr, value_ptr):
         native_bt.trace_class_set_user_attributes(obj_ptr, value_ptr)
 
-    def _assigns_automatic_stream_class_id(self, auto_id):
+    def _set_assigns_automatic_stream_class_id(self, auto_id):
         bt2_utils._check_bool(auto_id)
         return native_bt.trace_class_set_assigns_automatic_stream_class_id(
             self._ptr, auto_id
         )
 
-    _assigns_automatic_stream_class_id = property(
-        fset=_assigns_automatic_stream_class_id
-    )
-
     # Field class creation methods.
 
     def _check_field_class_create_status(self, ptr, type_name):
@@ -282,7 +278,7 @@ class _TraceClass(bt2_user_attrs._WithUserAttrs, _TraceClassConst):
     @staticmethod
     def _set_field_class_user_attrs(fc, user_attributes):
         if user_attributes is not None:
-            fc._user_attributes = user_attributes
+            fc._set_user_attributes(user_attributes)
 
     def create_bool_field_class(self, user_attributes=None):
         field_class_ptr = native_bt.field_class_bool_create(self._ptr)
@@ -322,10 +318,10 @@ class _TraceClass(bt2_user_attrs._WithUserAttrs, _TraceClassConst):
         field_class = py_cls._create_from_ptr(field_class_ptr)
 
         if field_value_range is not None:
-            field_class._field_value_range = field_value_range
+            field_class._set_field_value_range(field_value_range)
 
         if preferred_display_base is not None:
-            field_class._preferred_display_base = preferred_display_base
+            field_class._set_preferred_display_base(preferred_display_base)
 
         self._set_field_class_user_attrs(field_class, user_attributes)
         return field_class
@@ -467,7 +463,7 @@ class _TraceClass(bt2_user_attrs._WithUserAttrs, _TraceClassConst):
         self._check_field_class_create_status(ptr, "option")
         fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
         self._set_field_class_user_attrs(fc, user_attributes)
-        fc._selector_is_reversed = selector_is_reversed
+        fc._set_selector_is_reversed(selector_is_reversed)
         return fc
 
     def create_option_with_integer_selector_field_class(
index 810dcbdbe23f0b55aa2ffe2dc6fef3447bc3f861..6fdef2e1d4ac7f4d882e536154920ed0d56f0e68 100644 (file)
@@ -42,9 +42,7 @@ class _WithUserAttrs(_WithUserAttrsBase, abc.ABC):
     def _set_user_attributes_ptr(obj_ptr, value_ptr):
         raise NotImplementedError
 
-    def _user_attributes(self, user_attributes):
+    def _set_user_attributes(self, user_attributes):
         value = bt2_value.create_value(user_attributes)
         bt2_utils._check_type(value, bt2_value.MapValue)
         self._set_user_attributes_ptr(self._ptr, value._ptr)
-
-    _user_attributes = property(fset=_user_attributes)
This page took 0.033324 seconds and 4 git commands to generate.