You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.type_info crashes on mixed-type Literals #1018
`msgspec.inspect.type_info` crashes with `TypeError` when called on a `Literal` type that mixes value types (e.g. `Literal[1, None]` or `Literal[True, "yes"]`).
Reproduction
importmsgspec.inspectasmifromtypingimportLiteralmi.type_info(Literal[1, None])
# TypeError: '<' not supported between instances of 'NoneType' and 'int'mi.type_info(Literal[True, "yes"])
# TypeError: '<' not supported between instances of 'str' and 'bool'
Cause
`inspect.py:888` does `LiteralType(tuple(sorted(args)))` which calls `sorted()` on the Literal args. Python 3 does not allow comparison between different types, so mixed-type tuples like `(1, None)` or `(True, "yes")` raise `TypeError`.
Additionally, `LiteralType.values` type annotation (`Union[Tuple[bool, ...], Tuple[str, ...], Tuple[int, ...]]`) does not cover mixed-type tuples.
Suggested fix
Replace `sorted(args)` with a type-aware sort (e.g. `sorted(args, key=lambda x: (type(x).name, x))`) or skip sorting for mixed types.
Widen `LiteralType.values` to `Tuple[Union[bool, str, int, None], ...]` or similar.
Add tests for `type_info(Literal[True])`, `type_info(Literal[True, "yes"])`, `type_info(Literal[1, None])`.
This is a pre-existing issue (not introduced by #1004), but #1004 makes it more likely to encounter since it enables `Literal[True, ...]` combinations.
Activity
fix: handle mixed-type Literals in inspect.type_info
Fix inspect.type_info crashing on mixed-type Literals (msgspec#1080)