~lttng/babeltrace/trunk

« back to all changes in this revision

Viewing changes to src/bindings/python/bt2/bt2/plugin.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:
91
91
 
92
92
 
93
93
class _PluginSet(object._SharedObject, collections.abc.Sequence):
94
 
    _put_ref = staticmethod(native_bt.plugin_set_put_ref)
95
 
    _get_ref = staticmethod(native_bt.plugin_set_get_ref)
 
94
    @staticmethod
 
95
    def _put_ref(ptr):
 
96
        native_bt.plugin_set_put_ref(ptr)
 
97
 
 
98
    @staticmethod
 
99
    def _get_ref(ptr):
 
100
        native_bt.plugin_set_get_ref(ptr)
96
101
 
97
102
    def __len__(self):
98
103
        count = native_bt.plugin_set_get_plugin_count(self._ptr)
232
237
 
233
238
 
234
239
class _Plugin(object._SharedObject):
235
 
    _put_ref = staticmethod(native_bt.plugin_put_ref)
236
 
    _get_ref = staticmethod(native_bt.plugin_get_ref)
 
240
    @staticmethod
 
241
    def _put_ref(ptr):
 
242
        native_bt.plugin_put_ref(ptr)
 
243
 
 
244
    @staticmethod
 
245
    def _get_ref(ptr):
 
246
        native_bt.plugin_get_ref(ptr)
237
247
 
238
248
    @property
239
249
    def name(self):