Skip to content

Convert typing tests from reveal_type+comments to assert_type - #1063

Merged
sobolevn merged 5 commits into
mainfrom
copilot/convert-tests-to-typing-extensions
Jun 14, 2026
Merged

sobolevn merged 5 commits into
mainfrom
copilot/convert-tests-to-typing-extensions

Conversation

Copilot AI commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Replaces the custom reveal_type + exec'd comment assertion approach with typing_extensions.assert_type, making type assertions first-class errors caught directly by the type checker.

Before:

def check_json_decode_typed_union() -> None:
    o: int | str = msgspec.json.decode(b"", type=int | str)
    reveal_type(o)  # assert "int" in typ and "str" in typ

After:

from typing_extensions import assert_type

def check_json_decode_typed_union() -> None:
    o: int | str = msgspec.json.decode(b"", type=int | str)
    assert_type(o, int | str)

Changes

  • basic_typing_examples.py: Replaced all 162 reveal_type()+comment assertions with assert_type(x, T). Added assert_type and Buffer to typing_extensions imports. Two spots where mypy and pyright genuinely disagree on the inferred type use variable assignments instead:

    • __match_args__ iteration: pyright narrows to Literal['x', 'y'], mypy gives str → _field_str: str = field
    • is_struct_type else-branch on type[Any]: pyright gives Any | type[Any], mypy gives type[Any] → _other_type: type[Any] = other
  • test_mypy.py / test_pyright.py: Replaced the custom output-parsing harness with a simple "run type checker, assert exit code 0" check. assert_type mismatches now surface directly as type checker errors.

Refs #1042

@sobolevn

Copy link
Copy Markdown
Member

I am reviewing this manually now, I've found several problems. This is the first step to change the typing tests for better :)

@sobolevn
sobolevn marked this pull request as ready for review June 13, 2026 11:36
@sobolevn
sobolevn requested a review from provinzkraut June 13, 2026 11:36
@sobolevn

Copy link
Copy Markdown
Member

Sorry for using AI-slop, but I did not want to manually convert all this copy-paste code :(
I double checked that every line is correct manually.

@Siyet

Siyet commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Thanks @sobolevn! Reviewed and verified the key parts:

  • The Struct.__rich_repr__ narrowing to list[tuple[str, Any]] is correct. Struct_rich_repr in _core.c builds a list of PyTuple_Pack(2, field, val), and at runtime P(1, "foo").__rich_repr__() returns [('x', 1), ('y', 'foo')]. The old Iterable[Any | tuple[Any] | tuple[str, Any] | tuple[str, Any, Any]] union was both too broad and inaccurate.
  • The assert_type conversions and the simplified exit-code-0 harness look good, and the two mypy/pyright divergence workarounds are sound.

One non-blocking nit: Meta.__rich_repr__ is still typed Iterable[tuple[str, Any]], but at runtime it also returns a list (Meta(ge=1).__rich_repr__() -> [('ge', 1)], built the same way in Meta_rich_repr). Want to narrow it to list[tuple[str, Any]] here too for consistency? Happy to merge as-is if you'd rather keep this PR focused.

@sobolevn
sobolevn enabled auto-merge June 14, 2026 16:58
@sobolevn
sobolevn added this pull request to the merge queue Jun 14, 2026
Merged via the queue into main with commit 49d5348 Jun 14, 2026
10 checks passed
@sobolevn
sobolevn deleted the copilot/convert-tests-to-typing-extensions branch June 14, 2026 16:59
pull Bot pushed a commit to Mu-L/msgspec that referenced this pull request Sep 24, 2026
The changelog has not been updated since the `## Version 0.22.0` section
was written, and that release was never published: PyPI still serves
0.21.1, and the tag and GitHub release were removed after the failed
run.

Adds ten entries under `## Unreleased`, covering twelve pull requests
merged after 2026-08-11. That is where they belong while the release
date is unknown. The entry for msgspec#1165 moves there from the 0.22.0 section
for the same reason, since its fix was merged on 2026-09-05. msgspec#1063 and
msgspec#1064 are added to the aggregated type stub line in the 0.22.0 section:
two June pull requests that edit shipped `.pyi` files and were missing
entirely.

A second commit extends `scripts/release_github.py` to substitute
`{issue}` references as well as `{pr}` ones, so they stop reaching the
release body as literal markup.

---------

Co-authored-by: Tseluiko Aleksandr <[email protected]>
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.

3 participants