#include "Python.h"
#include "pycore_object.h"
#include "pycore_typevarobject.h"
#include "pycore_unionobject.h"
typevarobject;
typevartupleobject;
paramspecobject;
typealiasobject;
#include "clinic/typevarobject.c.h"
static PyObject *
NoDefault_repr(PyObject *op)
{ … }
static PyObject *
NoDefault_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
static PyMethodDef nodefault_methods[] = …;
static PyObject *
nodefault_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ … }
static void
nodefault_dealloc(PyObject *nodefault)
{ … }
PyDoc_STRVAR(nodefault_doc,
"NoDefaultType()\n"
"--\n\n"
"The type of the NoDefault singleton.");
PyTypeObject _PyNoDefault_Type = …;
PyObject _Py_NoDefaultStruct = …;
constevaluatorobject;
static void
constevaluator_dealloc(PyObject *self)
{ … }
static int
constevaluator_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
static int
constevaluator_clear(PyObject *self)
{ … }
static PyObject *
constevaluator_repr(PyObject *self)
{ … }
static PyObject *
constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs)
{ … }
static PyObject *
constevaluator_alloc(PyObject *value)
{ … }
PyDoc_STRVAR(constevaluator_doc,
"_ConstEvaluator()\n"
"--\n\n"
"Internal type for implementing evaluation functions.");
static PyType_Slot constevaluator_slots[] = …;
PyType_Spec constevaluator_spec = …;
int
_Py_typing_type_repr(PyUnicodeWriter *writer, PyObject *p)
{ … }
static PyObject *
call_typing_func_object(const char *name, PyObject **args, size_t nargs)
{ … }
static PyObject *
type_check(PyObject *arg, const char *msg)
{ … }
static PyObject *
make_union(PyObject *self, PyObject *other)
{ … }
static PyObject *
caller(void)
{ … }
static PyObject *
typevartuple_unpack(PyObject *tvt)
{ … }
static int
contains_typevartuple(PyTupleObject *params)
{ … }
static PyObject *
unpack_typevartuples(PyObject *params)
{ … }
static void
typevar_dealloc(PyObject *self)
{ … }
static int
typevar_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
static int
typevar_clear(typevarobject *self)
{ … }
static PyObject *
typevar_repr(PyObject *self)
{ … }
static PyMemberDef typevar_members[] = …;
static PyObject *
typevar_bound(typevarobject *self, void *Py_UNUSED(ignored))
{ … }
static PyObject *
typevar_default(typevarobject *self, void *unused)
{ … }
static PyObject *
typevar_constraints(typevarobject *self, void *Py_UNUSED(ignored))
{ … }
static PyObject *
typevar_evaluate_bound(typevarobject *self, void *Py_UNUSED(ignored))
{ … }
static PyObject *
typevar_evaluate_constraints(typevarobject *self, void *Py_UNUSED(ignored))
{ … }
static PyObject *
typevar_evaluate_default(typevarobject *self, void *Py_UNUSED(ignored))
{ … }
static PyGetSetDef typevar_getset[] = …;
static typevarobject *
typevar_alloc(PyObject *name, PyObject *bound, PyObject *evaluate_bound,
PyObject *constraints, PyObject *evaluate_constraints,
PyObject *default_value,
bool covariant, bool contravariant, bool infer_variance,
PyObject *module)
{ … }
static PyObject *
typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
PyObject *bound, PyObject *default_value, int covariant,
int contravariant, int infer_variance)
{ … }
static PyObject *
typevar_typing_subst(typevarobject *self, PyObject *arg)
{ … }
static PyObject *
typevar_typing_prepare_subst_impl(typevarobject *self, PyObject *alias,
PyObject *args)
{ … }
static PyObject *
typevar_reduce_impl(typevarobject *self)
{ … }
static PyObject *
typevar_has_default_impl(typevarobject *self)
{ … }
static PyObject *
typevar_mro_entries(PyObject *self, PyObject *args)
{ … }
static PyMethodDef typevar_methods[] = …;
PyDoc_STRVAR(typevar_doc,
"Type variable.\n\
\n\
The preferred way to construct a type variable is via the dedicated\n\
syntax for generic functions, classes, and type aliases::\n\
\n\
class Sequence[T]: # T is a TypeVar\n\
...\n\
\n\
This syntax can also be used to create bound and constrained type\n\
variables::\n\
\n\
# S is a TypeVar bound to str\n\
class StrSequence[S: str]:\n\
...\n\
\n\
# A is a TypeVar constrained to str or bytes\n\
class StrOrBytesSequence[A: (str, bytes)]:\n\
...\n\
\n\
Type variables can also have defaults:\n\
\n\
class IntDefault[T = int]:\n\
...\n\
\n\
However, if desired, reusable type variables can also be constructed\n\
manually, like so::\n\
\n\
T = TypeVar('T') # Can be anything\n\
S = TypeVar('S', bound=str) # Can be any subtype of str\n\
A = TypeVar('A', str, bytes) # Must be exactly str or bytes\n\
D = TypeVar('D', default=int) # Defaults to int\n\
\n\
Type variables exist primarily for the benefit of static type\n\
checkers. They serve as the parameters for generic types as well\n\
as for generic function and type alias definitions.\n\
\n\
The variance of type variables is inferred by type checkers when they\n\
are created through the type parameter syntax and when\n\
``infer_variance=True`` is passed. Manually created type variables may\n\
be explicitly marked covariant or contravariant by passing\n\
``covariant=True`` or ``contravariant=True``. By default, manually\n\
created type variables are invariant. See PEP 484 and PEP 695 for more\n\
details.\n\
");
static PyType_Slot typevar_slots[] = …;
PyType_Spec typevar_spec = …;
paramspecattrobject;
static void
paramspecattr_dealloc(PyObject *self)
{ … }
static int
paramspecattr_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
static int
paramspecattr_clear(paramspecattrobject *self)
{ … }
static PyObject *
paramspecattr_richcompare(PyObject *a, PyObject *b, int op)
{ … }
static PyMemberDef paramspecattr_members[] = …;
static paramspecattrobject *
paramspecattr_new(PyTypeObject *tp, PyObject *origin)
{ … }
static PyObject *
paramspecargs_repr(PyObject *self)
{ … }
static PyObject *
paramspecargs_new_impl(PyTypeObject *type, PyObject *origin)
{ … }
static PyObject *
paramspecargs_mro_entries(PyObject *self, PyObject *args)
{ … }
static PyMethodDef paramspecargs_methods[] = …;
PyDoc_STRVAR(paramspecargs_doc,
"The args for a ParamSpec object.\n\
\n\
Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.\n\
\n\
ParamSpecArgs objects have a reference back to their ParamSpec::\n\
\n\
>>> P = ParamSpec(\"P\")\n\
>>> P.args.__origin__ is P\n\
True\n\
\n\
This type is meant for runtime introspection and has no special meaning\n\
to static type checkers.\n\
");
static PyType_Slot paramspecargs_slots[] = …;
PyType_Spec paramspecargs_spec = …;
static PyObject *
paramspeckwargs_repr(PyObject *self)
{ … }
static PyObject *
paramspeckwargs_new_impl(PyTypeObject *type, PyObject *origin)
{ … }
static PyObject *
paramspeckwargs_mro_entries(PyObject *self, PyObject *args)
{ … }
static PyMethodDef paramspeckwargs_methods[] = …;
PyDoc_STRVAR(paramspeckwargs_doc,
"The kwargs for a ParamSpec object.\n\
\n\
Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.\n\
\n\
ParamSpecKwargs objects have a reference back to their ParamSpec::\n\
\n\
>>> P = ParamSpec(\"P\")\n\
>>> P.kwargs.__origin__ is P\n\
True\n\
\n\
This type is meant for runtime introspection and has no special meaning\n\
to static type checkers.\n\
");
static PyType_Slot paramspeckwargs_slots[] = …;
PyType_Spec paramspeckwargs_spec = …;
static void
paramspec_dealloc(PyObject *self)
{ … }
static int
paramspec_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
static int
paramspec_clear(paramspecobject *self)
{ … }
static PyObject *
paramspec_repr(PyObject *self)
{ … }
static PyMemberDef paramspec_members[] = …;
static PyObject *
paramspec_args(PyObject *self, void *unused)
{ … }
static PyObject *
paramspec_kwargs(PyObject *self, void *unused)
{ … }
static PyObject *
paramspec_default(paramspecobject *self, void *unused)
{ … }
static PyObject *
paramspec_evaluate_default(paramspecobject *self, void *unused)
{ … }
static PyGetSetDef paramspec_getset[] = …;
static paramspecobject *
paramspec_alloc(PyObject *name, PyObject *bound, PyObject *default_value, bool covariant,
bool contravariant, bool infer_variance, PyObject *module)
{ … }
static PyObject *
paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
PyObject *default_value, int covariant, int contravariant,
int infer_variance)
{ … }
static PyObject *
paramspec_typing_subst(paramspecobject *self, PyObject *arg)
{ … }
static PyObject *
paramspec_typing_prepare_subst_impl(paramspecobject *self, PyObject *alias,
PyObject *args)
{ … }
static PyObject *
paramspec_reduce_impl(paramspecobject *self)
{ … }
static PyObject *
paramspec_has_default_impl(paramspecobject *self)
{ … }
static PyObject *
paramspec_mro_entries(PyObject *self, PyObject *args)
{ … }
static PyMethodDef paramspec_methods[] = …;
PyDoc_STRVAR(paramspec_doc,
"Parameter specification variable.\n\
\n\
The preferred way to construct a parameter specification is via the\n\
dedicated syntax for generic functions, classes, and type aliases,\n\
where the use of '**' creates a parameter specification::\n\
\n\
type IntFunc[**P] = Callable[P, int]\n\
\n\
The following syntax creates a parameter specification that defaults\n\
to a callable accepting two positional-only arguments of types int\n\
and str:\n\
\n\
type IntFuncDefault[**P = (int, str)] = Callable[P, int]\n\
\n\
For compatibility with Python 3.11 and earlier, ParamSpec objects\n\
can also be created as follows::\n\
\n\
P = ParamSpec('P')\n\
DefaultP = ParamSpec('DefaultP', default=(int, str))\n\
\n\
Parameter specification variables exist primarily for the benefit of\n\
static type checkers. They are used to forward the parameter types of\n\
one callable to another callable, a pattern commonly found in\n\
higher-order functions and decorators. They are only valid when used\n\
in ``Concatenate``, or as the first argument to ``Callable``, or as\n\
parameters for user-defined Generics. See class Generic for more\n\
information on generic types.\n\
\n\
An example for annotating a decorator::\n\
\n\
def add_logging[**P, T](f: Callable[P, T]) -> Callable[P, T]:\n\
'''A type-safe decorator to add logging to a function.'''\n\
def inner(*args: P.args, **kwargs: P.kwargs) -> T:\n\
logging.info(f'{f.__name__} was called')\n\
return f(*args, **kwargs)\n\
return inner\n\
\n\
@add_logging\n\
def add_two(x: float, y: float) -> float:\n\
'''Add two numbers together.'''\n\
return x + y\n\
\n\
Parameter specification variables can be introspected. e.g.::\n\
\n\
>>> P = ParamSpec(\"P\")\n\
>>> P.__name__\n\
'P'\n\
\n\
Note that only parameter specification variables defined in the global\n\
scope can be pickled.\n\
");
static PyType_Slot paramspec_slots[] = …;
PyType_Spec paramspec_spec = …;
static void
typevartuple_dealloc(PyObject *self)
{ … }
static PyObject *
typevartuple_iter(PyObject *self)
{ … }
static PyObject *
typevartuple_repr(PyObject *self)
{ … }
static PyMemberDef typevartuple_members[] = …;
static typevartupleobject *
typevartuple_alloc(PyObject *name, PyObject *module, PyObject *default_value)
{ … }
static PyObject *
typevartuple_impl(PyTypeObject *type, PyObject *name,
PyObject *default_value)
{ … }
static PyObject *
typevartuple_typing_subst(typevartupleobject *self, PyObject *arg)
{ … }
static PyObject *
typevartuple_typing_prepare_subst_impl(typevartupleobject *self,
PyObject *alias, PyObject *args)
{ … }
static PyObject *
typevartuple_reduce_impl(typevartupleobject *self)
{ … }
static PyObject *
typevartuple_has_default_impl(typevartupleobject *self)
{ … }
static PyObject *
typevartuple_mro_entries(PyObject *self, PyObject *args)
{ … }
static int
typevartuple_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
static int
typevartuple_clear(PyObject *self)
{ … }
static PyObject *
typevartuple_default(typevartupleobject *self, void *unused)
{ … }
static PyObject *
typevartuple_evaluate_default(typevartupleobject *self, void *unused)
{ … }
static PyGetSetDef typevartuple_getset[] = …;
static PyMethodDef typevartuple_methods[] = …;
PyDoc_STRVAR(typevartuple_doc,
"Type variable tuple. A specialized form of type variable that enables\n\
variadic generics.\n\
\n\
The preferred way to construct a type variable tuple is via the\n\
dedicated syntax for generic functions, classes, and type aliases,\n\
where a single '*' indicates a type variable tuple::\n\
\n\
def move_first_element_to_last[T, *Ts](tup: tuple[T, *Ts]) -> tuple[*Ts, T]:\n\
return (*tup[1:], tup[0])\n\
\n\
Type variables tuples can have default values:\n\
\n\
type AliasWithDefault[*Ts = (str, int)] = tuple[*Ts]\n\
\n\
For compatibility with Python 3.11 and earlier, TypeVarTuple objects\n\
can also be created as follows::\n\
\n\
Ts = TypeVarTuple('Ts') # Can be given any name\n\
DefaultTs = TypeVarTuple('Ts', default=(str, int))\n\
\n\
Just as a TypeVar (type variable) is a placeholder for a single type,\n\
a TypeVarTuple is a placeholder for an *arbitrary* number of types. For\n\
example, if we define a generic class using a TypeVarTuple::\n\
\n\
class C[*Ts]: ...\n\
\n\
Then we can parameterize that class with an arbitrary number of type\n\
arguments::\n\
\n\
C[int] # Fine\n\
C[int, str] # Also fine\n\
C[()] # Even this is fine\n\
\n\
For more details, see PEP 646.\n\
\n\
Note that only TypeVarTuples defined in the global scope can be\n\
pickled.\n\
");
PyType_Slot typevartuple_slots[] = …;
PyType_Spec typevartuple_spec = …;
PyObject *
_Py_make_typevar(PyObject *name, PyObject *evaluate_bound, PyObject *evaluate_constraints)
{ … }
PyObject *
_Py_make_paramspec(PyThreadState *Py_UNUSED(ignored), PyObject *v)
{ … }
PyObject *
_Py_make_typevartuple(PyThreadState *Py_UNUSED(ignored), PyObject *v)
{ … }
static PyObject *
get_type_param_default(PyThreadState *ts, PyObject *typeparam) { … }
static void
typealias_dealloc(PyObject *self)
{ … }
static PyObject *
typealias_get_value(typealiasobject *ta)
{ … }
static PyObject *
typealias_repr(PyObject *self)
{ … }
static PyMemberDef typealias_members[] = …;
static PyObject *
typealias_value(PyObject *self, void *unused)
{ … }
static PyObject *
typealias_evaluate_value(PyObject *self, void *unused)
{ … }
static PyObject *
typealias_parameters(PyObject *self, void *unused)
{ … }
static PyObject *
typealias_type_params(PyObject *self, void *unused)
{ … }
static PyObject *
typealias_module(PyObject *self, void *unused)
{ … }
static PyGetSetDef typealias_getset[] = …;
static PyObject *
typealias_check_type_params(PyObject *type_params, int *err) { … }
static PyObject *
typelias_convert_type_params(PyObject *type_params)
{ … }
static typealiasobject *
typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
PyObject *value, PyObject *module)
{ … }
static int
typealias_traverse(typealiasobject *self, visitproc visit, void *arg)
{ … }
static int
typealias_clear(typealiasobject *self)
{ … }
static PyObject *
typealias_reduce_impl(typealiasobject *self)
{ … }
static PyObject *
typealias_subscript(PyObject *self, PyObject *args)
{ … }
static PyMethodDef typealias_methods[] = …;
static PyObject *
typealias_new_impl(PyTypeObject *type, PyObject *name, PyObject *value,
PyObject *type_params)
{ … }
PyDoc_STRVAR(typealias_doc,
"Type alias.\n\
\n\
Type aliases are created through the type statement::\n\
\n\
type Alias = int\n\
\n\
In this example, Alias and int will be treated equivalently by static\n\
type checkers.\n\
\n\
At runtime, Alias is an instance of TypeAliasType. The __name__\n\
attribute holds the name of the type alias. The value of the type alias\n\
is stored in the __value__ attribute. It is evaluated lazily, so the\n\
value is computed only if the attribute is accessed.\n\
\n\
Type aliases can also be generic::\n\
\n\
type ListOrSet[T] = list[T] | set[T]\n\
\n\
In this case, the type parameters of the alias are stored in the\n\
__type_params__ attribute.\n\
\n\
See PEP 695 for more information.\n\
");
static PyNumberMethods typealias_as_number = …;
static PyMappingMethods typealias_as_mapping = …;
PyTypeObject _PyTypeAlias_Type = …;
PyObject *
_Py_make_typealias(PyThreadState* unused, PyObject *args)
{ … }
PyDoc_STRVAR(generic_doc,
"Abstract base class for generic types.\n\
\n\
On Python 3.12 and newer, generic classes implicitly inherit from\n\
Generic when they declare a parameter list after the class's name::\n\
\n\
class Mapping[KT, VT]:\n\
def __getitem__(self, key: KT) -> VT:\n\
...\n\
# Etc.\n\
\n\
On older versions of Python, however, generic classes have to\n\
explicitly inherit from Generic.\n\
\n\
After a class has been declared to be generic, it can then be used as\n\
follows::\n\
\n\
def lookup_name[KT, VT](mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:\n\
try:\n\
return mapping[key]\n\
except KeyError:\n\
return default\n\
");
PyDoc_STRVAR(generic_class_getitem_doc,
"Parameterizes a generic class.\n\
\n\
At least, parameterizing a generic class is the *main* thing this\n\
method does. For example, for some generic class `Foo`, this is called\n\
when we do `Foo[int]` - there, with `cls=Foo` and `params=int`.\n\
\n\
However, note that this method is also called when defining generic\n\
classes in the first place with `class Foo[T]: ...`.\n\
");
static PyObject *
call_typing_args_kwargs(const char *name, PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{ … }
static PyObject *
generic_init_subclass(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{ … }
static PyObject *
generic_class_getitem(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{ … }
PyObject *
_Py_subscript_generic(PyThreadState* unused, PyObject *params)
{ … }
static PyMethodDef generic_methods[] = …;
static void
generic_dealloc(PyObject *self)
{ … }
static int
generic_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
static PyType_Slot generic_slots[] = …;
PyType_Spec generic_spec = …;
int _Py_initialize_generic(PyInterpreterState *interp)
{ … }
void _Py_clear_generic_types(PyInterpreterState *interp)
{ … }
PyObject *
_Py_set_typeparam_default(PyThreadState *ts, PyObject *typeparam, PyObject *evaluate_default)
{ … }