Expose and document Encodable.register - #772
Merged
Merged
Conversation
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
force-pushed
the
worktree-issue-769-encodable-register
branch
from
September 2, 2026 15:42
e576636 to
baeacac
Compare
jfeser
approved these changes
Sep 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #769