Skip to content

Support type annotation with subscripted types, e.g. type[int]#26

Merged
maread99 merged 6 commits into
mainfrom
claude/lucid-hypatia-KOoLh
Jun 2, 2026
Merged

Support type annotation with subscripted types, e.g. type[int]#26
maread99 merged 6 commits into
mainfrom
claude/lucid-hypatia-KOoLh

Conversation

@maread99

@maread99 maread99 commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Summary

Adds support for validating inputs against the subscripted type annotation, for example type[int] (previously the unsubscripted type annotation 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 of X (the class X itself is valid, consistent with issubclass).
  • type[Union[X, Y]] (and the X | Y form) — input validated as being a subclass of one of the union members.
  • typing.Type[X] is equivalent to type[X].
  • Works alongside Coerce, Parser, Optional and Annotated metadata.

Implementation

  • New private helper validates_as_subclass resolves a subscripted argument (single type, typing.Any, or a union) against a candidate class using issubclass.
  • New origin is type branch in validates_against_hint. The input is already confirmed to be a class by the existing isinstance(obj, origin) check, so the branch only validates the subscripted argument.

Docs

  • Usage example incorporated into existing README examples.
  • docs/tutorials/tutorial.ipynb: new ### type section demonstrating valid usage and the error raised for invalid inputs.

Generated by Claude. Manual: fully revised and revisions made.
Generated by Claude Code

claude and others added 6 commits June 1, 2026 22:35
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
@maread99 maread99 added the enhancement New feature or request label Jun 2, 2026
@maread99 maread99 changed the title Support the type annotation Support type annotation with subscripted types, e.g. type[int] Jun 2, 2026
@maread99 maread99 merged commit 5731bbf into main Jun 2, 2026
8 checks passed
@maread99 maread99 deleted the claude/lucid-hypatia-KOoLh branch June 2, 2026 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants