GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support PEP 747 TypeForm in deserialization APIs by yukinarit · Pull Request #796 · yukinarit/pyserde

Conversation

@yukinarit

@yukinarit yukinarit commented Sep 16, 2026

Copy link
Copy Markdown
Owner

What

Adds a TypeForm[T] overload to the deserialization entry points so that passing a type expression infers a precise return type instead of Any.

Call Before After
from_json(Foo, s) Foo Foo (unchanged)
from_json(list[Foo], s) Any list[Foo]
from_json(Optional[Foo], s) Any Foo | None
from_json(Union[Foo, Bar], s) Any Foo | Bar
from_json(dict[str, list[Foo]], s) Any dict[str, list[Foo]]
from_json(Untagged(Foo | Bar), s) Any Any (unchanged, correct)

Why

Every from_* function used a two-overload workaround — type[T] -> T for class objects, plus a c: Any -> Any catch-all commented # For Union, Optional etc. That catch-all existed only so non-class type expressions wouldn't fail to match, at the cost of silently losing the return type. It affected usages pyserde's own docs recommend, including the from_dict(list[Foo], lst) doctest in serde/de.py.

PEP 747 (Final, targets Python 3.15) adds TypeForm[T], which describes exactly this: the value of a type expression.

How

A TypeForm[T] -> T overload is inserted into from_json, from_msgpack, from_yaml, from_toml, from_pickle, from_dict and from_tuple. Overload order is deliberate:

  • type[T] stays first, so plain-class calls resolve exactly as they do today, even on checkers with no PEP 747 support.
  • The Any overload stays last. examples/union_directly.py passes tagging instances (Untagged(...), InternalTagging(...), AdjacentTagging(...)), which are objects rather than type expressions and cannot match TypeForm.

Does this break downstream users?

No — this was the main adoption risk, so it was tested explicitly.

  • mypy users need no configuration. mypy emits TypeForm is experimental, must be enabled with --enable-incomplete-feature=TypeForm only when checking the defining file. Installing a prototype into site-packages and checking a consumer gives Success: no issues found and the precise inference above. The enable_incomplete_feature setting added here is only for pyserde's own CI.
  • pyright users degrade gracefully. Without enableExperimentalFeatures, pyright still resolves list[Foo]/dict[...]/tuple[...] precisely; union forms fall through to the Any overload with no error — i.e. exactly today's behaviour, never worse.
  • type[T] variables still forward. The common pattern def load(cls: type[T], s: str) -> T: return from_json(cls, s) type-checks clean under both.

Drive-by fixes

Two pre-existing bugs surfaced by the new overloads:

  • from_msgpack's implementation takes a skip_none parameter that was missing from both overloads, making it uncallable in typed code.
  • from_dict's o: dict[str, Any] was too narrow for non-dict targets — the doctest from_dict(list[Foo], lst) passes a list. Widened to Any on the non-class overloads only, so dataclass calls keep their input checking.

Tests

New tests/test_typing.py using assert_type. It is a no-op at runtime, so it runs under the normal pytest invocation while pyright makes the real assertions via the existing pre-commit hook — guarding against a future refactor silently collapsing the overloads back to Any.

Verified locally: pytest tests --doctest-modules serde (5284 passed), examples/runner.py, and pre-commit run -a (black, pyright, ruff, mypy all pass).

Notes

  • typing_extensions floor raised to >=4.13.0 (TypeForm landed in 4.13). It is already a hard runtime dependency and already imported in serde/compat.py for TypeAliasType.
  • enable_incomplete_feature = ["TypeForm"] can be dropped once mypy enables it by default (TypeForm: Enable by default python/mypy#21262).
  • Not included, deliberately: serde/de.py from_obj is annotated type[T] but is documented and used with unions, Optional, list[...] and PEP 695 aliases — the sole reason for five type: ignores there. TypeForm[T] would fix it properly; left for a follow-up to keep this PR focused.

🤖 Generated with Claude Code

https://claude.ai/code/session_017M48r4aKMBGdbt4kcfVhmG

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yukinarit @claude
Support PEP 747 TypeForm in deserialization APIs
3bd0953
Every from_* entry point used a two-overload workaround: `type[T] -> T`
for class objects, plus a `c: Any -> Any` catch-all commented
"For Union, Optional etc.". The catch-all meant that passing a type
expression silently degraded the return type to Any, including usages
pyserde's own docs and doctests recommend, e.g. `from_dict(list[Foo], lst)`.

PEP 747 `TypeForm[T]` describes the value of a type expression, so add a
`TypeForm[T] -> T` overload to from_json, from_msgpack, from_yaml,
from_toml, from_pickle, from_dict and from_tuple.

`type[T]` stays first so plain-class calls resolve exactly as before. The
`Any` overload stays last because tagging instances (Untagged,
InternalTagging, AdjacentTagging) are objects, not type expressions, and
cannot match TypeForm.

Both checkers now infer `from_json(list[Foo], s)` as `list[Foo]`,
`Optional[Foo]` as `Foo | None` and `Union[Foo, Bar]` as `Foo | Bar`.

Downstream users need no configuration: mypy reports the "TypeForm is
experimental" error only when checking the defining file, so a consumer
importing pyserde from site-packages type-checks clean and still gets the
precise inference. pyright without enableExperimentalFeatures resolves
non-union forms precisely and falls back to Any for unions, i.e. exactly
the previous behaviour.

Two pre-existing bugs surfaced by the new overloads are fixed:

* from_msgpack's implementation takes a `skip_none` parameter that was
  missing from both overloads, making it uncallable in typed code.
* from_dict's `o: dict[str, Any]` was too narrow for non-dict targets;
  the doctest `from_dict(list[Foo], lst)` passes a list. Widened to Any on
  the non-class overloads only, so dataclass calls keep input checking.

Add tests/test_typing.py using assert_type. It is a no-op at runtime, so
it runs under pytest while pyright makes the real assertions via the
existing pre-commit hook, guarding against a refactor collapsing the
overloads back to Any.

Raise the typing_extensions floor to >=4.13.0 (TypeForm landed in 4.13),
enable it for mypy via enable_incomplete_feature and for pyright via
enableExperimentalFeatures.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017M48r4aKMBGdbt4kcfVhmG
@yukinarit yukinarit added the enhancement New feature or request label Sep 17, 2026
@yukinarit
yukinarit marked this pull request as ready for review September 17, 2026 13:57
@yukinarit
yukinarit merged commit 089f39e into main Sep 17, 2026
8 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@yukinarit
yukinarit deleted the pep747-typeform branch September 17, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

enhancement New feature or request

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@yukinarit
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.