Represents a parameter in a function signature. Has the following public attributes: * name : str The name of the parameter as a string. * default : object The default value for the parameter if specified. If the parameter has no default value, this attribute i
| 2652 | |
| 2653 | |
| 2654 | class Parameter: |
| 2655 | """Represents a parameter in a function signature. |
| 2656 | |
| 2657 | Has the following public attributes: |
| 2658 | |
| 2659 | * name : str |
| 2660 | The name of the parameter as a string. |
| 2661 | * default : object |
| 2662 | The default value for the parameter if specified. If the |
| 2663 | parameter has no default value, this attribute is set to |
| 2664 | `Parameter.empty`. |
| 2665 | * annotation |
| 2666 | The annotation for the parameter if specified. If the |
| 2667 | parameter has no annotation, this attribute is set to |
| 2668 | `Parameter.empty`. |
| 2669 | * kind |
| 2670 | Describes how argument values are bound to the parameter. |
| 2671 | Possible values: `Parameter.POSITIONAL_ONLY`, |
| 2672 | `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`, |
| 2673 | `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`. |
| 2674 | Every value has a `description` attribute describing meaning. |
| 2675 | """ |
| 2676 | |
| 2677 | __slots__ = ('_name', '_kind', '_default', '_annotation') |
| 2678 | |
| 2679 | POSITIONAL_ONLY = _POSITIONAL_ONLY |
| 2680 | POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD |
| 2681 | VAR_POSITIONAL = _VAR_POSITIONAL |
| 2682 | KEYWORD_ONLY = _KEYWORD_ONLY |
| 2683 | VAR_KEYWORD = _VAR_KEYWORD |
| 2684 | |
| 2685 | empty = _empty |
| 2686 | |
| 2687 | def __init__(self, name, kind, *, default=_empty, annotation=_empty): |
| 2688 | try: |
| 2689 | self._kind = _ParameterKind(kind) |
| 2690 | except ValueError: |
| 2691 | raise ValueError(f'value {kind!r} is not a valid Parameter.kind') |
| 2692 | if default is not _empty: |
| 2693 | if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD): |
| 2694 | msg = '{} parameters cannot have default values' |
| 2695 | msg = msg.format(self._kind.description) |
| 2696 | raise ValueError(msg) |
| 2697 | self._default = default |
| 2698 | self._annotation = annotation |
| 2699 | |
| 2700 | if name is _empty: |
| 2701 | raise ValueError('name is a required attribute for Parameter') |
| 2702 | |
| 2703 | if not isinstance(name, str): |
| 2704 | msg = 'name must be a str, not a {}'.format(type(name).__name__) |
| 2705 | raise TypeError(msg) |
| 2706 | |
| 2707 | if name[0] == '.' and name[1:].isdigit(): |
| 2708 | # These are implicit arguments generated by comprehensions. In |
| 2709 | # order to provide a friendlier interface to users, we recast |
| 2710 | # their name as "implicitN" and treat them as positional-only. |
| 2711 | # See issue 19611. |
no outgoing calls
searching dependent graphs…