Support type annotation with subscripted types, e.g. type[int]#26
Merged
Conversation
Add validation support for the `type` annotation, including subscripted forms such as `type[int]` and `type[Union[int, str]]`. A subscripted `type` validates that the input is a subclass of the subscripted type (which may be a union). The unsubscripted `type` annotation already validated that an input was a class. Add tests covering valid and invalid inputs, together with use of `Coerce` and `Parser`. Update the README and tutorial notebook to demonstrate and confirm support for the `type` annotation. https://claude.ai/code/session_01Ast6azhb3Aj7tSFqa4Sx7G
Python 3.14 changed repr(typing.Union[int, str]) to "int | str". Add the "int, str" mapping to the test helper so the expected error message regex matches on both older Pythons and 3.14. https://claude.ai/code/session_01Ast6azhb3Aj7tSFqa4Sx7G
Complete the consolidated error-message check for invalid `type` inputs via a new INVALID_MSG_TYPE constant, replacing the individual assertions. Document the `type` annotation by extending the main `public_function` README example with `type`, `type[int]` and `type[Union[int, str]]` parameters (one also featured in the invalid-inputs example), and remove the standalone `type` section. https://claude.ai/code/session_01Ast6azhb3Aj7tSFqa4Sx7G
Only append " of type {type(obj)}." to the `type` subclass-failure
TypeError when it adds information, i.e. when the received class has a
metaclass other than `type`. For an ordinary class the suffix is
redundant (the repr already identifies the class) and is omitted.
Rename the README example's `type` parameters to g/h/i and the existing
keyword-only parameters to j/k so that all parameter names read in
alphabetical order.
Update the README, tutorial and tests to reflect the revised message,
and add a test covering the metaclass case where the type is noted.
https://claude.ai/code/session_01Ast6azhb3Aj7tSFqa4Sx7G
type annotationtype annotation with subscripted types, e.g. type[int]
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.
Summary
Adds support for validating inputs against the subscripted
typeannotation, for exampletype[int](previously the unsubscriptedtypeannotation only validated only that an input was a class.).Behaviour
type— input validated as being a class (unchanged).type[X]— input validated as being a subclass ofX(the classXitself is valid, consistent withissubclass).type[Union[X, Y]](and theX | Yform) — input validated as being a subclass of one of the union members.typing.Type[X]is equivalent totype[X].Coerce,Parser,OptionalandAnnotatedmetadata.Implementation
validates_as_subclassresolves a subscripted argument (single type,typing.Any, or a union) against a candidate class usingissubclass.origin is typebranch invalidates_against_hint. The input is already confirmed to be a class by the existingisinstance(obj, origin)check, so the branch only validates the subscripted argument.Docs
docs/tutorials/tutorial.ipynb: new### typesection demonstrating valid usage and the error raised for invalid inputs.Generated by Claude. Manual: fully revised and revisions made.
Generated by Claude Code