From: Simon Marchi Date: Mon, 12 Jun 2023 20:29:52 +0000 (-0400) Subject: bt2: replace internal property setters with standard methods X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=dca9db19512fa4191bc965cf8dcce885f68a8bff;p=babeltrace.git bt2: replace internal property setters with standard methods 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/10250 Reviewed-by: Philippe Proulx --- diff --git a/src/bindings/python/bt2/bt2/clock_class.py b/src/bindings/python/bt2/bt2/clock_class.py index e87e6825..0c9daea5 100644 --- a/src/bindings/python/bt2/bt2/clock_class.py +++ b/src/bindings/python/bt2/bt2/clock_class.py @@ -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) diff --git a/src/bindings/python/bt2/bt2/component.py b/src/bindings/python/bt2/bt2/component.py index 30f0849c..b0398eaa 100644 --- a/src/bindings/python/bt2/bt2/component.py +++ b/src/bindings/python/bt2/bt2/component.py @@ -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 diff --git a/src/bindings/python/bt2/bt2/event_class.py b/src/bindings/python/bt2/bt2/event_class.py index a6745c17..905a7fc2 100644 --- a/src/bindings/python/bt2/bt2/event_class.py +++ b/src/bindings/python/bt2/bt2/event_class.py @@ -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, diff --git a/src/bindings/python/bt2/bt2/field_class.py b/src/bindings/python/bt2/bt2/field_class.py index 73965357..b361828f 100644 --- a/src/bindings/python/bt2/bt2/field_class.py +++ b/src/bindings/python/bt2/bt2/field_class.py @@ -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: diff --git a/src/bindings/python/bt2/bt2/stream.py b/src/bindings/python/bt2/bt2/stream.py index 15836455..c8c3e1bc 100644 --- a/src/bindings/python/bt2/bt2/stream.py +++ b/src/bindings/python/bt2/bt2/stream.py @@ -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) diff --git a/src/bindings/python/bt2/bt2/stream_class.py b/src/bindings/python/bt2/bt2/stream_class.py index 41fa12ab..00ba8b37 100644 --- a/src/bindings/python/bt2/bt2/stream_class.py +++ b/src/bindings/python/bt2/bt2/stream_class.py @@ -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, diff --git a/src/bindings/python/bt2/bt2/trace.py b/src/bindings/python/bt2/bt2/trace.py index 0f18e3a8..9b8e4cc4 100644 --- a/src/bindings/python/bt2/bt2/trace.py +++ b/src/bindings/python/bt2/bt2/trace.py @@ -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 diff --git a/src/bindings/python/bt2/bt2/trace_class.py b/src/bindings/python/bt2/bt2/trace_class.py index dc872eab..38ee59b2 100644 --- a/src/bindings/python/bt2/bt2/trace_class.py +++ b/src/bindings/python/bt2/bt2/trace_class.py @@ -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( diff --git a/src/bindings/python/bt2/bt2/user_attributes.py b/src/bindings/python/bt2/bt2/user_attributes.py index 810dcbdb..6fdef2e1 100644 --- a/src/bindings/python/bt2/bt2/user_attributes.py +++ b/src/bindings/python/bt2/bt2/user_attributes.py @@ -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)