eclipse-cyclonedds / cyclonedds-python

Other
54 stars 44 forks source link

Make `cyclonedds typeof` work on derived types #243

Closed eboasson closed 2 months ago

eboasson commented 3 months ago

This solves

  File "/usr/lib64/python3.11/site-packages/cyclonedds/idl/__init__.py", line 39, in make_idl_struct
    bases = tuple(list(*bases) + [IdlStruct])
                  ^^^^^^^^^^^^
TypeError: 'IdlMeta' object is not iterable

when cyclonedds typeof is used to print a derived type.

With this change it prints the fields including those of the base type, but not the fact that is actually a derived type:

As defined in participant(s) 011015d7-62dd-3bff-a9fa-efb3000001c1
module Hierarchy {                                                                     
    @mutable                                                                           
    struct Base {                                                                      
        string fieldA;                                                                 
    };                                                                                 
};                                                                                     

As defined in participant(s) 011015d7-62dd-3bff-a9fa-efb3000001c1
module Hierarchy {                                                                     
    @mutable                                                                           
    struct Derived {                                                                   
        string fieldA;                                                                 
        string fieldB;                                                                 
    };                                                                                 
};                                                                                     

What I would like to see is of course:

module Hierarchy {                                                                     
    @mutable                                                                           
    struct Base {                                                                      
        string fieldA;                                                                 
    };                                                                                 
    @mutable                                                                           
    struct Derived : Base {                                                                   
        string fieldB;                                                                 
    };                                                                                 
};                                                                                     

Fixes #241 in the narrow sense that the error goes away and it does something at least somewhat useful.

eboasson commented 3 months ago

@adrianomarto you might give this a try, it does work for simple case, but whether I actually got it right ...

@thijsmie I do my best to solve whatever problems are discovered in the Python binding and tooling, but sometimes I have a feeling I am only finding something merely exhibiting the symptoms of a solution. What I did here feels like one of those cases, though it does work for cyclonedds typeof Hello given the following:

module Hierarchy {
  @mutable
  struct Vogel {
    long v;
  };
  struct Kwartel : Vogel {
    long k;
  };
  @mutable struct Base {
    string fieldA;
    Kwartel fieldK;
  };
  struct Derived : Base {
    string fieldB;
  };
};
from dataclasses import dataclass
from cyclonedds.domain import DomainParticipant
from cyclonedds.pub import DataWriter
from cyclonedds.topic import Topic

import Hierarchy
from time import sleep

dp = DomainParticipant(0)
tp = Topic(dp, "Hello", Hierarchy.Base)
tp2 = Topic(dp, "Hello", Hierarchy.Derived)
dw = DataWriter(dp, tp)
dw2 = DataWriter(dp, tp2)

while True:
    sleep(10)

If by any chance you feel like giving this a look over, I'd appreciate it.

adrianomarto commented 2 months ago

@eboasson, I confirm your fix also works for the specific cases I am working on. Thanks for that!

eboasson commented 2 months ago

Thank you very much @thijsmie, this is exactly the type of detail that I was worried about ☺️