Skip to content

Commit 7aeee08

Browse files
committed
Add docs to typevarobject.c
1 parent fa81c01 commit 7aeee08

File tree

1 file changed

+146
-4
lines changed

1 file changed

+146
-4
lines changed

Objects/typevarobject.c

Lines changed: 146 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,50 @@ static PyNumberMethods typevar_as_number = {
250250
.nb_or = _Py_union_type_or, // Add __or__ function
251251
};
252252

253+
PyDoc_STRVAR(typevar_doc,
254+
"Type variable.\n\
255+
\n\
256+
Usage::\n\
257+
\n\
258+
T = TypeVar('T') # Can be anything\n\
259+
A = TypeVar('A', str, bytes) # Must be str or bytes\n\
260+
\n\
261+
Type variables exist primarily for the benefit of static type\n\
262+
checkers. They serve as the parameters for generic types as well\n\
263+
as for generic function definitions. See class Generic for more\n\
264+
information on generic types. Generic functions work as follows:\n\
265+
\n\
266+
def repeat(x: T, n: int) -> List[T]:\n\
267+
'''Return a list containing n references to x.'''\n\
268+
return [x]*n\n\
269+
\n\
270+
def longest(x: A, y: A) -> A:\n\
271+
'''Return the longest of two strings.'''\n\
272+
return x if len(x) >= len(y) else y\n\
273+
\n\
274+
The latter example's signature is essentially the overloading\n\
275+
of (str, str) -> str and (bytes, bytes) -> bytes. Also note\n\
276+
that if the arguments are instances of some subclass of str,\n\
277+
the return type is still plain str.\n\
278+
\n\
279+
At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.\n\
280+
\n\
281+
Type variables defined with covariant=True or contravariant=True\n\
282+
can be used to declare covariant or contravariant generic types.\n\
283+
See PEP 484 for more details. By default generic types are invariant\n\
284+
in all type variables.\n\
285+
\n\
286+
Type variables can be introspected. e.g.:\n\
287+
\n\
288+
T.__name__ == 'T'\n\
289+
T.__constraints__ == ()\n\
290+
T.__covariant__ == False\n\
291+
T.__contravariant__ = False\n\
292+
A.__constraints__ == (str, bytes)\n\
293+
\n\
294+
Note that only type variables defined in global scope can be pickled.\n\
295+
");
296+
253297
PyTypeObject _PyTypeVar_Type = {
254298
PyVarObject_HEAD_INIT(&PyType_Type, 0)
255299
.tp_name = "typing.TypeVar",
@@ -264,6 +308,7 @@ PyTypeObject _PyTypeVar_Type = {
264308
.tp_methods = typevar_methods,
265309
.tp_new = typevar_new,
266310
.tp_as_number = &typevar_as_number,
311+
.tp_doc = typevar_doc,
267312
};
268313

269314
typedef struct {
@@ -347,6 +392,19 @@ paramspecargs_new_impl(PyTypeObject *type, PyObject *origin)
347392
return (PyObject *)paramspecargsobject_new(origin);
348393
}
349394

395+
PyDoc_STRVAR(paramspecargs_doc,
396+
"The args for a ParamSpec object.\n\
397+
\n\
398+
Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.\n\
399+
\n\
400+
ParamSpecArgs objects have a reference back to their ParamSpec:\n\
401+
\n\
402+
P.args.__origin__ is P\n\
403+
\n\
404+
This type is meant for runtime introspection and has no special meaning to\n\
405+
static type checkers.\n\
406+
");
407+
350408
PyTypeObject _PyParamSpecArgs_Type = {
351409
PyVarObject_HEAD_INIT(&PyType_Type, 0)
352410
.tp_name = "typing.ParamSpecArgs",
@@ -360,6 +418,7 @@ PyTypeObject _PyParamSpecArgs_Type = {
360418
.tp_traverse = paramspecargsobject_traverse,
361419
.tp_members = paramspecargs_members,
362420
.tp_new = paramspecargs_new,
421+
.tp_doc = paramspecargs_doc,
363422
};
364423

365424
typedef struct {
@@ -443,6 +502,19 @@ paramspeckwargs_new_impl(PyTypeObject *type, PyObject *origin)
443502
return (PyObject *)paramspeckwargsobject_new(origin);
444503
}
445504

505+
PyDoc_STRVAR(paramspeckwargs_doc,
506+
"The kwargs for a ParamSpec object.\n\
507+
\n\
508+
Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.\n\
509+
\n\
510+
ParamSpecKwargs objects have a reference back to their ParamSpec:\n\
511+
\n\
512+
P.kwargs.__origin__ is P\n\
513+
\n\
514+
This type is meant for runtime introspection and has no special meaning to\n\
515+
static type checkers.\n\
516+
");
517+
446518
PyTypeObject _PyParamSpecKwargs_Type = {
447519
PyVarObject_HEAD_INIT(&PyType_Type, 0)
448520
.tp_name = "typing.ParamSpecKwargs",
@@ -456,6 +528,7 @@ PyTypeObject _PyParamSpecKwargs_Type = {
456528
.tp_traverse = paramspeckwargsobject_traverse,
457529
.tp_members = paramspeckwargs_members,
458530
.tp_new = paramspeckwargs_new,
531+
.tp_doc = paramspeckwargs_doc,
459532
};
460533

461534
static void paramspecobject_dealloc(PyObject *self)
@@ -635,8 +708,52 @@ static PyNumberMethods paramspec_as_number = {
635708
.nb_or = _Py_union_type_or, // Add __or__ function
636709
};
637710

638-
// TODO:
639-
// - pickling
711+
PyDoc_STRVAR(paramspec_doc,
712+
"Parameter specification variable.\n\
713+
\n\
714+
Usage::\n\
715+
\n\
716+
P = ParamSpec('P')\n\
717+
\n\
718+
Parameter specification variables exist primarily for the benefit of static\n\
719+
type checkers. They are used to forward the parameter types of one\n\
720+
callable to another callable, a pattern commonly found in higher order\n\
721+
functions and decorators. They are only valid when used in ``Concatenate``,\n\
722+
or as the first argument to ``Callable``, or as parameters for user-defined\n\
723+
Generics. See class Generic for more information on generic types. An\n\
724+
example for annotating a decorator::\n\
725+
\n\
726+
T = TypeVar('T')\n\
727+
P = ParamSpec('P')\n\
728+
\n\
729+
def add_logging(f: Callable[P, T]) -> Callable[P, T]:\n\
730+
'''A type-safe decorator to add logging to a function.'''\n\
731+
def inner(*args: P.args, **kwargs: P.kwargs) -> T:\n\
732+
logging.info(f'{f.__name__} was called')\n\
733+
return f(*args, **kwargs)\n\
734+
return inner\n\
735+
\n\
736+
@add_logging\n\
737+
def add_two(x: float, y: float) -> float:\n\
738+
'''Add two numbers together.'''\n\
739+
return x + y\n\
740+
\n\
741+
Parameter specification variables defined with covariant=True or\n\
742+
contravariant=True can be used to declare covariant or contravariant\n\
743+
generic types. These keyword arguments are valid, but their actual semantics\n\
744+
are yet to be decided. See PEP 612 for details.\n\
745+
\n\
746+
Parameter specification variables can be introspected. e.g.:\n\
747+
\n\
748+
P.__name__ == 'P'\n\
749+
P.__bound__ == None\n\
750+
P.__covariant__ == False\n\
751+
P.__contravariant__ == False\n\
752+
\n\
753+
Note that only parameter specification variables defined in global scope can\n\
754+
be pickled.\n\
755+
");
756+
640757
PyTypeObject _PyParamSpec_Type = {
641758
PyVarObject_HEAD_INIT(&PyType_Type, 0)
642759
.tp_name = "typing.ParamSpec",
@@ -652,6 +769,7 @@ PyTypeObject _PyParamSpec_Type = {
652769
.tp_getset = paramspec_getset,
653770
.tp_new = paramspec_new,
654771
.tp_as_number = &paramspec_as_number,
772+
.tp_doc = paramspec_doc,
655773
};
656774

657775
static void typevartupleobject_dealloc(PyObject *self)
@@ -791,8 +909,31 @@ static PyMethodDef typevartuple_methods[] = {
791909
{0}
792910
};
793911

794-
// TODO:
795-
// - Pickling
912+
PyDoc_STRVAR(typevartuple_doc,
913+
"Type variable tuple.\n\
914+
\n\
915+
Usage:\n\
916+
\n\
917+
Ts = TypeVarTuple('Ts') # Can be given any name\n\
918+
\n\
919+
Just as a TypeVar (type variable) is a placeholder for a single type,\n\
920+
a TypeVarTuple is a placeholder for an *arbitrary* number of types. For\n\
921+
example, if we define a generic class using a TypeVarTuple:\n\
922+
\n\
923+
class C(Generic[*Ts]): ...\n\
924+
\n\
925+
Then we can parameterize that class with an arbitrary number of type\n\
926+
arguments:\n\
927+
\n\
928+
C[int] # Fine\n\
929+
C[int, str] # Also fine\n\
930+
C[()] # Even this is fine\n\
931+
\n\
932+
For more details, see PEP 646.\n\
933+
\n\
934+
Note that only TypeVarTuples defined in global scope can be pickled.\n\
935+
");
936+
796937
PyTypeObject _PyTypeVarTuple_Type = {
797938
PyVarObject_HEAD_INIT(&PyType_Type, 0)
798939
.tp_name = "typing.TypeVarTuple",
@@ -805,6 +946,7 @@ PyTypeObject _PyTypeVarTuple_Type = {
805946
.tp_members = typevartuple_members,
806947
.tp_methods = typevartuple_methods,
807948
.tp_new = typevartuple,
949+
.tp_doc = typevartuple_doc,
808950
};
809951

810952
PyObject *_Py_make_typevar(const char *name, PyObject *bound_or_constraints) {

0 commit comments

Comments
 (0)