Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literals of enum variants don't reference the enum class #363

Open
ItsDrike opened this issue Jun 25, 2023 · 1 comment
Open

Literals of enum variants don't reference the enum class #363

ItsDrike opened this issue Jun 25, 2023 · 1 comment

Comments

@ItsDrike
Copy link

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

@gaborbernat
Copy link
Member

PR welcome 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants