Skip to main content

types

The types module exposes the constructors and type objects that the interpreter creates implicitly. It is the canonical source of FunctionType, ModuleType, MethodType, MappingProxyType, SimpleNamespace, and the rest of the under-the-hood types that have no other Python name.

Source-of-record: Lib/types.py, types docs.

Constructable types

NameConstructor
FunctionTypeFunctionType(code, globals, name=None, argdefs=None, closure=None, kwdefaults=None)
LambdaTypeSame as FunctionType.
MethodTypeMethodType(func, instance)
BuiltinFunctionType(not constructible)
BuiltinMethodType(not constructible)
WrapperDescriptorType(not constructible)
MethodWrapperType(not constructible)
MethodDescriptorType(not constructible)
ClassMethodDescriptorType(not constructible)
ModuleTypeModuleType(name, doc=None)
TracebackTypeTracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)
FrameType(not constructible)
GetSetDescriptorType(not constructible)
MemberDescriptorType(not constructible)
MappingProxyTypeMappingProxyType(mapping)
SimpleNamespaceSimpleNamespace(**kwargs)
GenericAliasGenericAlias(origin, args)
UnionType`int
EllipsisTypetype(...). Singleton: Ellipsis.
NoneTypetype(None). Singleton: None.
NotImplementedTypetype(NotImplemented). Singleton.
CodeType(code.replace(...) is preferred).
CellType(closure cells).
GeneratorType(not constructible directly).
CoroutineType(not constructible directly).
AsyncGeneratorType(not constructible directly).

Helpers

FunctionReturns
new_class(name, bases=(), kwds=None, exec_body=None)Build a class using a metaclass and exec body.
prepare_class(name, bases=(), kwds=None)Metaclass and namespace for new_class.
resolve_bases(bases)Apply __mro_entries__ rewriting.
get_original_bases(cls)Bases pre-__mro_entries__.
coroutine(gen_func)Wrap a generator into an awaitable.
DynamicClassAttributeDescriptor that defers to the metaclass when accessed on the class.

Module helpers

HelperBehaviour
ModuleType instance attributes__name__, __doc__, __loader__, __spec__, __package__, __file__ (where applicable).
ModuleType.__class__ swapUsed by __getattr__ (PEP 562).

PEP 695 helpers (3.12+)

TypeAliasType instances (created by type Alias = ...) are also exposed via the types module. Lazy value is computed via __value__.

Gopy status

AreaState
Every type alias listed aboveComplete.
new_class, prepare_class, resolve_basesComplete.
SimpleNamespaceComplete.
MappingProxyTypeComplete.
GenericAlias, UnionTypeComplete.
TypeAliasType (PEP 695)Complete.

Reference

  • CPython 3.14: types.
  • Lib/types.py.
  • module/types/. gopy port.