Metatype: A Classifier for Special Forms#

class my.typing.Metatype.Metatype(*values)#

A name-based classifier for the type annotations that aren’t ordinary type instances.

Each member holds a frozenset of the names (i.e. __name__) of the special forms it covers, so that Metatype(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 falsy NULL.

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/.kwargs accessors) 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_root detects them via isinstance instead.

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 types module.

LITERAL = frozenset({'Literal'})#

The Literal[...] form, handled specially during parsing.

property val: frozenset[str]#

The set of names associated with this metatype.