Skip to content

Add missing __class_getitem__ methods to typing - #1062

Merged
Siyet merged 1 commit into
msgspec:mainfrom
sobolevn:add-missing-class-getitem
Jun 14, 2026
Merged

Siyet merged 1 commit into
msgspec:mainfrom
sobolevn:add-missing-class-getitem

Conversation

@sobolevn

Copy link
Copy Markdown
Member

Runtime:

>>> import msgspec
>>> msgspec.json.Decoder.__class_getitem__((int,))
msgspec.json.Decoder[int]

Mypy:

» mypy -c 'import msgspec; msgspec.json.Decoder.__class_getitem__'
<string>:1: error: "type[Decoder[_T]]" has no attribute "__class_getitem__"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Now this symbol would be defined.
I used the same definition that we use in typeshed: https://github.com/python/typeshed/blob/7e3b6cd8101ec0b80eedffa9a0ffd949f3ea88ab/stdlib/queue.pyi#L49

@Siyet

Siyet commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Thanks @sobolevn! Verified the stub matches runtime: both msgspec.json.Decoder and msgspec.msgpack.Decoder register __class_getitem__ via Py_GenericAlias (METH_CLASS) in _core.c, and at runtime Decoder[int] returns a real types.GenericAlias. These two are the only Generic classes in the stubs (Encoder is not generic), so coverage is complete. Merging.

@Siyet
Siyet added this pull request to the merge queue Jun 14, 2026
Merged via the queue into msgspec:main with commit 2b2e155 Jun 14, 2026
25 checks passed
pull Bot pushed a commit to Mu-L/msgspec that referenced this pull request Sep 5, 2026
Closes msgspec#1056.

Adds a `stubtest` step to the `test-typing` job so stub/runtime drift is
caught in CI instead of by hand (as in msgspec#1043, msgspec#1062). `mypy` is already
in the `test-typing` group, so no new dependency.

### Root-cause fixes (instead of allowlisting)

- **Positional-only `__text_signature__`.** `json`/`msgpack`
`encode`/`decode` and the `Encoder`/`Decoder` methods take their first
arg positional-only at runtime, but their C `__text_signature__` omitted
the `/`, so `inspect.signature` reported them as positional-or-keyword
and stubtest flagged 10 mismatches. Added the `/` in `_core.c` so
introspection matches runtime; behaviour is unchanged (keyword calls
already raised). This removes all 10 pos-only allowlist entries.
- **`StructMeta` `@disjoint_base`.** `StructMeta` is a disjoint base at
runtime; marked it `@disjoint_base` in the stub (the stub already
imports from `typing_extensions`, so no new floor of consequence).
Removes that allowlist entry.

### Other stub fixes

- `structs.pyi` was missing `__all__`.
- `json.format`s stub marked `buf` positional-only (`/`), but the
runtime accepts it as a keyword, so the `/` is dropped to match (this is
the one callable that really takes a keyword).

### Allowlist

What is left in `tests/typing/stubtest_allowlist.txt` is 10 structural
false positives stubtest cannot see through: the per-class `__init__`
synthesized in C for every `Struct`/`inspect` type, the custom `__new__`
on `Meta`/`Ext`/`UnsetType`, `Struct.__init_subclass__` (config kwargs
via `dataclass_transform`, surfaces on 3.14+), and the private `_utils`
module.

Run with `--ignore-unused-allowlist`. The `test-typing` job runs
stubtest on a single Python resolved from `python-version-file:
pyproject.toml` (currently 3.14, floats up over time), not a pinned
matrix, so a single superset allowlist with `--ignore-unused` is robust
to the runner Python moving. Verified green on 3.12, 3.14, 3.15.

---------

Co-authored-by: Siyet <Siyet@users.noreply.github.com>
pull Bot pushed a commit to Future-Outlier/msgspec that referenced this pull request Sep 14, 2026
Widening the runtime to accept `None` was the alternative. I rejected
it: the docstring specifies `offset : int, optional` and reserves `-1`
as the sentinel for writing at the end of the buffer, which leaves
`None` with no meaning to carry. Narrowing the stub makes the declared
type match both the C code and the documented contract.

The `int | None` traces back to the original `Optional[int]` annotation
and survived the 3.10+ syntax migration in msgspec#1041. Nothing in the tree
passes `None`.

Residual limitation: this edits the same two stub lines as open PR
msgspec#1160, which reshapes `buffer` and the return type while keeping
`offset: int | None`. Whichever lands second needs a one-line rebase.
The two changes are otherwise independent, since msgspec#1160 keeps reading
`offset` via `PyLong_AsSsize_t`.

Measurement, CPython 3.14:

- `just test-typing` clean under mypy 2.3.0, pyright 1.1.411, pyrefly
1.2.0.
- Reverting the stub hunk alone makes all three checkers fail on both
new call sites in `basic_typing_examples.py`, so the type-level test is
not vacuous.
- `pytest tests/unit`: 6119 passed, 404 skipped, identical to the
pre-change baseline. The two runtime assertions are pins on current
behavior rather than new coverage.
- `ruff format` collapses the signature onto one line once `| None` is
gone; that reformat is included.

No changelog entry, following merged stub PRs msgspec#1055, msgspec#1057, msgspec#1062,
msgspec#1065, msgspec#1074 and msgspec#1093, which leave it to the aggregated "type stub
improvements" line at release.

Co-authored-by: Dylan Pulver <dylanpulver@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants