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
Commit ecf2282
Browse filesBrowse the repository at this point in the historyBrowse files
Fix inspect.type_info crashing on mixed-type Literals (msgspec#1080)
Fixesmsgspec#1018.
### Problem
`msgspec.inspect.type_info` crashes on mixed-type `Literal`s:
```python
msgspec.inspect.type_info(Literal[1, None]) # TypeError: '<' not supported between instances of 'NoneType' and 'int'
msgspec.inspect.type_info(Literal[True, "yes"]) # same
```
`type_info` does `tuple(sorted(args))` on the literal's values, and
Python 3 forbids `<` between values of different types. The
encoder/decoder already handle these mixed-type literals fine (e.g.
`Literal[1, None]` decodes `1`/`null` and rejects `2`), so `type_info`
failing on them is a contract violation.
### Fix
Sort the literal values with a `(type name, value)` key instead. This
keeps existing same-type ordering identical (`Literal[3,1,2]` →
`(1,2,3)`, `Literal[True,False]` → `(False,True)`) while grouping
mixed-type literals deterministically rather than crashing, and a
`try/except TypeError` falls back to the original order as a safety net.
The `LiteralType.values` annotation is widened to allow mixed and `None`
members.
### Tests
Added `test_mixed_literal` (`Literal[1, None]`, `Literal[True, "yes"]`)
and `test_bool_literal`. Without the fix `test_mixed_literal` raises the
`TypeError`; with it the full `test_inspect.py`/`test_schema.py` pass
(276 passed, 13 skipped).
I'll add a `docs/changelog.md` entry referencing this PR number in a
follow-up commit.
Disclosure: I used AI assistance (Claude) to help locate and draft this
fix, under my direction and review.
0 commit comments