diff --git a/doc/spec.md b/doc/spec.md index 3d1cf0d3..c98ee173 100644 --- a/doc/spec.md +++ b/doc/spec.md @@ -3759,20 +3759,20 @@ x.remove(2) # error: element not found If `x` is not an element of set `S`, `S.add(x)` adds it to the set or fails if the set is frozen. If `x` already an element of the set, `add(x)` has no effect. -`add` fails if the set does not contain `x` and is frozen. - It returns None. ```python x = set([1, 2]) -x.add(3) # None (x == set([1, 2, 3])) -x.add(3) # None (x == set([1, 2, 3])) +x.add(3) # None +x # set([1, 2, 3]) +x.add(3) # None +x # set([1, 2, 3]) ``` ### set·clear -`S.clear()` removes all items from the set and fails if the set is non-empty and frozen. +`S.clear()` removes all items from the set or fails if the set is non-empty and frozen. It returns None. @@ -3803,7 +3803,7 @@ x # set([1, 3]) `S.pop()` removes the first inserted item from the set and returns it. -`pop` fails if the set is empty or is frozen. +`pop` fails if the set is empty or frozen. ```python x = set([1, 2]) diff --git a/starlark/library.go b/starlark/library.go index c85bf4b0..0e3c7bf7 100644 --- a/starlark/library.go +++ b/starlark/library.go @@ -2196,12 +2196,10 @@ func set_clear(_ *Thread, b *Builtin, args Tuple, kwargs []Tuple) (Value, error) if err := UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil { return nil, err } - if b.Receiver().(*Set).Len() == 0 { // clear on an empty set is non-mutating - return None, nil - } - err := b.Receiver().(*Set).Clear() - if err != nil { - return nil, nameErr(b, err) + if b.Receiver().(*Set).Len() > 0 { + if err := b.Receiver().(*Set).Clear(); err != nil { + return nil, nameErr(b, err) + } } return None, nil } @@ -2218,7 +2216,7 @@ func set_discard(_ *Thread, b *Builtin, args Tuple, kwargs []Tuple) (Value, erro return None, nil } if _, err := b.Receiver().(*Set).Delete(k); err != nil { - return nil, nameErr(b, err) // dict is frozen or key is unhashable + return nil, nameErr(b, err) // set is frozen } return None, nil }