|
114 | 114 | 'no_type_check_decorator', |
115 | 115 | 'NoReturn', |
116 | 116 | 'overload', |
| 117 | + 'ParamSpecArgs', |
| 118 | + 'ParamSpecKwargs', |
117 | 119 | 'runtime_checkable', |
118 | 120 | 'Text', |
119 | 121 | 'TYPE_CHECKING', |
@@ -727,6 +729,44 @@ def __init__(self, name, *constraints, bound=None, |
727 | 729 | self.__module__ = def_mod |
728 | 730 |
|
729 | 731 |
|
| 732 | +class ParamSpecArgs(_Final, _Immutable, _root=True): |
| 733 | + """The args for a ParamSpec object. |
| 734 | +
|
| 735 | + Given a ParamSpec object P, P.args is an instance of ParamSpecArgs. |
| 736 | +
|
| 737 | + ParamSpecArgs objects have a reference back to their ParamSpec: |
| 738 | +
|
| 739 | + P.args.__origin__ is P |
| 740 | +
|
| 741 | + This type is meant for runtime introspection and has no special meaning to |
| 742 | + static type checkers. |
| 743 | + """ |
| 744 | + def __init__(self, origin): |
| 745 | + self.__origin__ = origin |
| 746 | + |
| 747 | + def __repr__(self): |
| 748 | + return f"{self.__origin__.__name__}.args" |
| 749 | + |
| 750 | + |
| 751 | +class ParamSpecKwargs(_Final, _Immutable, _root=True): |
| 752 | + """The kwargs for a ParamSpec object. |
| 753 | +
|
| 754 | + Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs. |
| 755 | +
|
| 756 | + ParamSpecKwargs objects have a reference back to their ParamSpec: |
| 757 | +
|
| 758 | + P.kwargs.__origin__ is P |
| 759 | +
|
| 760 | + This type is meant for runtime introspection and has no special meaning to |
| 761 | + static type checkers. |
| 762 | + """ |
| 763 | + def __init__(self, origin): |
| 764 | + self.__origin__ = origin |
| 765 | + |
| 766 | + def __repr__(self): |
| 767 | + return f"{self.__origin__.__name__}.kwargs" |
| 768 | + |
| 769 | + |
730 | 770 | class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True): |
731 | 771 | """Parameter specification variable. |
732 | 772 |
|
@@ -776,8 +816,13 @@ def add_two(x: float, y: float) -> float: |
776 | 816 | __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__', |
777 | 817 | '__dict__') |
778 | 818 |
|
779 | | - args = object() |
780 | | - kwargs = object() |
| 819 | + @property |
| 820 | + def args(self): |
| 821 | + return ParamSpecArgs(self) |
| 822 | + |
| 823 | + @property |
| 824 | + def kwargs(self): |
| 825 | + return ParamSpecKwargs(self) |
781 | 826 |
|
782 | 827 | def __init__(self, name, *, bound=None, covariant=False, contravariant=False): |
783 | 828 | self.__name__ = name |
@@ -1662,10 +1707,12 @@ def get_origin(tp): |
1662 | 1707 | get_origin(Generic[T]) is Generic |
1663 | 1708 | get_origin(Union[T, int]) is Union |
1664 | 1709 | get_origin(List[Tuple[T, T]][int]) == list |
| 1710 | + get_origin(P.args) is P |
1665 | 1711 | """ |
1666 | 1712 | if isinstance(tp, _AnnotatedAlias): |
1667 | 1713 | return Annotated |
1668 | | - if isinstance(tp, (_BaseGenericAlias, GenericAlias)): |
| 1714 | + if isinstance(tp, (_BaseGenericAlias, GenericAlias, |
| 1715 | + ParamSpecArgs, ParamSpecKwargs)): |
1669 | 1716 | return tp.__origin__ |
1670 | 1717 | if tp is Generic: |
1671 | 1718 | return Generic |
|
0 commit comments