An instance of a Python set.
| 3651 | |
| 3652 | |
| 3653 | class Set(Container[t.Set[t.Any]]): |
| 3654 | """An instance of a Python set.""" |
| 3655 | |
| 3656 | klass = set |
| 3657 | _cast_types = (tuple, list) |
| 3658 | |
| 3659 | _literal_from_string_pairs = ("[]", "()", "{}") |
| 3660 | |
| 3661 | # Redefine __init__ just to make the docstring more accurate. |
| 3662 | def __init__( |
| 3663 | self, |
| 3664 | trait: t.Any = None, |
| 3665 | default_value: t.Any = Undefined, |
| 3666 | minlen: int = 0, |
| 3667 | maxlen: int = sys.maxsize, |
| 3668 | **kwargs: t.Any, |
| 3669 | ) -> None: |
| 3670 | """Create a Set trait type from a list, set, or tuple. |
| 3671 | |
| 3672 | The default value is created by doing ``set(default_value)``, |
| 3673 | which creates a copy of the ``default_value``. |
| 3674 | |
| 3675 | ``trait`` can be specified, which restricts the type of elements |
| 3676 | in the container to that TraitType. |
| 3677 | |
| 3678 | If only one arg is given and it is not a Trait, it is taken as |
| 3679 | ``default_value``: |
| 3680 | |
| 3681 | ``c = Set({1, 2, 3})`` |
| 3682 | |
| 3683 | Parameters |
| 3684 | ---------- |
| 3685 | trait : TraitType [ optional ] |
| 3686 | the type for restricting the contents of the Container. |
| 3687 | If unspecified, types are not checked. |
| 3688 | default_value : SequenceType [ optional ] |
| 3689 | The default value for the Trait. Must be list/tuple/set, and |
| 3690 | will be cast to the container type. |
| 3691 | minlen : Int [ default 0 ] |
| 3692 | The minimum length of the input list |
| 3693 | maxlen : Int [ default sys.maxsize ] |
| 3694 | The maximum length of the input list |
| 3695 | """ |
| 3696 | self._maxlen = maxlen |
| 3697 | self._minlen = minlen |
| 3698 | super().__init__(trait=trait, default_value=default_value, **kwargs) |
| 3699 | |
| 3700 | def length_error(self, obj: t.Any, value: t.Any) -> None: |
| 3701 | e = ( |
| 3702 | "The '%s' trait of %s instance must be of length %i <= L <= %i, but a value of %s was specified." |
| 3703 | % (self.name, class_of(obj), self._minlen, self._maxlen, value) |
| 3704 | ) |
| 3705 | raise TraitError(e) |
| 3706 | |
| 3707 | def validate_elements(self, obj: t.Any, value: t.Any) -> t.Any: |
| 3708 | length = len(value) |
| 3709 | if length < self._minlen or length > self._maxlen: |
| 3710 | self.length_error(obj, value) |
no outgoing calls