@@ -250,6 +250,50 @@ static PyNumberMethods typevar_as_number = {
250
250
.nb_or = _Py_union_type_or , // Add __or__ function
251
251
};
252
252
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
+
253
297
PyTypeObject _PyTypeVar_Type = {
254
298
PyVarObject_HEAD_INIT (& PyType_Type , 0 )
255
299
.tp_name = "typing.TypeVar" ,
@@ -264,6 +308,7 @@ PyTypeObject _PyTypeVar_Type = {
264
308
.tp_methods = typevar_methods ,
265
309
.tp_new = typevar_new ,
266
310
.tp_as_number = & typevar_as_number ,
311
+ .tp_doc = typevar_doc ,
267
312
};
268
313
269
314
typedef struct {
@@ -347,6 +392,19 @@ paramspecargs_new_impl(PyTypeObject *type, PyObject *origin)
347
392
return (PyObject * )paramspecargsobject_new (origin );
348
393
}
349
394
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
+
350
408
PyTypeObject _PyParamSpecArgs_Type = {
351
409
PyVarObject_HEAD_INIT (& PyType_Type , 0 )
352
410
.tp_name = "typing.ParamSpecArgs" ,
@@ -360,6 +418,7 @@ PyTypeObject _PyParamSpecArgs_Type = {
360
418
.tp_traverse = paramspecargsobject_traverse ,
361
419
.tp_members = paramspecargs_members ,
362
420
.tp_new = paramspecargs_new ,
421
+ .tp_doc = paramspecargs_doc ,
363
422
};
364
423
365
424
typedef struct {
@@ -443,6 +502,19 @@ paramspeckwargs_new_impl(PyTypeObject *type, PyObject *origin)
443
502
return (PyObject * )paramspeckwargsobject_new (origin );
444
503
}
445
504
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
+
446
518
PyTypeObject _PyParamSpecKwargs_Type = {
447
519
PyVarObject_HEAD_INIT (& PyType_Type , 0 )
448
520
.tp_name = "typing.ParamSpecKwargs" ,
@@ -456,6 +528,7 @@ PyTypeObject _PyParamSpecKwargs_Type = {
456
528
.tp_traverse = paramspeckwargsobject_traverse ,
457
529
.tp_members = paramspeckwargs_members ,
458
530
.tp_new = paramspeckwargs_new ,
531
+ .tp_doc = paramspeckwargs_doc ,
459
532
};
460
533
461
534
static void paramspecobject_dealloc (PyObject * self )
@@ -635,8 +708,52 @@ static PyNumberMethods paramspec_as_number = {
635
708
.nb_or = _Py_union_type_or , // Add __or__ function
636
709
};
637
710
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
+
640
757
PyTypeObject _PyParamSpec_Type = {
641
758
PyVarObject_HEAD_INIT (& PyType_Type , 0 )
642
759
.tp_name = "typing.ParamSpec" ,
@@ -652,6 +769,7 @@ PyTypeObject _PyParamSpec_Type = {
652
769
.tp_getset = paramspec_getset ,
653
770
.tp_new = paramspec_new ,
654
771
.tp_as_number = & paramspec_as_number ,
772
+ .tp_doc = paramspec_doc ,
655
773
};
656
774
657
775
static void typevartupleobject_dealloc (PyObject * self )
@@ -791,8 +909,31 @@ static PyMethodDef typevartuple_methods[] = {
791
909
{0 }
792
910
};
793
911
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
+
796
937
PyTypeObject _PyTypeVarTuple_Type = {
797
938
PyVarObject_HEAD_INIT (& PyType_Type , 0 )
798
939
.tp_name = "typing.TypeVarTuple" ,
@@ -805,6 +946,7 @@ PyTypeObject _PyTypeVarTuple_Type = {
805
946
.tp_members = typevartuple_members ,
806
947
.tp_methods = typevartuple_methods ,
807
948
.tp_new = typevartuple ,
949
+ .tp_doc = typevartuple_doc ,
808
950
};
809
951
810
952
PyObject * _Py_make_typevar (const char * name , PyObject * bound_or_constraints ) {
0 commit comments