Metatype: A Classifier for Special Forms#
- class my.typing.Metatype.Metatype(*values)#
A name-based classifier for the type annotations that aren’t ordinary
typeinstances.Each member holds a
frozensetof the names (i.e.__name__) of the special forms it covers, so thatMetatype(value)can categorize any annotation by looking up its name. Ordinary types (e.g.int,list,dict) belong to no special form and resolve to the falsyNULL.Examples
Categorize annotations by their special-form name:
>>> from typing import Any, Optional >>> from my.typing.Metatype import Metatype >>> Metatype(Any) is Metatype.ALWAYS True >>> Metatype(Optional[int]) is Metatype.POLY True >>> bool(Metatype(int)) # ordinary types resolve to the falsy NULL False
- NULL = frozenset({})#
Unrecognized / ordinary types – the falsy fallback.
- ALWAYS = frozenset({'Any', 'Unknown'})#
Forms that always match (e.g.
Any).
- NEVER = frozenset({'', 'AsyncGenerator', 'AsyncIterable', 'AsyncIterator', 'Callable', 'CapsuleType', 'Concatenate', 'Coroutine', 'Generator', 'Iterator', 'LiteralString', 'NamedTuple', 'Never', 'NewType', 'NoReturn', 'None', 'NoneType', 'NotImplementedType', 'Protocol', 'ReadOnly', 'Self', 'Type', 'TypeGuard', 'TypeIs', 'TypedDict'})#
Forms that are treated as unmatchable or are not yet handled. NOTE:
TypeVar/TypeVarTuple/ParamSpec(and its.args/.kwargsaccessors) are deliberately absent here – each per-declaration instance reports its own parameter name (e.g.'T') as__name__, not the special form’s name, so they can never be matched by name.MyType._process_rootdetects them viaisinstanceinstead.
- MONO = frozenset({'Annotated', 'ClassVar', 'Final', 'NotRequired', 'Required', 'Unpack'})#
Simple wrapper forms that unwrap to a single inner type (e.g.
Annotated[int, ...]).
- POLY = frozenset({'Optional', 'Union', 'UnionType'})#
Forms that wrap more than one type at once (i.e. unions).
- TYPE = frozenset({'Ellipsis', 'EllipsisType', 'TypeAlias', 'TypeAliasType', 'ellipsis'})#
Fundamental stdlib forms from the
typesmodule.
- LITERAL = frozenset({'Literal'})#
The
Literal[...]form, handled specially during parsing.