sphinx-autodoc-typehints icon indicating copy to clipboard operation
sphinx-autodoc-typehints copied to clipboard

Literals of enum variants don't reference the enum class

Open ItsDrike opened this issue 2 years ago • 1 comments

I'm trying to get a nice formatting for my method, which has several overloads, using literals with various enum variants. This is the code:

Click to expand the code snippet

class StructFormat(str, Enum):
    BOOL = "?"
    CHAR = "c"
    BYTE = "b"
    UBYTE = "B"
    SHORT = "h"
    USHORT = "H"
    INT = "i"
    UINT = "I"
    LONG = "l"
    ULONG = "L"
    FLOAT = "f"
    DOUBLE = "d"
    HALFFLOAT = "e"
    LONGLONG = "q"
    ULONGLONG = "Q"


INT_FORMATS_TYPE: TypeAlias = Union[
    Literal[StructFormat.BYTE],
    Literal[StructFormat.UBYTE],
    Literal[StructFormat.SHORT],
    Literal[StructFormat.USHORT],
    Literal[StructFormat.INT],
    Literal[StructFormat.UINT],
    Literal[StructFormat.LONG],
    Literal[StructFormat.ULONG],
    Literal[StructFormat.LONGLONG],
    Literal[StructFormat.ULONGLONG],
]

FLOAT_FORMATS_TYPE: TypeAlias = Union[
    Literal[StructFormat.FLOAT],
    Literal[StructFormat.DOUBLE],
    Literal[StructFormat.HALFFLOAT],
]


class BaseAsyncWriter(ABC):
    @abstractmethod
    async def write(self, data: bytes, /) -> None:
        ...

    @overload
    async def write_value(self, fmt: INT_FORMATS_TYPE, value: int, /) -> None:
        ...

    @overload
    async def write_value(self, fmt: FLOAT_FORMATS_TYPE, value: float, /) -> None:
        ...

    @overload
    async def write_value(self, fmt: Literal[StructFormat.BOOL], value: bool, /) -> None:
        ...

    @overload
    async def write_value(self, fmt: Literal[StructFormat.CHAR], value: str, /) -> None:
        ...

    async def write_value(self, fmt: StructFormat, value: object, /) -> None:
        """Write a given ``value`` as given struct format (``fmt``) in big-endian mode."""
        await self.write(struct.pack(">" + fmt.value, value))

You can also see the entire file here

My issue is that it the docs for write_value function don't include links to back to the StructFormat class. Also, the formatting here seems very cluttery, the enum is printed with the string value representation, which I don't think is all that relevant to see here. Also, it's really weird that you can directly see ~typing.Literal text, rather than just Literal (this might be an issue on my side though, by not having intersphinx configured properly maybe? Not sure though as other python stdlib things seem to produce links normally).

image

ItsDrike avatar Jun 25 '23 02:06 ItsDrike

PR welcome 😁

gaborbernat avatar Jun 25 '23 03:06 gaborbernat