Skip to content

Expose and document Encodable.register - #772

Merged
jfeser merged 1 commit into
masterfrom
worktree-issue-769-encodable-register
Sep 2, 2026
Merged

Expose and document Encodable.register#772
jfeser merged 1 commit into
masterfrom
worktree-issue-769-encodable-register

Conversation

@eb8680

@eb8680 eb8680 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Resolves #769

@eb8680
eb8680 requested a review from jfeser September 2, 2026 15:36
The encoding registry was designed to be extensible so that a type the
library serializes badly, or not at all, can be given an encoding without
a library-level fix. That hook was only reachable as
`TypeToPydanticType.register` on the internal handler class, so the escape
hatch was effectively undiscoverable from the public interface.

`register` is now a classmethod on `Encodable`, documented with a doctest
that starts from the failure a user actually hits and ends with the type
encoding, decoding and schematizing. It takes a `TypeForm` rather than a
`type`, since the registry also accepts unions and typing special forms,
and returns a decorator that preserves the function it decorates. The
internal registrations keep calling `TypeToPydanticType.register`; the
library registering into its own handler class is the right layering.

The developer-facing pointer in `_NoEncoding`'s error message still names
the internal class, deliberately: #770 deletes that block outright, so
editing it here would buy nothing but a merge conflict.

`Encodable` stays a `TYPE_CHECKING`-only alias, so `@Encodable.register`
in typed code needs `# type: ignore[attr-defined]`, as the example shows.
That is forced, not incidental: mypy resolves a name as either a generic
alias (usable in annotations) or an object with attributes, never both.
Replacing the alias with a class of any shape -- plain, generic, Protocol,
or a `TypeForm`-typed metaclass `__getitem__` -- makes `register` check
but breaks `field: Encodable[T]`, and mypy ignores `__class_getitem__`
entirely, so no subscript signature can rescue it. Under that ignore the
call is `Any`, so the signature documents rather than checks.

Closes #769

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@eb8680
eb8680 force-pushed the worktree-issue-769-encodable-register branch from e576636 to baeacac Compare September 2, 2026 15:42
@jfeser
jfeser merged commit cf6e700 into master Sep 2, 2026
42 of 45 checks passed
@jfeser
jfeser deleted the worktree-issue-769-encodable-register branch September 2, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encodable should expose and document a public register method

2 participants