Search code examples
pythonpython-sphinxprotobuf-python

Cleaning-up sphinx-doc protobuf types


I feel like I had this figured out before but it's not working again, I am developing some code based on protobuf and grpc and in the documentation all of the types come out really messily:

create_session(name: str, path: str, file_type: <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object at 0x10984e7d0> = 0, sample_rate: <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object at 0x109837710> = 2, bit_depth: <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object at 0x10982c6d0> = 2, io_setting: <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object at 0x109819d10> = 1, is_interleaved: bool = True)

This is from the following function in my source


import ptsl.PTSL_pb2 as pt # my grpc-tools generated type header

# yada yada yada

    def create_session(self,
                       name: str,
                       path: str,
                       file_type: 'SessionAudioFormat' = pt.SAF_WAVE,
                       sample_rate: 'SampleRate' = pt.SR_48000,
                       bit_depth: 'BitDepth' = pt.Bit24,
                       io_setting: 'IOSettings' = pt.IO_Last,
                       is_interleaved: bool = True) -> None:

# etc...

My types annotations in the source are being converted into instances of that type and not just references to be linked into the rest of the documentation. Is there a way to make the documentation resolve to the real type names and link to my documentation of those types where I've done it (like any other type)?


Solution

  • My mistake, I'd forgotten to import the types at the top. Sphinx can't seem to link the types in the annotations to their definition unless they've been imported into the file, even if the annotation is a forward reference or a string.