GithubHelp home page GithubHelp logo

ordered-set-37's People

Contributors

bustawin avatar changaco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

changaco

ordered-set-37's Issues

discard(elem) raises KeyError on missing element

Hi,

according to the Python documentation of Set Types, the difference between remove(elem) and discard(elem) is that the former raises a KeyError if elemis not a member of the set whereas the latter is a no-op in this case.

Unfortunately, in ordered-set-37 version 1.0, both OrderedSet.remove(elem) and OrderedSet.discard(elem) raise a KeyError if the argument is not in the set. Test case:

import pytest
from ordered_set_37 import OrderedSet


def test_discard_deletes_existing_element():
    x = OrderedSet([1, 2, -1])
    x.discard(2)
    assert list(x) == [1, -1]

def test_discard_ignores_missing_element():
    x = OrderedSet([1, 2, -1])
    x.discard(3)
    assert list(x) == [1, 2, -1]

def test_remove_deletes_existing_element():
    x = OrderedSet([1, 2, -1])
    x.remove(2)
    assert list(x) == [1, -1]

def test_remove_raises_key_error_on_missing_element():
    x = OrderedSet([1, 2, -1])
    with pytest.raises(KeyError):
        x.remove(3)

    # x is not modified
    assert list(x) == [1, 2, -1]

Abbreviated test output:

================ FAILURES =================
____ test_discard_ignores_missing_element____

    def test_discard_ignores_missing_element():
        x = OrderedSet([1, 2, -1])
>       x.discard(3)

scratch_1.py:13:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <OrderedSet {1, 2, -1}>, x = 3

    def discard(self, x: T) -> None:
>       self._d.pop(x)
E       KeyError: 3

../../.pyenv/versions/3.8.1/envs/ong-mono/lib/python3.8/site-packages/ordered_set_37/__init__.py:20: KeyError
=========================================

Proposed fix: Add an (ignored) default argument to the call of dict.pop(x) in line 20 of ordered_set_37.__init__.py, e.g.:

    def discard(self, x: T) -> None:
        self._d.pop(x, None)

StableSet: Pointing to a new implementation

Hi,

I wanted to point out that I made an implementation of OrderedSet that uses the same foundations as your implementation but adds many more features.
(I called it StableSet, as OrderedSet was a name for a different implementation) .
I also used your tests to test full compatibility between your implementation and 2 other implementations.

Please have a look if you are interested:
https://pypi.org/project/stableset/

I made a PR to merge it upstream:
https://github.com/rspeer/ordered-set/pull/92/files

Initializer parameter type hint: Iterator or Iterable?

Hi,

I am wondering about the signature of OrderedSet.__init__(self, iterable: t.Optional[t.Iterator[T]] = None). Why did you declare iterable to be an Iterator[T] (or None) instead of an Iterable[T]? dict.from_keys() (which you call in your implementation) accepts any Iterable[T].

Not only does the parameter name suggest this was an oversight, but collections.abc.Iterator also inherits from collections.abc.Iterable; the former is therefore the more specialized type.

The collection classes are iterables but not iterator instances. In fact, initializing OrderedSet from a list, set, or even another OrderedSet instance works fine but makes mypy complain:

$ mypy ~/.pyenv/versions/ong-mono/lib/python3.8/site-packages/ordered_set_37/__init__.py ~/Library/Application\ Support/Jetbrains/PyCharm2020.2/scratches/scratch.py
/Users/ludwigc/Library/Application Support/Jetbrains/PyCharm2020.2/scratches/scratch.py:3: error: Argument 1 to "OrderedSet" has incompatible type "List[str]"; expected "Optional[Iterator[str]]"
/Users/ludwigc/Library/Application Support/Jetbrains/PyCharm2020.2/scratches/scratch.py:5: error: Argument 1 to "OrderedSet" has incompatible type "OrderedSet[str]"; expected "Optional[Iterator[<nothing>]]"
Found 2 errors in 1 file (checked 2 source files)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.