rpds_py-0.20.0/Cargo.toml000064400000000623000000000000010511ustar [package] name = "rpds-py" version = "0.20.0" edition = "2021" [lib] name = "rpds" crate-type = ["cdylib"] [dependencies] rpds = "1.1.0" archery = "1.2.0" [dependencies.pyo3] version = "0.22.2" # To build extension for PyPy on Windows, "generate-import-lib" is needed: # https://github.com/PyO3/maturin-action/issues/267#issuecomment-2106844429 features = ["extension-module", "generate-import-lib"] rpds_py-0.20.0/.github/SECURITY.md010064400017510000177000000011601465445241300145740ustar 00000000000000# Security Policy ## Supported Versions In general, only the latest released `rpds-py` version is supported and will receive updates. ## Reporting a Vulnerability To report a security vulnerability, please send an email to `Julian+Security` at `GrayVines.com` with subject line `SECURITY (rpds-py)`. I will do my best to respond within 48 hours to acknowledge the message and discuss further steps. If the vulnerability is accepted, an advisory will be sent out via GitHub's security advisory functionality. For non-sensitive discussion related to this policy itself, feel free to open an issue on the issue tracker. rpds_py-0.20.0/.github/dependabot.yml010064400017510000177000000003201465445241300156300ustar 00000000000000version: 2 updates: - package-ecosystem: "github-actions" directory: "/" schedule: interval: "weekly" - package-ecosystem: "cargo" directory: "/" schedule: interval: "weekly" rpds_py-0.20.0/.github/release.yml010064400017510000177000000001141465445241300151440ustar 00000000000000changelog: exclude: authors: - dependabot - pre-commit-ci rpds_py-0.20.0/.github/workflows/CI.yml010064400017510000177000000150601465445241300160620ustar 00000000000000name: CI on: push: branches: - main tags: - "v[0-9].*" pull_request: release: types: [published] schedule: # Daily at 5:33 - cron: "33 5 * * *" workflow_dispatch: jobs: list: runs-on: ubuntu-latest outputs: noxenvs: ${{ steps.noxenvs-matrix.outputs.noxenvs }} steps: - uses: actions/checkout@v4 - name: Set up nox uses: wntrblm/nox@2024.04.15 - id: noxenvs-matrix run: | echo >>$GITHUB_OUTPUT noxenvs=$( nox --list-sessions --json | jq '[.[].session]' ) test: needs: list runs-on: ubuntu-latest strategy: fail-fast: false matrix: noxenv: ${{ fromJson(needs.list.outputs.noxenvs) }} steps: - uses: actions/checkout@v4 - name: Install dependencies run: sudo apt-get update && sudo apt-get install -y libenchant-2-dev if: runner.os == 'Linux' && startsWith(matrix.noxenv, 'docs') - name: Install dependencies run: brew install enchant if: runner.os == 'macOS' && startsWith(matrix.noxenv, 'docs') - name: Set up Python uses: actions/setup-python@v5 with: python-version: | 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10 allow-prereleases: true - name: Set up uv uses: hynek/setup-cached-uv@v2 - name: Set up nox uses: wntrblm/nox@2024.04.15 - name: Run nox run: nox -s "${{ matrix.noxenv }}" manylinux: needs: test runs-on: ubuntu-latest strategy: matrix: target: [x86_64, x86, aarch64, armv7, s390x, ppc64le] steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: | 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10 allow-prereleases: true - name: Build wheels uses: PyO3/maturin-action@v1 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10' sccache: "true" manylinux: auto - name: Upload wheels uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }}-${{ matrix.target }} path: dist musllinux: needs: test runs-on: ubuntu-latest strategy: matrix: target: - aarch64-unknown-linux-musl - i686-unknown-linux-musl - x86_64-unknown-linux-musl steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: | 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10 allow-prereleases: true - name: Build wheels uses: PyO3/maturin-action@v1 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10' manylinux: musllinux_1_2 sccache: "true" - name: Upload wheels uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }}-${{ matrix.target }} path: dist windows: needs: test runs-on: windows-latest strategy: matrix: target: [x64, x86] # x86 is not supported by pypy steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: | 3.8 3.9 3.10 3.11 3.12 3.13 ${{ matrix.target == 'x64' && 'pypy3.9' || '' }} ${{ matrix.target == 'x64' && 'pypy3.10' || '' }} allow-prereleases: true architecture: ${{ matrix.target }} - name: Build wheels uses: PyO3/maturin-action@v1 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.8 3.9 3.10 3.11 3.12 3.13' --interpreter ${{ matrix.target == 'x64' && 'pypy3.9 pypy3.10' || '' }} sccache: "true" - name: Upload wheels uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }}-${{ matrix.target }} path: dist macos: needs: test runs-on: macos-latest strategy: matrix: target: [x86_64, aarch64] steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: | 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10 allow-prereleases: true - name: Build wheels uses: PyO3/maturin-action@v1 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10' sccache: "true" - name: Upload wheels uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }}-${{ matrix.target }} path: dist sdist: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: | 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10 allow-prereleases: true - name: Build an sdist uses: PyO3/maturin-action@v1 with: command: sdist args: --out dist - name: Upload sdist uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }} path: dist release: needs: [manylinux, musllinux, windows, macos] runs-on: ubuntu-latest if: "startsWith(github.ref, 'refs/tags/')" environment: name: PyPI url: https://pypi.org/p/rpds-py permissions: contents: write id-token: write steps: - uses: actions/download-artifact@v4 with: pattern: dist-* merge-multiple: true - name: Publish to PyPI uses: PyO3/maturin-action@v1 with: command: upload args: --non-interactive --skip-existing * - name: Create a GitHub Release if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') uses: softprops/action-gh-release@v2 with: files: | * generate_release_notes: true rpds_py-0.20.0/.gitignore010064400017510000177000000013241465445241300134350ustar 00000000000000/target # Byte-compiled / optimized / DLL files __pycache__/ .pytest_cache/ *.py[cod] # C extensions *.so # Distribution / packaging .Python .venv/ env/ bin/ build/ develop-eggs/ dist/ eggs/ lib/ lib64/ parts/ sdist/ var/ include/ man/ venv/ *.egg-info/ .installed.cfg *.egg # Installer logs pip-log.txt pip-delete-this-directory.txt pip-selfcheck.json # Unit test / coverage reports htmlcov/ .tox/ .coverage .cache nosetests.xml coverage.xml # Translations *.mo # Mr Developer .mr.developer.cfg .project .pydevproject # Rope .ropeproject # Django stuff: *.log *.pot .DS_Store # Sphinx documentation docs/_build/ # PyCharm .idea/ # VSCode .vscode/ # Pyenv .python-version # User defined /dirhtml _cache TODO rpds_py-0.20.0/.pre-commit-config.yaml010064400017510000177000000014041465445241300157250ustar 00000000000000ci: skip: # pre-commit.ci doesn't have Rust installed - fmt - clippy repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: - id: check-ast - id: check-docstring-first - id: check-toml - id: check-vcs-permalinks - id: check-yaml - id: debug-statements - id: end-of-file-fixer - id: mixed-line-ending args: [--fix, lf] - id: trailing-whitespace - repo: https://github.com/doublify/pre-commit-rust rev: "v1.0" hooks: - id: fmt - id: clippy - repo: https://github.com/psf/black rev: 24.8.0 hooks: - id: black - repo: https://github.com/pre-commit/mirrors-prettier rev: "v4.0.0-alpha.8" hooks: - id: prettier rpds_py-0.20.0/.readthedocs.yml010064400017510000177000000003611465445241300145330ustar 00000000000000version: 2 build: os: ubuntu-22.04 tools: python: "3.11" rust: "1.70" sphinx: builder: dirhtml configuration: docs/conf.py fail_on_warning: true formats: all python: install: - requirements: docs/requirements.txt rpds_py-0.20.0/LICENSE010064400017510000177000000020411465445241300124470ustar 00000000000000Copyright (c) 2023 Julian Berman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rpds_py-0.20.0/README.rst010064400017510000177000000052161465445241300131400ustar 00000000000000=========== ``rpds.py`` =========== |PyPI| |Pythons| |CI| .. |PyPI| image:: https://img.shields.io/pypi/v/rpds-py.svg :alt: PyPI version :target: https://pypi.org/project/rpds-py/ .. |Pythons| image:: https://img.shields.io/pypi/pyversions/rpds-py.svg :alt: Supported Python versions :target: https://pypi.org/project/rpds-py/ .. |CI| image:: https://github.com/crate-py/rpds/workflows/CI/badge.svg :alt: Build status :target: https://github.com/crate-py/rpds/actions?query=workflow%3ACI .. |ReadTheDocs| image:: https://readthedocs.org/projects/referencing/badge/?version=stable&style=flat :alt: ReadTheDocs status :target: https://referencing.readthedocs.io/en/stable/ Python bindings to the `Rust rpds crate `_ for persistent data structures. What's here is quite minimal (in transparency, it was written initially to support replacing ``pyrsistent`` in the `referencing library `_). If you see something missing (which is very likely), a PR is definitely welcome to add it. Installation ------------ The distribution on PyPI is named ``rpds.py`` (equivalently ``rpds-py``), and thus can be installed via e.g.: .. code:: sh $ pip install rpds-py Note that if you install ``rpds-py`` from source, you will need a Rust toolchain installed, as it is a build-time dependency. An example of how to do so in a ``Dockerfile`` can be found `here `_. If you believe you are on a common platform which should have wheels built (i.e. and not need to compile from source), feel free to file an issue or pull request modifying the GitHub action used here to build wheels via ``maturin``. Usage ----- Methods in general are named similarly to their ``rpds`` counterparts (rather than ``pyrsistent``\ 's conventions, though probably a full drop-in ``pyrsistent``\ -compatible wrapper module is a good addition at some point). .. code:: python >>> from rpds import HashTrieMap, HashTrieSet, List >>> m = HashTrieMap({"foo": "bar", "baz": "quux"}) >>> m.insert("spam", 37) == HashTrieMap({"foo": "bar", "baz": "quux", "spam": 37}) True >>> m.remove("foo") == HashTrieMap({"baz": "quux"}) True >>> s = HashTrieSet({"foo", "bar", "baz", "quux"}) >>> s.insert("spam") == HashTrieSet({"foo", "bar", "baz", "quux", "spam"}) True >>> s.remove("foo") == HashTrieSet({"bar", "baz", "quux"}) True >>> L = List([1, 3, 5]) >>> L.push_front(-1) == List([-1, 1, 3, 5]) True >>> L.rest == List([3, 5]) True rpds_py-0.20.0/docs/api.rst010064400017510000177000000002511465445241300136760ustar 00000000000000API Reference ============= .. automodule:: rpds :members: :undoc-members: :imported-members: :special-members: __iter__, __getitem__, __len__, __rmatmul__ rpds_py-0.20.0/docs/conf.py010064400017510000177000000032151465445241300136750ustar 00000000000000import importlib.metadata import re from url import URL GITHUB = URL.parse("https://github.com/") HOMEPAGE = GITHUB / "crate-py/rpds" project = "rpds.py" author = "Julian Berman" copyright = f"2023, {author}" release = importlib.metadata.version("rpds.py") version = release.partition("-")[0] language = "en" default_role = "any" extensions = [ "sphinx.ext.autodoc", "sphinx.ext.autosectionlabel", "sphinx.ext.coverage", "sphinx.ext.doctest", "sphinx.ext.extlinks", "sphinx.ext.intersphinx", "sphinx.ext.napoleon", "sphinx.ext.todo", "sphinx.ext.viewcode", "sphinx_copybutton", "sphinxcontrib.spelling", "sphinxext.opengraph", ] pygments_style = "lovelace" pygments_dark_style = "one-dark" html_theme = "furo" def entire_domain(host): return r"http.?://" + re.escape(host) + r"($|/.*)" linkcheck_ignore = [ entire_domain("img.shields.io"), f"{GITHUB}.*#.*", str(HOMEPAGE / "actions"), str(HOMEPAGE / "workflows/CI/badge.svg"), ] # = Extensions = # -- autodoc -- autodoc_default_options = { "members": True, "member-order": "bysource", } # -- autosectionlabel -- autosectionlabel_prefix_document = True # -- intersphinx -- intersphinx_mapping = { "python": ("https://docs.python.org/", None), } # -- extlinks -- extlinks = { "gh": (str(HOMEPAGE) + "/%s", None), "github": (str(GITHUB) + "/%s", None), } extlinks_detect_hardcoded_links = True # -- sphinx-copybutton -- copybutton_prompt_text = r">>> |\.\.\. |\$" copybutton_prompt_is_regexp = True # -- sphinxcontrib-spelling -- spelling_word_list_filename = "spelling-wordlist.txt" spelling_show_suggestions = True rpds_py-0.20.0/docs/index.rst010064400017510000177000000040061465445241300142360ustar 00000000000000Python bindings to the `Rust rpds crate `_ for persistent data structures. What's here is quite minimal (in transparency, it was written initially to support replacing ``pyrsistent`` in the `referencing library `_). If you see something missing (which is very likely), a PR is definitely welcome to add it. Installation ------------ The distribution on PyPI is named ``rpds.py`` (equivalently ``rpds-py``), and thus can be installed via e.g.: .. code:: sh $ pip install rpds-py Note that if you install ``rpds-py`` from source, you will need a Rust toolchain installed, as it is a build-time dependency. An example of how to do so in a ``Dockerfile`` can be found `here `_. If you believe you are on a common platform which should have wheels built (i.e. and not need to compile from source), feel free to file an issue or pull request modifying the GitHub action used here to build wheels via ``maturin``. Usage ----- Methods in general are named similarly to their ``rpds`` counterparts (rather than ``pyrsistent``\ 's conventions, though probably a full drop-in ``pyrsistent``\ -compatible wrapper module is a good addition at some point). .. code:: python >>> from rpds import HashTrieMap, HashTrieSet, List >>> m = HashTrieMap({"foo": "bar", "baz": "quux"}) >>> m.insert("spam", 37) == HashTrieMap({"foo": "bar", "baz": "quux", "spam": 37}) True >>> m.remove("foo") == HashTrieMap({"baz": "quux"}) True >>> s = HashTrieSet({"foo", "bar", "baz", "quux"}) >>> s.insert("spam") == HashTrieSet({"foo", "bar", "baz", "quux", "spam"}) True >>> s.remove("foo") == HashTrieSet({"bar", "baz", "quux"}) True >>> L = List([1, 3, 5]) >>> L.push_front(-1) == List([-1, 1, 3, 5]) True >>> L.rest == List([3, 5]) True .. toctree:: :glob: :hidden: api rpds_py-0.20.0/docs/requirements.in010064400017510000177000000001761465445241300154540ustar 00000000000000file:.#egg=rpds-py furo pygments-github-lexers sphinx-copybutton sphinx>5 sphinxcontrib-spelling>5 sphinxext-opengraph url.py rpds_py-0.20.0/docs/requirements.txt010064400017510000177000000032651465445241300156670ustar 00000000000000# # This file is autogenerated by pip-compile with Python 3.12 # by the following command: # # pip-compile --strip-extras docs/requirements.in # alabaster==0.7.16 # via sphinx babel==2.15.0 # via sphinx beautifulsoup4==4.12.3 # via furo certifi==2024.7.4 # via requests charset-normalizer==3.3.2 # via requests docutils==0.21.2 # via sphinx furo==2024.5.6 # via -r docs/requirements.in idna==3.7 # via requests imagesize==1.4.1 # via sphinx jinja2==3.1.4 # via sphinx markupsafe==2.1.5 # via jinja2 packaging==24.1 # via sphinx pyenchant==3.2.2 # via sphinxcontrib-spelling pygments==2.18.0 # via # furo # pygments-github-lexers # sphinx pygments-github-lexers==0.0.5 # via -r docs/requirements.in requests==2.32.3 # via sphinx file:.#egg=rpds-py # via -r docs/requirements.in snowballstemmer==2.2.0 # via sphinx soupsieve==2.5 # via beautifulsoup4 sphinx==7.3.7 # via # -r docs/requirements.in # furo # sphinx-basic-ng # sphinx-copybutton # sphinxcontrib-spelling # sphinxext-opengraph sphinx-basic-ng==1.0.0b2 # via furo sphinx-copybutton==0.5.2 # via -r docs/requirements.in sphinxcontrib-applehelp==1.0.8 # via sphinx sphinxcontrib-devhelp==1.0.6 # via sphinx sphinxcontrib-htmlhelp==2.0.5 # via sphinx sphinxcontrib-jsmath==1.0.1 # via sphinx sphinxcontrib-qthelp==1.0.7 # via sphinx sphinxcontrib-serializinghtml==1.1.10 # via sphinx sphinxcontrib-spelling==8.0.0 # via -r docs/requirements.in sphinxext-opengraph==0.9.1 # via -r docs/requirements.in url-py==0.11.3 # via -r docs/requirements.in urllib3==2.2.2 # via requests rpds_py-0.20.0/docs/spelling-wordlist.txt010064400017510000177000000000231465445241300166130ustar 00000000000000iter len toolchain rpds_py-0.20.0/noxfile.py010064400017510000177000000106661465445241300134740ustar 00000000000000from pathlib import Path from tempfile import TemporaryDirectory import os import nox ROOT = Path(__file__).parent PYPROJECT = ROOT / "pyproject.toml" DOCS = ROOT / "docs" TESTS = ROOT / "tests" REQUIREMENTS = dict( docs=DOCS / "requirements.txt", tests=TESTS / "requirements.txt", ) REQUIREMENTS_IN = [ # this is actually ordered, as files depend on each other (path.parent / f"{path.stem}.in", path) for path in REQUIREMENTS.values() ] SUPPORTED = ["3.8", "3.9", "3.10", "pypy3.10", "3.11", "3.12", "3.13"] LATEST = "3.12" nox.options.default_venv_backend = "uv|virtualenv" nox.options.sessions = [] def session(default=True, python=LATEST, **kwargs): # noqa: D103 def _session(fn): if default: nox.options.sessions.append(kwargs.get("name", fn.__name__)) return nox.session(python=python, **kwargs)(fn) return _session @session(python=SUPPORTED) def tests(session): """ Run the test suite with a corresponding Python version. """ session.install("-r", REQUIREMENTS["tests"]) if session.posargs and session.posargs[0] == "coverage": if len(session.posargs) > 1 and session.posargs[1] == "github": github = Path(os.environ["GITHUB_STEP_SUMMARY"]) else: github = None session.install("coverage[toml]") session.run("coverage", "run", "-m", "pytest", TESTS) if github is None: session.run("coverage", "report") else: with github.open("a") as summary: summary.write("### Coverage\n\n") summary.flush() # without a flush, output seems out of order. session.run( "coverage", "report", "--format=markdown", stdout=summary, ) else: session.run("pytest", *session.posargs, TESTS) @session() def audit(session): """ Audit dependencies for vulnerabilities. """ session.install("pip-audit", ROOT) session.run("python", "-m", "pip_audit") @session(tags=["build"]) def build(session): """ Build a distribution suitable for PyPI and check its validity. """ session.install("build", "twine") with TemporaryDirectory() as tmpdir: session.run("python", "-m", "build", ROOT, "--outdir", tmpdir) session.run("twine", "check", "--strict", tmpdir + "/*") @session(tags=["style"]) def style(session): """ Check Python code style. """ session.install("ruff") session.run("ruff", "check", TESTS, __file__) @session() def typing(session): """ Check the codebase using pyright by type checking the test suite. """ session.install("pyright", ROOT, "-r", REQUIREMENTS["tests"]) session.run("pyright", TESTS) @session(tags=["docs"]) @nox.parametrize( "builder", [ nox.param(name, id=name) for name in [ "dirhtml", "doctest", "linkcheck", "man", "spelling", ] ], ) def docs(session, builder): """ Build the documentation using a specific Sphinx builder. """ session.install("-r", REQUIREMENTS["docs"]) with TemporaryDirectory() as tmpdir_str: tmpdir = Path(tmpdir_str) argv = ["-n", "-T", "-W"] if builder != "spelling": argv += ["-q"] posargs = session.posargs or [tmpdir / builder] session.run( "python", "-m", "sphinx", "-b", builder, DOCS, *argv, *posargs, ) @session(tags=["docs", "style"], name="docs(style)") def docs_style(session): """ Check the documentation style. """ session.install( "doc8", "pygments", "pygments-github-lexers", ) session.run("python", "-m", "doc8", "--config", PYPROJECT, DOCS) @session(default=False) def requirements(session): """ Update the project's pinned requirements. You should commit the result afterwards. """ if session.venv_backend == "uv": cmd = ["uv", "pip", "compile"] else: session.install("pip-tools") cmd = ["pip-compile", "--resolver", "backtracking", "--strip-extras"] for each, out in REQUIREMENTS_IN: # otherwise output files end up with silly absolute path comments... relative = each.relative_to(ROOT) session.run(*cmd, "--upgrade", "--output-file", out, relative) rpds_py-0.20.0/rpds.pyi010064400017510000177000000050121465445241300131360ustar 00000000000000from typing import ( ItemsView, Iterable, Iterator, KeysView, Mapping, TypeVar, ValuesView, ) _T = TypeVar("_T") _KT_co = TypeVar("_KT_co", covariant=True) _VT_co = TypeVar("_VT_co", covariant=True) _KU_co = TypeVar("_KU_co", covariant=True) _VU_co = TypeVar("_VU_co", covariant=True) class HashTrieMap(Mapping[_KT_co, _VT_co]): def __init__( self, value: Mapping[_KT_co, _VT_co] | Iterable[tuple[_KT_co, _VT_co]] = {}, **kwds: Mapping[_KT_co, _VT_co], ): ... def __getitem__(self, key: _KT_co) -> _VT_co: ... def __iter__(self) -> Iterator[_KT_co]: ... def __len__(self) -> int: ... def discard(self, key: _KT_co) -> HashTrieMap[_KT_co, _VT_co]: ... def items(self) -> ItemsView[_KT_co, _VT_co]: ... def keys(self) -> KeysView[_KT_co]: ... def values(self) -> ValuesView[_VT_co]: ... def remove(self, key: _KT_co) -> HashTrieMap[_KT_co, _VT_co]: ... def insert( self, key: _KT_co, val: _VT_co, ) -> HashTrieMap[_KT_co, _VT_co]: ... def update( self, *args: Mapping[_KU_co, _VU_co] | Iterable[tuple[_KU_co, _VU_co]], ) -> HashTrieMap[_KT_co | _KU_co, _VT_co | _VU_co]: ... @classmethod def convert( cls, value: Mapping[_KT_co, _VT_co] | Iterable[tuple[_KT_co, _VT_co]], ) -> HashTrieMap[_KT_co, _VT_co]: ... @classmethod def fromkeys( cls, keys: Iterable[_KT_co], value: _VT_co = None, ) -> HashTrieMap[_KT_co, _VT_co]: ... class HashTrieSet(frozenset[_T]): def __init__(self, value: Iterable[_T] = ()): ... def __iter__(self) -> Iterator[_T]: ... def __len__(self) -> int: ... def discard(self, value: _T) -> HashTrieSet[_T]: ... def remove(self, value: _T) -> HashTrieSet[_T]: ... def insert(self, value: _T) -> HashTrieSet[_T]: ... def update(self, *args: Iterable[_T]) -> HashTrieSet[_T]: ... class List(Iterable[_T]): def __init__(self, value: Iterable[_T] = (), *more: _T): ... def __iter__(self) -> Iterator[_T]: ... def __len__(self) -> int: ... def push_front(self, value: _T) -> List[_T]: ... def drop_first(self) -> List[_T]: ... class Queue(Iterable[_T]): def __init__(self, value: Iterable[_T] = (), *more: _T): ... def __iter__(self) -> Iterator[_T]: ... def __len__(self) -> int: ... def enqueue(self, value: _T) -> Queue[_T]: ... def dequeue(self, value: _T) -> Queue[_T]: ... @property def is_empty(self) -> _T: ... @property def peek(self) -> _T: ... rpds_py-0.20.0/src/lib.rs010064400017510000177000001214751465445241300133620ustar 00000000000000use pyo3::exceptions::{PyIndexError, PyTypeError}; use pyo3::pyclass::CompareOp; use pyo3::types::{PyDict, PyIterator, PyTuple, PyType}; use pyo3::{exceptions::PyKeyError, types::PyMapping, types::PyTupleMethods}; use pyo3::{prelude::*, AsPyPointer, PyTypeInfo}; use rpds::{ HashTrieMap, HashTrieMapSync, HashTrieSet, HashTrieSetSync, List, ListSync, Queue, QueueSync, }; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; fn hash_shuffle_bits(h: usize) -> usize { ((h ^ 89869747) ^ (h << 16)) * 3644798167 } #[derive(Debug)] struct Key { hash: isize, inner: PyObject, } impl Hash for Key { fn hash(&self, state: &mut H) { state.write_isize(self.hash); } } impl Eq for Key {} impl PartialEq for Key { fn eq(&self, other: &Self) -> bool { Python::with_gil(|py| { self.inner .call_method1(py, "__eq__", (&other.inner,)) .and_then(|value| value.extract(py)) .expect("__eq__ failed!") }) } } impl Key { fn clone_ref(&self, py: Python<'_>) -> Self { Key { hash: self.hash, inner: self.inner.clone_ref(py), } } } impl IntoPy for Key { fn into_py(self, py: Python<'_>) -> PyObject { self.inner.into_py(py) } } unsafe impl AsPyPointer for Key { fn as_ptr(&self) -> *mut pyo3::ffi::PyObject { self.inner.as_ptr() } } impl<'source> FromPyObject<'source> for Key { fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { Ok(Key { hash: ob.hash()?, inner: ob.clone().unbind(), }) } } #[repr(transparent)] #[pyclass(name = "HashTrieMap", module = "rpds", frozen, mapping)] struct HashTrieMapPy { inner: HashTrieMapSync, } impl From> for HashTrieMapPy { fn from(map: HashTrieMapSync) -> Self { HashTrieMapPy { inner: map } } } impl<'source> FromPyObject<'source> for HashTrieMapPy { fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { let mut ret = HashTrieMap::new_sync(); if let Ok(mapping) = ob.downcast::() { for each in mapping.items()?.iter()? { let (k, v): (Key, PyObject) = each?.extract()?; ret.insert_mut(k, v); } } else { for each in ob.iter()? { let (k, v): (Key, PyObject) = each?.extract()?; ret.insert_mut(k, v); } } Ok(HashTrieMapPy { inner: ret }) } } #[pymethods] impl HashTrieMapPy { #[new] #[pyo3(signature = (value=None, ** kwds))] fn init(value: Option, kwds: Option<&Bound<'_, PyDict>>) -> PyResult { let mut map: HashTrieMapPy; if let Some(value) = value { map = value; } else { map = HashTrieMapPy { inner: HashTrieMap::new_sync(), }; } if let Some(kwds) = kwds { for (k, v) in kwds { map.inner.insert_mut(Key::extract_bound(&k)?, v.into()); } } Ok(map) } fn __contains__(&self, key: Key) -> bool { self.inner.contains_key(&key) } fn __iter__(slf: PyRef<'_, Self>) -> KeysIterator { KeysIterator { inner: slf.inner.clone(), } } fn __getitem__(&self, key: Key, py: Python) -> PyResult { match self.inner.get(&key) { Some(value) => Ok(value.clone_ref(py)), None => Err(PyKeyError::new_err(key)), } } fn __len__(&self) -> usize { self.inner.size() } fn __repr__(&self, py: Python) -> String { let contents = self.inner.into_iter().map(|(k, v)| { format!( "{}: {}", k.inner .call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()), v.call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()) ) }); format!( "HashTrieMap({{{}}})", contents.collect::>().join(", ") ) } fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyResult { match op { CompareOp::Eq => Ok((self.inner.size() == other.inner.size() && self .inner .iter() .map(|(k1, v1)| (v1, other.inner.get(k1))) .map(|(v1, v2)| v1.bind(py).eq(v2)) .all(|r| r.unwrap_or(false))) .into_py(py)), CompareOp::Ne => Ok((self.inner.size() != other.inner.size() || self .inner .iter() .map(|(k1, v1)| (v1, other.inner.get(k1))) .map(|(v1, v2)| v1.bind(py).ne(v2)) .all(|r| r.unwrap_or(true))) .into_py(py)), _ => Ok(py.NotImplemented()), } } fn __hash__(&self, py: Python) -> PyResult { // modified from https://github.com/python/cpython/blob/d69529d31ccd1510843cfac1ab53bb8cb027541f/Objects/setobject.c#L715 let mut hash_val = self .inner .iter() .map(|(key, val)| { let mut hasher = DefaultHasher::new(); let val_bound = val.bind(py); let key_hash = key.hash; let val_hash = val_bound.hash().map_err(|_| { PyTypeError::new_err(format!( "Unhashable type in HashTrieMap of key {}: {}", key.inner .bind(py) .repr() .and_then(|r| r.extract()) .unwrap_or(" error".to_string()), val_bound .repr() .and_then(|r| r.extract()) .unwrap_or(" error".to_string()) )) })?; hasher.write_isize(key_hash); hasher.write_isize(val_hash); Ok(hasher.finish() as usize) }) .try_fold(0, |acc: usize, x: PyResult| { PyResult::::Ok(acc ^ hash_shuffle_bits(x?)) })?; // factor in the number of entries in the collection hash_val ^= (self.inner.size() + 1) * 1927868237; // dispense patterns in the hash value hash_val ^= (hash_val >> 11) ^ (hash_val >> 25); hash_val = hash_val * 69069 + 907133923; Ok(hash_val as isize) } fn __reduce__(slf: PyRef) -> (Bound<'_, PyType>, (Vec<(Key, PyObject)>,)) { ( HashTrieMapPy::type_object_bound(slf.py()), (slf.inner .iter() .map(|(k, v)| (k.clone_ref(slf.py()), v.clone_ref(slf.py()))) .collect(),), ) } #[classmethod] fn convert( _cls: &Bound<'_, PyType>, value: Bound<'_, PyAny>, py: Python, ) -> PyResult { if value.is_instance_of::() { Ok(value.unbind()) } else { Ok(HashTrieMapPy::extract_bound(&value)?.into_py(py)) } } #[classmethod] #[pyo3(signature = (keys, val=None))] fn fromkeys( _cls: &Bound<'_, PyType>, keys: &Bound<'_, PyAny>, val: Option<&Bound<'_, PyAny>>, py: Python, ) -> PyResult { let mut inner = HashTrieMap::new_sync(); let none = py.None().into_bound(py); let value = val.unwrap_or(&none); for each in keys.iter()? { let key = Key::extract_bound(&each?)?; inner.insert_mut(key, value.clone().unbind()); } Ok(HashTrieMapPy { inner }) } #[pyo3(signature = (key, default=None))] fn get(&self, key: Key, default: Option, py: Python) -> Option { if let Some(value) = self.inner.get(&key) { Some(value.clone_ref(py)) } else { default } } fn keys(&self) -> KeysView { KeysView { inner: self.inner.clone(), } } fn values(&self) -> ValuesView { ValuesView { inner: self.inner.clone(), } } fn items(&self) -> ItemsView { ItemsView { inner: self.inner.clone(), } } fn discard(&self, key: Key) -> PyResult { match self.inner.contains_key(&key) { true => Ok(HashTrieMapPy { inner: self.inner.remove(&key), }), false => Ok(HashTrieMapPy { inner: self.inner.clone(), }), } } fn insert(&self, key: Key, value: Bound<'_, PyAny>) -> HashTrieMapPy { HashTrieMapPy { inner: self.inner.insert(key, value.unbind()), } } fn remove(&self, key: Key) -> PyResult { match self.inner.contains_key(&key) { true => Ok(HashTrieMapPy { inner: self.inner.remove(&key), }), false => Err(PyKeyError::new_err(key)), } } #[pyo3(signature = (*maps, **kwds))] fn update( &self, maps: &Bound<'_, PyTuple>, kwds: Option<&Bound<'_, PyDict>>, ) -> PyResult { let mut inner = self.inner.clone(); for value in maps { let map = HashTrieMapPy::extract_bound(&value)?; for (k, v) in &map.inner { inner.insert_mut(k.clone_ref(value.py()), v.clone_ref(value.py())); } } if let Some(kwds) = kwds { for (k, v) in kwds { inner.insert_mut(Key::extract_bound(&k)?, v.extract()?); } } Ok(HashTrieMapPy { inner }) } } #[pyclass(module = "rpds")] struct KeysIterator { inner: HashTrieMapSync, } #[pymethods] impl KeysIterator { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { slf } fn __next__(mut slf: PyRefMut<'_, Self>) -> Option { let first = slf.inner.keys().next()?.clone_ref(slf.py()); slf.inner = slf.inner.remove(&first); Some(first) } } #[pyclass(module = "rpds")] struct ValuesIterator { inner: HashTrieMapSync, } #[pymethods] impl ValuesIterator { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { slf } fn __next__(mut slf: PyRefMut<'_, Self>) -> Option { let kv = slf.inner.iter().next()?; let value = kv.1.clone_ref(slf.py()); slf.inner = slf.inner.remove(kv.0); Some(value) } } #[pyclass(module = "rpds")] struct ItemsIterator { inner: HashTrieMapSync, } #[pymethods] impl ItemsIterator { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { slf } fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<(Key, PyObject)> { let kv = slf.inner.iter().next()?; let key = kv.0.clone_ref(slf.py()); let value = kv.1.clone_ref(slf.py()); slf.inner = slf.inner.remove(kv.0); Some((key, value)) } } #[pyclass(module = "rpds")] struct KeysView { inner: HashTrieMapSync, } #[pymethods] impl KeysView { fn __contains__(&self, key: Key) -> bool { self.inner.contains_key(&key) } fn __eq__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? != slf.inner.size() { return Ok(false); } for each in other.iter()? { if !slf.inner.contains_key(&Key::extract_bound(&each?)?) { return Ok(false); } } Ok(true) } fn __lt__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? <= slf.inner.size() { return Ok(false); } for each in slf.inner.keys() { if !other.contains(each.inner.clone_ref(slf.py()))? { return Ok(false); } } Ok(true) } fn __le__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? < slf.inner.size() { return Ok(false); } for each in slf.inner.keys() { if !other.contains(each.inner.clone_ref(slf.py()))? { return Ok(false); } } Ok(true) } fn __gt__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? >= slf.inner.size() { return Ok(false); } for each in other.iter()? { if !slf.inner.contains_key(&Key::extract_bound(&each?)?) { return Ok(false); } } Ok(true) } fn __ge__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? > slf.inner.size() { return Ok(false); } for each in other.iter()? { if !slf.inner.contains_key(&Key::extract_bound(&each?)?) { return Ok(false); } } Ok(true) } fn __iter__(slf: PyRef<'_, Self>) -> KeysIterator { KeysIterator { inner: slf.inner.clone(), } } fn __len__(slf: PyRef<'_, Self>) -> usize { slf.inner.size() } fn __and__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>) -> PyResult { KeysView::intersection(slf, other) } fn __or__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { KeysView::union(slf, other, py) } fn __repr__(&self, py: Python) -> String { let contents = self.inner.into_iter().map(|(k, _)| { k.clone_ref(py) .inner .into_py(py) .call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()) }); format!("keys_view({{{}}})", contents.collect::>().join(", ")) } fn intersection(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>) -> PyResult { // TODO: iterate over the shorter one if it's got a length let mut inner = HashTrieSet::new_sync(); for each in other.iter()? { let key = Key::extract_bound(&each?)?; if slf.inner.contains_key(&key) { inner.insert_mut(key); } } Ok(HashTrieSetPy { inner }) } fn union(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { // There doesn't seem to be a low-effort way to get a HashTrieSet out of a map, // so we just keep our map and add values we'll ignore. let mut inner = slf.inner.clone(); for each in other.iter()? { inner.insert_mut(Key::extract_bound(&each?)?, py.None()); } Ok(KeysView { inner }) } } #[pyclass(module = "rpds")] struct ValuesView { inner: HashTrieMapSync, } #[pymethods] impl ValuesView { fn __iter__(slf: PyRef<'_, Self>) -> ValuesIterator { ValuesIterator { inner: slf.inner.clone(), } } fn __len__(slf: PyRef<'_, Self>) -> usize { slf.inner.size() } fn __repr__(&self, py: Python) -> String { let contents = self.inner.into_iter().map(|(_, v)| { v.into_py(py) .call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()) }); format!("values_view([{}])", contents.collect::>().join(", ")) } } #[pyclass(module = "rpds")] struct ItemsView { inner: HashTrieMapSync, } #[derive(FromPyObject)] struct ItemViewQuery(Key, PyObject); #[pymethods] impl ItemsView { fn __contains__(slf: PyRef<'_, Self>, item: ItemViewQuery) -> PyResult { if let Some(value) = slf.inner.get(&item.0) { return item.1.bind(slf.py()).eq(value); } Ok(false) } fn __iter__(slf: PyRef<'_, Self>) -> ItemsIterator { ItemsIterator { inner: slf.inner.clone(), } } fn __len__(slf: PyRef<'_, Self>) -> usize { slf.inner.size() } fn __eq__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? != slf.inner.size() { return Ok(false); } for (k, v) in slf.inner.iter() { if !other.contains((k.inner.clone_ref(slf.py()), v))? { return Ok(false); } } Ok(true) } fn __repr__(&self, py: Python) -> String { let contents = self.inner.into_iter().map(|(k, v)| { let tuple = PyTuple::new_bound(py, [k.inner.clone_ref(py), v.clone_ref(py)]); format!("{:?}", tuple) }); format!("items_view([{}])", contents.collect::>().join(", ")) } fn __lt__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? <= slf.inner.size() { return Ok(false); } for (k, v) in slf.inner.iter() { let pair = PyTuple::new_bound(py, [k.inner.clone_ref(py), v.clone_ref(py)]); // FIXME: needs to compare if !other.contains(pair)? { return Ok(false); } } Ok(true) } fn __le__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? < slf.inner.size() { return Ok(false); } for (k, v) in slf.inner.iter() { let pair = PyTuple::new_bound(py, [k.inner.clone_ref(py), v.clone_ref(py)]); // FIXME: needs to compare if !other.contains(pair)? { return Ok(false); } } Ok(true) } fn __gt__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? >= slf.inner.size() { return Ok(false); } for each in other.iter()? { let kv = each?; let k = kv.get_item(0)?; match slf.inner.get(&Key::extract_bound(&k)?) { Some(value) => { let pair = PyTuple::new_bound(py, [k, value.bind(py).clone()]); if !pair.eq(kv)? { return Ok(false); } } None => return Ok(false), } } Ok(true) } fn __ge__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? > slf.inner.size() { return Ok(false); } for each in other.iter()? { let kv = each?; let k = kv.get_item(0)?; match slf.inner.get(&Key::extract_bound(&k)?) { Some(value) => { let pair = PyTuple::new_bound(py, [k, value.bind(py).clone()]); if !pair.eq(kv)? { return Ok(false); } } None => return Ok(false), } } Ok(true) } fn __and__( slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python, ) -> PyResult { ItemsView::intersection(slf, other, py) } fn __or__( slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python, ) -> PyResult { ItemsView::union(slf, other, py) } fn intersection( slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python, ) -> PyResult { // TODO: iterate over the shorter one if it's got a length let mut inner = HashTrieSet::new_sync(); for each in other.iter()? { let kv = each?; let k = kv.get_item(0)?; if let Some(value) = slf.inner.get(&Key::extract_bound(&k)?) { let pair = PyTuple::new_bound(py, [k, value.bind(py).clone()]); if pair.eq(kv)? { inner.insert_mut(Key::extract_bound(&pair)?); } } } Ok(HashTrieSetPy { inner }) } fn union( slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python, ) -> PyResult { // TODO: this is very inefficient, but again can't seem to get a HashTrieSet out of ourself let mut inner = HashTrieSet::new_sync(); for (k, v) in slf.inner.iter() { let pair = PyTuple::new_bound(py, [k.inner.clone_ref(py), v.clone_ref(py)]); inner.insert_mut(Key::extract_bound(&pair)?); } for each in other.iter()? { inner.insert_mut(Key::extract_bound(&each?)?); } Ok(HashTrieSetPy { inner }) } } #[repr(transparent)] #[pyclass(name = "HashTrieSet", module = "rpds", frozen)] struct HashTrieSetPy { inner: HashTrieSetSync, } impl<'source> FromPyObject<'source> for HashTrieSetPy { fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { let mut ret = HashTrieSet::new_sync(); for each in ob.iter()? { let k: Key = each?.extract()?; ret.insert_mut(k); } Ok(HashTrieSetPy { inner: ret }) } } #[pymethods] impl HashTrieSetPy { #[new] #[pyo3(signature = (value=None))] fn init(value: Option) -> Self { if let Some(value) = value { value } else { HashTrieSetPy { inner: HashTrieSet::new_sync(), } } } fn __contains__(&self, key: Key) -> bool { self.inner.contains(&key) } fn __and__(&self, other: &Self, py: Python) -> Self { self.intersection(other, py) } fn __or__(&self, other: &Self, py: Python) -> Self { self.union(other, py) } fn __sub__(&self, other: &Self) -> Self { self.difference(other) } fn __xor__(&self, other: &Self, py: Python) -> Self { self.symmetric_difference(other, py) } fn __iter__(slf: PyRef<'_, Self>) -> SetIterator { SetIterator { inner: slf.inner.clone(), } } fn __len__(&self) -> usize { self.inner.size() } fn __repr__(&self, py: Python) -> String { let contents = self.inner.into_iter().map(|k| { k.clone_ref(py) .into_py(py) .call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()) }); format!( "HashTrieSet({{{}}})", contents.collect::>().join(", ") ) } fn __eq__(slf: PyRef<'_, Self>, other: Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? != slf.inner.size() { return Ok(false); } for each in other.iter()? { if !slf.inner.contains(&Key::extract_bound(&each?)?) { return Ok(false); } } Ok(true) } fn __hash__(&self) -> PyResult { // modified from https://github.com/python/cpython/blob/d69529d31ccd1510843cfac1ab53bb8cb027541f/Objects/setobject.c#L715 let mut hash_val = self .inner .iter() .map(|k| k.hash as usize) .fold(0, |acc: usize, x: usize| acc ^ hash_shuffle_bits(x)); // factor in the number of entries in the collection hash_val ^= (self.inner.size() + 1) * 1927868237; // dispense patterns in the hash value hash_val ^= (hash_val >> 11) ^ (hash_val >> 25); hash_val = hash_val * 69069 + 907133923; Ok(hash_val as isize) } fn __lt__(slf: PyRef<'_, Self>, other: Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? <= slf.inner.size() { return Ok(false); } for each in slf.inner.iter() { if !other.contains(each.inner.clone_ref(py))? { return Ok(false); } } Ok(true) } fn __le__(slf: PyRef<'_, Self>, other: Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? < slf.inner.size() { return Ok(false); } for each in slf.inner.iter() { if !other.contains(each.inner.clone_ref(slf.py()))? { return Ok(false); } } Ok(true) } fn __gt__(slf: PyRef<'_, Self>, other: Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? >= slf.inner.size() { return Ok(false); } for each in other.iter()? { if !slf.inner.contains(&Key::extract_bound(&each?)?) { return Ok(false); } } Ok(true) } fn __ge__(slf: PyRef<'_, Self>, other: Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import_bound(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? > slf.inner.size() { return Ok(false); } for each in other.iter()? { if !slf.inner.contains(&Key::extract_bound(&each?)?) { return Ok(false); } } Ok(true) } fn __reduce__(slf: PyRef) -> (Bound<'_, PyType>, (Vec,)) { ( HashTrieSetPy::type_object_bound(slf.py()), (slf.inner.iter().map(|e| e.clone_ref(slf.py())).collect(),), ) } fn insert(&self, value: Key) -> HashTrieSetPy { HashTrieSetPy { inner: self.inner.insert(value), } } fn discard(&self, value: Key) -> PyResult { match self.inner.contains(&value) { true => Ok(HashTrieSetPy { inner: self.inner.remove(&value), }), false => Ok(HashTrieSetPy { inner: self.inner.clone(), }), } } fn remove(&self, value: Key) -> PyResult { match self.inner.contains(&value) { true => Ok(HashTrieSetPy { inner: self.inner.remove(&value), }), false => Err(PyKeyError::new_err(value)), } } fn difference(&self, other: &Self) -> HashTrieSetPy { let mut inner = self.inner.clone(); for value in other.inner.iter() { inner.remove_mut(value); } HashTrieSetPy { inner } } fn intersection(&self, other: &Self, py: Python) -> HashTrieSetPy { let mut inner: HashTrieSetSync = HashTrieSet::new_sync(); let larger: &HashTrieSetSync; let iter; if self.inner.size() > other.inner.size() { larger = &self.inner; iter = other.inner.iter(); } else { larger = &other.inner; iter = self.inner.iter(); } for value in iter { if larger.contains(value) { inner.insert_mut(value.clone_ref(py)); } } HashTrieSetPy { inner } } fn symmetric_difference(&self, other: &Self, py: Python) -> HashTrieSetPy { let mut inner: HashTrieSetSync; let iter; if self.inner.size() > other.inner.size() { inner = self.inner.clone(); iter = other.inner.iter(); } else { inner = other.inner.clone(); iter = self.inner.iter(); } for value in iter { if inner.contains(value) { inner.remove_mut(value); } else { inner.insert_mut(value.clone_ref(py)); } } HashTrieSetPy { inner } } fn union(&self, other: &Self, py: Python) -> HashTrieSetPy { let mut inner: HashTrieSetSync; let iter; if self.inner.size() > other.inner.size() { inner = self.inner.clone(); iter = other.inner.iter(); } else { inner = other.inner.clone(); iter = self.inner.iter(); } for value in iter { inner.insert_mut(value.clone_ref(py)); } HashTrieSetPy { inner } } #[pyo3(signature = (*iterables))] fn update(&self, iterables: Bound<'_, PyTuple>) -> PyResult { let mut inner = self.inner.clone(); for each in iterables { let iter = each.iter()?; for value in iter { inner.insert_mut(Key::extract_bound(&value?)?); } } Ok(HashTrieSetPy { inner }) } } #[pyclass(module = "rpds")] struct SetIterator { inner: HashTrieSetSync, } #[pymethods] impl SetIterator { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { slf } fn __next__(mut slf: PyRefMut<'_, Self>) -> Option { let first = slf.inner.iter().next()?.clone_ref(slf.py()); slf.inner = slf.inner.remove(&first); Some(first) } } #[repr(transparent)] #[pyclass(name = "List", module = "rpds", frozen, sequence)] struct ListPy { inner: ListSync, } impl From> for ListPy { fn from(elements: ListSync) -> Self { ListPy { inner: elements } } } impl<'source> FromPyObject<'source> for ListPy { fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { let mut ret = List::new_sync(); let reversed = PyModule::import_bound(ob.py(), "builtins")?.getattr("reversed")?; let rob: Bound<'_, PyIterator> = reversed.call1((ob,))?.iter()?; for each in rob { ret.push_front_mut(each?.extract()?); } Ok(ListPy { inner: ret }) } } #[pymethods] impl ListPy { #[new] #[pyo3(signature = (*elements))] fn init(elements: &Bound<'_, PyTuple>) -> PyResult { let mut ret: ListPy; if elements.len() == 1 { ret = elements.get_item(0)?.extract()?; } else { ret = ListPy { inner: List::new_sync(), }; if elements.len() > 1 { for each in (0..elements.len()).rev() { ret.inner .push_front_mut(elements.get_item(each)?.extract()?); } } } Ok(ret) } fn __len__(&self) -> usize { self.inner.len() } fn __repr__(&self, py: Python) -> String { let contents = self.inner.into_iter().map(|k| { k.into_py(py) .call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()) }); format!("List([{}])", contents.collect::>().join(", ")) } fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyResult { match op { CompareOp::Eq => Ok((self.inner.len() == other.inner.len() && self .inner .iter() .zip(other.inner.iter()) .map(|(e1, e2)| e1.bind(py).eq(e2)) .all(|r| r.unwrap_or(false))) .into_py(py)), CompareOp::Ne => Ok((self.inner.len() != other.inner.len() || self .inner .iter() .zip(other.inner.iter()) .map(|(e1, e2)| e1.bind(py).ne(e2)) .any(|r| r.unwrap_or(true))) .into_py(py)), _ => Ok(py.NotImplemented()), } } fn __hash__(&self, py: Python) -> PyResult { let mut hasher = DefaultHasher::new(); self.inner .iter() .enumerate() .try_for_each(|(index, each)| { each.bind(py) .hash() .map_err(|_| { PyTypeError::new_err(format!( "Unhashable type at {} element in List: {}", index, each.bind(py) .repr() .and_then(|r| r.extract()) .unwrap_or(" error".to_string()) )) }) .map(|x| hasher.write_isize(x)) })?; Ok(hasher.finish()) } fn __iter__(slf: PyRef<'_, Self>) -> ListIterator { ListIterator { inner: slf.inner.clone(), } } fn __reversed__(&self) -> ListPy { ListPy { inner: self.inner.reverse(), } } fn __reduce__(slf: PyRef) -> (Bound<'_, PyType>, (Vec,)) { ( ListPy::type_object_bound(slf.py()), (slf.inner.iter().map(|e| e.clone_ref(slf.py())).collect(),), ) } #[getter] fn first(&self) -> PyResult<&PyObject> { self.inner .first() .ok_or_else(|| PyIndexError::new_err("empty list has no first element")) } #[getter] fn rest(&self) -> ListPy { let mut inner = self.inner.clone(); inner.drop_first_mut(); ListPy { inner } } fn push_front(&self, other: PyObject) -> ListPy { ListPy { inner: self.inner.push_front(other), } } fn drop_first(&self) -> PyResult { if let Some(inner) = self.inner.drop_first() { Ok(ListPy { inner }) } else { Err(PyIndexError::new_err("empty list has no first element")) } } } #[pyclass(module = "rpds")] struct ListIterator { inner: ListSync, } #[pymethods] impl ListIterator { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { slf } fn __next__(mut slf: PyRefMut<'_, Self>) -> Option { let first_op = slf.inner.first()?; let first = first_op.clone_ref(slf.py()); slf.inner = slf.inner.drop_first()?; Some(first) } } #[pyclass(module = "rpds")] struct QueueIterator { inner: QueueSync, } #[pymethods] impl QueueIterator { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { slf } fn __next__(mut slf: PyRefMut<'_, Self>) -> Option { let first_op = slf.inner.peek()?; let first = first_op.clone_ref(slf.py()); slf.inner = slf.inner.dequeue()?; Some(first) } } #[repr(transparent)] #[pyclass(name = "Queue", module = "rpds", frozen, sequence)] struct QueuePy { inner: QueueSync, } impl From> for QueuePy { fn from(elements: QueueSync) -> Self { QueuePy { inner: elements } } } impl<'source> FromPyObject<'source> for QueuePy { fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { let mut ret = Queue::new_sync(); for each in ob.iter()? { ret.enqueue_mut(each?.extract()?); } Ok(QueuePy { inner: ret }) } } #[pymethods] impl QueuePy { #[new] #[pyo3(signature = (*elements))] fn init(elements: &Bound<'_, PyTuple>, py: Python<'_>) -> PyResult { let mut ret: QueuePy; if elements.len() == 1 { ret = elements.get_item(0)?.extract()?; } else { ret = QueuePy { inner: Queue::new_sync(), }; if elements.len() > 1 { for each in elements { ret.inner.enqueue_mut(each.into_py(py)); } } } Ok(ret) } fn __eq__(&self, other: &Self, py: Python<'_>) -> bool { (self.inner.len() == other.inner.len()) && self .inner .iter() .zip(other.inner.iter()) .map(|(e1, e2)| e1.bind(py).eq(e2)) .all(|r| r.unwrap_or(false)) } fn __hash__(&self, py: Python<'_>) -> PyResult { let mut hasher = DefaultHasher::new(); self.inner .iter() .enumerate() .try_for_each(|(index, each)| { each.bind(py) .hash() .map_err(|_| { PyTypeError::new_err(format!( "Unhashable type at {} element in Queue: {}", index, each.bind(py) .repr() .and_then(|r| r.extract()) .unwrap_or(" error".to_string()) )) }) .map(|x| hasher.write_isize(x)) })?; Ok(hasher.finish()) } fn __ne__(&self, other: &Self, py: Python<'_>) -> bool { (self.inner.len() != other.inner.len()) || self .inner .iter() .zip(other.inner.iter()) .map(|(e1, e2)| e1.bind(py).ne(e2)) .any(|r| r.unwrap_or(true)) } fn __iter__(slf: PyRef<'_, Self>) -> QueueIterator { QueueIterator { inner: slf.inner.clone(), } } fn __len__(&self) -> usize { self.inner.len() } fn __repr__(&self, py: Python) -> String { let contents = self.inner.into_iter().map(|k| { k.into_py(py) .call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()) }); format!("Queue([{}])", contents.collect::>().join(", ")) } #[getter] fn peek(&self, py: Python) -> PyResult { if let Some(peeked) = self.inner.peek() { Ok(peeked.clone_ref(py)) } else { Err(PyIndexError::new_err("peeked an empty queue")) } } #[getter] fn is_empty(&self) -> bool { self.inner.is_empty() } fn enqueue(&self, value: Bound<'_, PyAny>) -> Self { QueuePy { inner: self.inner.enqueue(value.into()), } } fn dequeue(&self) -> PyResult { if let Some(inner) = self.inner.dequeue() { Ok(QueuePy { inner }) } else { Err(PyIndexError::new_err("dequeued an empty queue")) } } } #[pymodule] #[pyo3(name = "rpds")] fn rpds_py(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?; PyMapping::register::(py)?; let abc = PyModule::import_bound(py, "collections.abc")?; abc.getattr("Set")? .call_method1("register", (HashTrieSetPy::type_object_bound(py),))?; abc.getattr("MappingView")? .call_method1("register", (KeysView::type_object_bound(py),))?; abc.getattr("MappingView")? .call_method1("register", (ValuesView::type_object_bound(py),))?; abc.getattr("MappingView")? .call_method1("register", (ItemsView::type_object_bound(py),))?; abc.getattr("KeysView")? .call_method1("register", (KeysView::type_object_bound(py),))?; abc.getattr("ValuesView")? .call_method1("register", (ValuesView::type_object_bound(py),))?; abc.getattr("ItemsView")? .call_method1("register", (ItemsView::type_object_bound(py),))?; Ok(()) } rpds_py-0.20.0/tests/__init__.py010064400017510000177000000000001465445241300147060ustar 00000000000000rpds_py-0.20.0/tests/requirements.in010064400017510000177000000000321465445241300156550ustar 00000000000000file:.#egg=rpds-py pytest rpds_py-0.20.0/tests/requirements.txt010064400017510000177000000005361465445241300160770ustar 00000000000000# # This file is autogenerated by pip-compile with Python 3.12 # by the following command: # # pip-compile --strip-extras tests/requirements.in # iniconfig==2.0.0 # via pytest packaging==24.1 # via pytest pluggy==1.5.0 # via pytest pytest==8.2.2 # via -r tests/requirements.in file:.#egg=rpds-py # via -r tests/requirements.in rpds_py-0.20.0/tests/test_hash_trie_map.py010064400017510000177000000352451465445241300170340ustar 00000000000000""" Modified from the pyrsistent test suite. Pre-modification, these were MIT licensed, and are copyright: Copyright (c) 2022 Tobias Gustafsson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from collections import abc from operator import methodcaller import pickle import pytest from rpds import HashTrieMap def test_instance_of_hashable(): assert isinstance(HashTrieMap(), abc.Hashable) def test_instance_of_map(): assert isinstance(HashTrieMap(), abc.Mapping) def test_literalish_works(): assert HashTrieMap() == HashTrieMap() assert HashTrieMap(a=1, b=2) == HashTrieMap({"a": 1, "b": 2}) def test_empty_initialization(): a_map = HashTrieMap() assert len(a_map) == 0 def test_initialization_with_one_element(): the_map = HashTrieMap({"a": 2}) assert len(the_map) == 1 assert the_map["a"] == 2 assert "a" in the_map empty_map = the_map.remove("a") assert len(empty_map) == 0 assert "a" not in empty_map def test_index_non_existing_raises_key_error(): m1 = HashTrieMap() with pytest.raises(KeyError) as error: m1["foo"] assert str(error.value) == "'foo'" def test_remove_non_existing_element_raises_key_error(): m1 = HashTrieMap(a=1) with pytest.raises(KeyError) as error: m1.remove("b") assert str(error.value) == "'b'" def test_various_iterations(): assert {"a", "b"} == set(HashTrieMap(a=1, b=2)) assert ["a", "b"] == sorted(HashTrieMap(a=1, b=2).keys()) assert [1, 2] == sorted(HashTrieMap(a=1, b=2).values()) assert {("a", 1), ("b", 2)} == set(HashTrieMap(a=1, b=2).items()) pm = HashTrieMap({k: k for k in range(100)}) assert len(pm) == len(pm.keys()) assert len(pm) == len(pm.values()) assert len(pm) == len(pm.items()) ks = pm.keys() assert all(k in pm for k in ks) assert all(k in ks for k in ks) us = pm.items() assert all(pm[k] == v for (k, v) in us) vs = pm.values() assert all(v in vs for v in vs) def test_initialization_with_two_elements(): map1 = HashTrieMap({"a": 2, "b": 3}) assert len(map1) == 2 assert map1["a"] == 2 assert map1["b"] == 3 map2 = map1.remove("a") assert "a" not in map2 assert map2["b"] == 3 def test_initialization_with_many_elements(): init_dict = {str(x): x for x in range(1700)} the_map = HashTrieMap(init_dict) assert len(the_map) == 1700 assert the_map["16"] == 16 assert the_map["1699"] == 1699 assert the_map.insert("256", 256) == the_map new_map = the_map.remove("1600") assert len(new_map) == 1699 assert "1600" not in new_map assert new_map["1601"] == 1601 # Some NOP properties assert new_map.discard("18888") == new_map assert "19999" not in new_map assert new_map["1500"] == 1500 assert new_map.insert("1500", new_map["1500"]) == new_map def test_access_non_existing_element(): map1 = HashTrieMap() assert len(map1) == 0 map2 = map1.insert("1", 1) assert "1" not in map1 assert map2["1"] == 1 assert "2" not in map2 def test_overwrite_existing_element(): map1 = HashTrieMap({"a": 2}) map2 = map1.insert("a", 3) assert len(map2) == 1 assert map2["a"] == 3 def test_hashing(): o = object() assert hash(HashTrieMap([(o, o), (1, o)])) == hash( HashTrieMap([(o, o), (1, o)]), ) assert hash(HashTrieMap([(o, o), (1, o)])) == hash( HashTrieMap([(1, o), (o, o)]), ) assert hash(HashTrieMap([(o, "foo")])) == hash(HashTrieMap([(o, "foo")])) assert hash(HashTrieMap()) == hash(HashTrieMap([])) assert hash(HashTrieMap({1: 2})) != hash(HashTrieMap({1: 3})) assert hash(HashTrieMap({o: 1})) != hash(HashTrieMap({o: o})) assert hash(HashTrieMap([])) != hash(HashTrieMap([(o, 1)])) assert hash(HashTrieMap({1: 2, 3: 4})) != hash(HashTrieMap({1: 3, 2: 4})) def test_same_hash_when_content_the_same_but_underlying_vector_size_differs(): x = HashTrieMap({x: x for x in range(1000)}) y = HashTrieMap({10: 10, 200: 200, 700: 700}) for z in x: if z not in y: x = x.remove(z) assert x == y # assert hash(x) == hash(y) # noqa: ERA001 class HashabilityControlled: hashable = True def __hash__(self): if self.hashable: return 4 # Proven random raise ValueError("I am not currently hashable.") def test_map_does_not_hash_values_on_second_hash_invocation(): hashable = HashabilityControlled() x = HashTrieMap(dict(el=hashable)) hash(x) hashable.hashable = False with pytest.raises( TypeError, match=r"Unhashable type in HashTrieMap of key 'el'", ): hash(x) def test_equal(): x = HashTrieMap(a=1, b=2, c=3) y = HashTrieMap(a=1, b=2, c=3) assert x == y assert not (x != y) assert y == x assert not (y != x) def test_equal_with_different_insertion_order(): x = HashTrieMap([(i, i) for i in range(50)]) y = HashTrieMap([(i, i) for i in range(49, -1, -1)]) assert x == y assert not (x != y) assert y == x assert not (y != x) def test_not_equal(): x = HashTrieMap(a=1, b=2, c=3) y = HashTrieMap(a=1, b=2) assert x != y assert not (x == y) assert y != x assert not (y == x) def test_not_equal_to_dict(): x = HashTrieMap(a=1, b=2, c=3) y = dict(a=1, b=2, d=4) assert x != y assert not (x == y) assert y != x assert not (y == x) def test_update_with_multiple_arguments(): # If same value is present in multiple sources, the rightmost is used. x = HashTrieMap(a=1, b=2, c=3) y = x.update(HashTrieMap(b=4, c=5), {"c": 6}) assert y == HashTrieMap(a=1, b=4, c=6) def test_update_one_argument(): x = HashTrieMap(a=1) assert x.update({"b": 2}) == HashTrieMap(a=1, b=2) def test_update_no_arguments(): x = HashTrieMap(a=1) assert x.update() == x class HashDummy: def __hash__(self): return 6528039219058920 # Hash of '33' def __eq__(self, other): return self is other def test_iteration_with_many_elements(): values = list(range(2000)) keys = [str(x) for x in values] init_dict = dict(zip(keys, values)) hash_dummy1 = HashDummy() hash_dummy2 = HashDummy() # Throw in a couple of hash collision nodes to tests # those properly as well init_dict[hash_dummy1] = 12345 init_dict[hash_dummy2] = 54321 a_map = HashTrieMap(init_dict) actual_values = set() actual_keys = set() for k, v in a_map.items(): actual_values.add(v) actual_keys.add(k) assert actual_keys == {*keys, hash_dummy1, hash_dummy2} assert actual_values == {*values, 12345, 54321} def test_repr(): rep = repr(HashTrieMap({"foo": "12", "": 37})) assert rep in { "HashTrieMap({'foo': '12', '': 37})", "HashTrieMap({'': 37, 'foo': '12'})", } def test_str(): s = str(HashTrieMap({1: 2, 3: 4})) assert s == "HashTrieMap({1: 2, 3: 4})" or s == "HashTrieMap({3: 4, 1: 2})" def test_empty_truthiness(): assert HashTrieMap(a=1) assert not HashTrieMap() def test_iterable(): m = HashTrieMap((i, i * 2) for i in range(3)) assert m == HashTrieMap({0: 0, 1: 2, 2: 4}) def test_convert_hashtriemap(): m = HashTrieMap({i: i * 2 for i in range(3)}) assert HashTrieMap.convert({i: i * 2 for i in range(3)}) == m def test_fast_convert_hashtriemap(): m = HashTrieMap({i: i * 2 for i in range(3)}) assert HashTrieMap.convert(m) is m # Non-pyrsistent-test-suite tests def test_more_eq(): o = object() assert HashTrieMap([(o, o), (1, o)]) == HashTrieMap([(o, o), (1, o)]) assert HashTrieMap([(o, "foo")]) == HashTrieMap([(o, "foo")]) assert HashTrieMap() == HashTrieMap([]) assert HashTrieMap({1: 2}) != HashTrieMap({1: 3}) assert HashTrieMap({o: 1}) != HashTrieMap({o: o}) assert HashTrieMap([]) != HashTrieMap([(o, 1)]) def test_pickle(): assert pickle.loads( pickle.dumps(HashTrieMap([(1, 2), (3, 4)])), ) == HashTrieMap([(1, 2), (3, 4)]) def test_get(): m1 = HashTrieMap({"foo": "bar"}) assert m1.get("foo") == "bar" assert m1.get("baz") is None assert m1.get("spam", "eggs") == "eggs" @pytest.mark.parametrize( "view", [pytest.param(methodcaller(p), id=p) for p in ["keys", "values", "items"]], ) @pytest.mark.parametrize( "cls", [ abc.Set, abc.MappingView, abc.KeysView, abc.ValuesView, abc.ItemsView, ], ) def test_views_abc(view, cls): m, d = HashTrieMap(), {} assert isinstance(view(m), cls) == isinstance(view(d), cls) def test_keys(): d = HashTrieMap({1: 2, 3: 4}) k = d.keys() assert 1 in k assert 2 not in k assert object() not in k assert len(k) == 2 assert k == d.keys() assert k == HashTrieMap({1: 2, 3: 4}).keys() assert k == {1, 3} assert k != iter({1, 3}) assert k != {1, 2, 3} assert k != {1, 4} assert not k == {1, 4} assert k != object() def test_keys_setlike(): assert {1: 2, 3: 4}.keys() & HashTrieMap({1: 2}).keys() == {1} assert {1: 2, 3: 4}.keys() & HashTrieMap({1: 2}).keys() != {1, 2} assert HashTrieMap({1: 2}).keys() & {1: 2, 3: 4}.keys() == {1} assert HashTrieMap({1: 2}).keys() & {1: 2, 3: 4}.keys() != {2} assert not HashTrieMap({1: 2}).keys() & {}.keys() assert HashTrieMap({1: 2}).keys() & {1} == {1} assert HashTrieMap({1: 2}).keys() & [1] == {1} assert HashTrieMap({1: 2}).keys() | {3} == {1, 3} assert HashTrieMap({1: 2}).keys() | [3] == {1, 3} # these don't really exist on the KeysView protocol but it's nice to have s = (1, "foo") assert HashTrieMap({1: 2, "foo": 7}).keys().intersection(s) == set(s) assert not HashTrieMap({1: 2}).keys().intersection({}) assert HashTrieMap({1: 2}).keys().union({3}) == {1, 3} assert HashTrieMap({1: 2, 3: 4}).keys() < {1, 2, 3} assert HashTrieMap({1: 2, 3: 4}).keys() <= {1, 2, 3} assert not HashTrieMap({1: 2}).keys() < {1} assert HashTrieMap({1: 2}).keys() > set() assert HashTrieMap({1: 2}).keys() >= set() def test_keys_repr(): m = HashTrieMap({"foo": 3, 37: "bar"}) assert repr(m.keys()) in { "keys_view({'foo', 37})", "keys_view({37, 'foo'})", } def test_values(): d = HashTrieMap({1: 2, 3: 4}) v = d.values() assert 2 in v assert 3 not in v assert object() not in v assert len(v) == 2 assert v == v # https://bugs.python.org/issue12445 which was WONTFIXed assert v != HashTrieMap({1: 2, 3: 4}).values() assert v != [2, 4] assert set(v) == {2, 4} def test_values_repr(): m = HashTrieMap({"foo": 3, 37: "bar", "baz": 3}) assert repr(m.values()) in { "values_view(['bar', 3, 3])", "values_view([3, 'bar', 3])", "values_view([3, 3, 'bar'])", } def test_items(): d = HashTrieMap({1: 2, 3: 4}) i = d.items() assert (1, 2) in i assert (1, 4) not in i assert len(i) == 2 assert i == d.items() assert i == HashTrieMap({1: 2, 3: 4}).items() assert i == {(1, 2), (3, 4)} assert i != iter({(1, 2), (3, 4)}) assert i != {(1, 2, 3), (3, 4, 5)} assert i == {1: 2, 3: 4}.items() assert i != {(1, 2), (3, 4), (5, 6)} assert i != {(1, 2)} assert not i == {1, 4} assert i != object() def test_items_setlike(): assert {1: 2, 3: 4}.items() & HashTrieMap({1: 2}).items() == {(1, 2)} assert {1: 2, 3: 4}.items() & HashTrieMap({1: 2}).items() != {(1, 2), 3} assert HashTrieMap({1: 2}).items() & {1: 2, 3: 4}.items() == {(1, 2)} assert HashTrieMap({1: 2}).items() & {1: 2, 3: 4}.items() != {(3, 4)} assert not HashTrieMap({1: 2}).items() & {}.items() assert HashTrieMap({1: 2}).items() & [(1, 2)] == {(1, 2)} assert HashTrieMap({1: 2}).items() & [[1, 2]] == set() assert HashTrieMap({1: 2}).items() | {(3, 4)} == {(1, 2), (3, 4)} assert HashTrieMap({1: 2}).items() | [7] == {(1, 2), 7} s = ((1, 2), ("foo", 37)) assert HashTrieMap({1: 2, "foo": 7}).items().intersection(s) == {(1, 2)} assert not HashTrieMap({1: 2}).items().intersection({}) assert HashTrieMap({1: 2}).items().union({3}) == {(1, 2), 3} assert HashTrieMap({1: 2, 3: 4}).items() < {(1, 2), (3, 4), ("foo", "bar")} assert HashTrieMap({1: 2, 3: 4}).items() <= {(1, 2), (3, 4)} assert not HashTrieMap({1: 2}).keys() < {1} assert HashTrieMap({1: 2}).items() > set() assert HashTrieMap({1: 2}).items() >= set() def test_items_repr(): m = HashTrieMap({"foo": 3, 37: "bar", "baz": 3}) assert repr(m.items()) in { "items_view([('foo', 3), (37, 'bar'), ('baz', 3)])", "items_view([('foo', 3), ('baz', 3), (37, 'bar')])", "items_view([(37, 'bar'), ('foo', 3), ('baz', 3)])", "items_view([(37, 'bar'), ('baz', 3), ('foo', 3)])", "items_view([('baz', 3), (37, 'bar'), ('foo', 3)])", "items_view([('baz', 3), ('foo', 3), (37, 'bar')])", } def test_fromkeys(): keys = list(range(10)) got = HashTrieMap.fromkeys(keys) expected = HashTrieMap((i, None) for i in keys) assert got == HashTrieMap(dict.fromkeys(keys)) == expected def test_fromkeys_explicit_value(): keys = list(range(10)) expected = HashTrieMap((i, "foo") for i in keys) got = HashTrieMap.fromkeys(keys, "foo") expected = HashTrieMap((i, "foo") for i in keys) assert got == HashTrieMap(dict.fromkeys(keys, "foo")) == expected def test_fromkeys_explicit_value_not_copied(): keys = list(range(5)) got = HashTrieMap.fromkeys(keys, []) got[3].append(1) assert got == HashTrieMap((i, [1]) for i in keys) def test_update_with_iterable_of_kvs(): assert HashTrieMap({1: 2}).update(iter([(3, 4), ("5", 6)])) == HashTrieMap( { 1: 2, 3: 4, "5": 6, }, ) rpds_py-0.20.0/tests/test_hash_trie_set.py010064400017510000177000000137161465445241300170510ustar 00000000000000""" Modified from the pyrsistent test suite. Pre-modification, these were MIT licensed, and are copyright: Copyright (c) 2022 Tobias Gustafsson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from collections import abc import pickle import pytest from rpds import HashTrieSet def test_key_is_tuple(): with pytest.raises(KeyError): HashTrieSet().remove((1, 1)) def test_key_is_not_tuple(): with pytest.raises(KeyError): HashTrieSet().remove("asdf") def test_hashing(): o = object() assert hash(HashTrieSet([o])) == hash(HashTrieSet([o])) assert hash(HashTrieSet([o, o])) == hash(HashTrieSet([o, o])) assert hash(HashTrieSet([])) == hash(HashTrieSet([])) assert hash(HashTrieSet([1, 2])) == hash(HashTrieSet([1, 2])) assert hash(HashTrieSet([1, 2])) == hash(HashTrieSet([2, 1])) assert not (HashTrieSet([1, 2]) == HashTrieSet([1, 3])) assert not (HashTrieSet([]) == HashTrieSet([o])) assert hash(HashTrieSet([1, 2])) != hash(HashTrieSet([1, 3])) assert hash(HashTrieSet([1, o])) != hash(HashTrieSet([1, 2])) assert hash(HashTrieSet([1, 2])) != hash(HashTrieSet([2, 1, 3])) assert not (HashTrieSet([o]) != HashTrieSet([o, o])) assert not (HashTrieSet([o, o]) != HashTrieSet([o, o])) assert not (HashTrieSet() != HashTrieSet([])) def test_empty_truthiness(): assert HashTrieSet([1]) assert not HashTrieSet() def test_contains_elements_that_it_was_initialized_with(): initial = [1, 2, 3] s = HashTrieSet(initial) assert set(s) == set(initial) assert len(s) == len(set(initial)) def test_is_immutable(): s1 = HashTrieSet([1]) s2 = s1.insert(2) assert s1 == HashTrieSet([1]) assert s2 == HashTrieSet([1, 2]) s3 = s2.remove(1) assert s2 == HashTrieSet([1, 2]) assert s3 == HashTrieSet([2]) def test_remove_when_not_present(): s1 = HashTrieSet([1, 2, 3]) with pytest.raises(KeyError): s1.remove(4) def test_discard(): s1 = HashTrieSet((1, 2, 3)) assert s1.discard(3) == HashTrieSet((1, 2)) assert s1.discard(4) == s1 def test_is_iterable(): assert sum(HashTrieSet([1, 2, 3])) == 6 def test_contains(): s = HashTrieSet([1, 2, 3]) assert 2 in s assert 4 not in s def test_supports_set_operations(): s1 = HashTrieSet([1, 2, 3]) s2 = HashTrieSet([3, 4, 5]) assert s1 | s2 == HashTrieSet([1, 2, 3, 4, 5]) assert s1.union(s2) == s1 | s2 assert s1 & s2 == HashTrieSet([3]) assert s1.intersection(s2) == s1 & s2 assert s1 - s2 == HashTrieSet([1, 2]) assert s1.difference(s2) == s1 - s2 assert s1 ^ s2 == HashTrieSet([1, 2, 4, 5]) assert s1.symmetric_difference(s2) == s1 ^ s2 def test_supports_set_comparisons(): s1 = HashTrieSet([1, 2, 3]) s3 = HashTrieSet([1, 2]) s4 = HashTrieSet([1, 2, 3]) assert HashTrieSet([1, 2, 3, 3, 5]) == HashTrieSet([1, 2, 3, 5]) assert s1 != s3 assert s3 < s1 assert s3 <= s1 assert s3 <= s4 assert s1 > s3 assert s1 >= s3 assert s4 >= s3 def test_repr(): rep = repr(HashTrieSet([1, 2])) assert rep == "HashTrieSet({1, 2})" or rep == "HashTrieSet({2, 1})" rep = repr(HashTrieSet(["1", "2"])) assert rep == "HashTrieSet({'1', '2'})" or rep == "HashTrieSet({'2', '1'})" def test_update(): assert HashTrieSet([1, 2, 3]).update([3, 4, 4, 5]) == HashTrieSet( [1, 2, 3, 4, 5], ) def test_update_no_elements(): s1 = HashTrieSet([1, 2]) assert s1.update([]) == s1 def test_iterable(): assert HashTrieSet(iter("a")) == HashTrieSet(iter("a")) def test_more_eq(): # Non-pyrsistent-test-suite test o = object() assert HashTrieSet([o]) == HashTrieSet([o]) assert HashTrieSet([o, o]) == HashTrieSet([o, o]) assert HashTrieSet([o]) == HashTrieSet([o, o]) assert HashTrieSet() == HashTrieSet([]) assert not (HashTrieSet([1, 2]) == HashTrieSet([1, 3])) assert not (HashTrieSet([o, 1]) == HashTrieSet([o, o])) assert not (HashTrieSet([]) == HashTrieSet([o])) assert HashTrieSet([1, 2]) != HashTrieSet([1, 3]) assert HashTrieSet([]) != HashTrieSet([o]) assert not (HashTrieSet([o]) != HashTrieSet([o])) assert not (HashTrieSet([o, o]) != HashTrieSet([o, o])) assert not (HashTrieSet([o]) != HashTrieSet([o, o])) assert not (HashTrieSet() != HashTrieSet([])) assert HashTrieSet([1, 2]) == {1, 2} assert HashTrieSet([1, 2]) != {1, 2, 3} assert HashTrieSet([1, 2]) != [1, 2] def test_more_set_comparisons(): s = HashTrieSet([1, 2, 3]) assert s == s assert not (s < s) assert s <= s assert not (s > s) assert s >= s def test_pickle(): assert pickle.loads( pickle.dumps(HashTrieSet([1, 2, 3, 4])), ) == HashTrieSet([1, 2, 3, 4]) def test_instance_of_set(): assert isinstance(HashTrieSet(), abc.Set) def test_lt_le_gt_ge(): assert HashTrieSet({}) < {1} assert HashTrieSet({}) <= {1} assert HashTrieSet({1}) > set() assert HashTrieSet({1}) >= set() rpds_py-0.20.0/tests/test_list.py010064400017510000177000000103431465445241300151740ustar 00000000000000""" Modified from the pyrsistent test suite. Pre-modification, these were MIT licensed, and are copyright: Copyright (c) 2022 Tobias Gustafsson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import pickle import pytest from rpds import List def test_literalish_works(): assert List(1, 2, 3) == List([1, 2, 3]) def test_first_and_rest(): pl = List([1, 2]) assert pl.first == 1 assert pl.rest.first == 2 assert pl.rest.rest == List() def test_instantiate_large_list(): assert List(range(1000)).first == 0 def test_iteration(): assert list(List()) == [] assert list(List([1, 2, 3])) == [1, 2, 3] def test_push_front(): assert List([1, 2, 3]).push_front(0) == List([0, 1, 2, 3]) def test_push_front_empty_list(): assert List().push_front(0) == List([0]) def test_truthiness(): assert List([1]) assert not List() def test_len(): assert len(List([1, 2, 3])) == 3 assert len(List()) == 0 def test_first_illegal_on_empty_list(): with pytest.raises(IndexError): List().first def test_rest_return_self_on_empty_list(): assert List().rest == List() def test_reverse(): assert reversed(List([1, 2, 3])) == List([3, 2, 1]) assert reversed(List()) == List() def test_inequality(): assert List([1, 2]) != List([1, 3]) assert List([1, 2]) != List([1, 2, 3]) assert List() != List([1, 2, 3]) def test_repr(): assert str(List()) == "List([])" assert str(List([1, 2, 3])) in "List([1, 2, 3])" def test_hashing(): o = object() assert hash(List([o, o])) == hash(List([o, o])) assert hash(List([o])) == hash(List([o])) assert hash(List()) == hash(List([])) assert not (hash(List([1, 2])) == hash(List([1, 3]))) assert not (hash(List([1, 2])) == hash(List([2, 1]))) assert not (hash(List([o])) == hash(List([o, o]))) assert not (hash(List([])) == hash(List([o]))) assert hash(List([1, 2])) != hash(List([1, 3])) assert hash(List([1, 2])) != hash(List([2, 1])) assert hash(List([o])) != hash(List([o, o])) assert hash(List([])) != hash(List([o])) assert not (hash(List([o, o])) != hash(List([o, o]))) assert not (hash(List([o])) != hash(List([o]))) assert not (hash(List([])) != hash(List([]))) def test_sequence(): m = List("asdf") assert m == List(["a", "s", "d", "f"]) # Non-pyrsistent-test-suite tests def test_drop_first(): assert List([1, 2, 3]).drop_first() == List([2, 3]) def test_drop_first_empty(): """ rpds itself returns an Option here but we try IndexError instead. """ with pytest.raises(IndexError): List([]).drop_first() def test_more_eq(): o = object() assert List([o, o]) == List([o, o]) assert List([o]) == List([o]) assert List() == List([]) assert not (List([1, 2]) == List([1, 3])) assert not (List([o]) == List([o, o])) assert not (List([]) == List([o])) assert List([1, 2]) != List([1, 3]) assert List([o]) != List([o, o]) assert List([]) != List([o]) assert not (List([o, o]) != List([o, o])) assert not (List([o]) != List([o])) assert not (List() != List([])) def test_pickle(): assert pickle.loads(pickle.dumps(List([1, 2, 3, 4]))) == List([1, 2, 3, 4]) rpds_py-0.20.0/tests/test_queue.py010064400017510000177000000071121465445241300153450ustar 00000000000000""" Modified from the pyrsistent test suite. Pre-modification, these were MIT licensed, and are copyright: Copyright (c) 2022 Tobias Gustafsson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import pytest from rpds import Queue def test_literalish_works(): assert Queue(1, 2, 3) == Queue([1, 2, 3]) def test_peek_dequeue(): pl = Queue([1, 2]) assert pl.peek == 1 assert pl.dequeue().peek == 2 assert pl.dequeue().dequeue().is_empty with pytest.raises(IndexError): pl.dequeue().dequeue().dequeue() def test_instantiate_large_list(): assert Queue(range(1000)).peek == 0 def test_iteration(): assert list(Queue()) == [] assert list(Queue([1, 2, 3])) == [1, 2, 3] def test_enqueue(): assert Queue([1, 2, 3]).enqueue(4) == Queue([1, 2, 3, 4]) def test_enqueue_empty_list(): assert Queue().enqueue(0) == Queue([0]) def test_truthiness(): assert Queue([1]) assert not Queue() def test_len(): assert len(Queue([1, 2, 3])) == 3 assert len(Queue()) == 0 def test_peek_illegal_on_empty_list(): with pytest.raises(IndexError): Queue().peek def test_inequality(): assert Queue([1, 2]) != Queue([1, 3]) assert Queue([1, 2]) != Queue([1, 2, 3]) assert Queue() != Queue([1, 2, 3]) def test_repr(): assert str(Queue()) == "Queue([])" assert str(Queue([1, 2, 3])) in "Queue([1, 2, 3])" def test_sequence(): m = Queue("asdf") assert m == Queue(["a", "s", "d", "f"]) # Non-pyrsistent-test-suite tests def test_dequeue(): assert Queue([1, 2, 3]).dequeue() == Queue([2, 3]) def test_dequeue_empty(): """ rpds itself returns an Option here but we try IndexError instead. """ with pytest.raises(IndexError): Queue([]).dequeue() def test_more_eq(): o = object() assert Queue([o, o]) == Queue([o, o]) assert Queue([o]) == Queue([o]) assert Queue() == Queue([]) assert not (Queue([1, 2]) == Queue([1, 3])) assert not (Queue([o]) == Queue([o, o])) assert not (Queue([]) == Queue([o])) assert Queue([1, 2]) != Queue([1, 3]) assert Queue([o]) != Queue([o, o]) assert Queue([]) != Queue([o]) assert not (Queue([o, o]) != Queue([o, o])) assert not (Queue([o]) != Queue([o])) assert not (Queue() != Queue([])) def test_hashing(): assert hash(Queue([1, 2])) == hash(Queue([1, 2])) assert hash(Queue([1, 2])) != hash(Queue([2, 1])) assert len({Queue([1, 2]), Queue([1, 2])}) == 1 def test_unhashable_contents(): q = Queue([1, {1}]) with pytest.raises(TypeError): hash(q) rpds_py-0.20.0/Cargo.lock010064400017510000177000000132001465445241300133460ustar 00000000000000# This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "archery" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8967cd1cc9e9e1954f644e14fbd6042fe9a37da96c52a67e44a2ac18261f8561" dependencies = [ "static_assertions", "triomphe", ] [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "cc" version = "1.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "indoc" version = "2.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "memoffset" version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" dependencies = [ "autocfg", ] [[package]] name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "portable-atomic" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" [[package]] name = "proc-macro2" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "pyo3" version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "831e8e819a138c36e212f3af3fd9eeffed6bf1510a805af35b0edee5ffa59433" dependencies = [ "cfg-if", "indoc", "libc", "memoffset", "once_cell", "portable-atomic", "pyo3-build-config", "pyo3-ffi", "pyo3-macros", "unindent", ] [[package]] name = "pyo3-build-config" version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e8730e591b14492a8945cdff32f089250b05f5accecf74aeddf9e8272ce1fa8" dependencies = [ "once_cell", "python3-dll-a", "target-lexicon", ] [[package]] name = "pyo3-ffi" version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e97e919d2df92eb88ca80a037969f44e5e70356559654962cbb3316d00300c6" dependencies = [ "libc", "pyo3-build-config", ] [[package]] name = "pyo3-macros" version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb57983022ad41f9e683a599f2fd13c3664d7063a3ac5714cae4b7bee7d3f206" dependencies = [ "proc-macro2", "pyo3-macros-backend", "quote", "syn", ] [[package]] name = "pyo3-macros-backend" version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec480c0c51ddec81019531705acac51bcdbeae563557c982aa8263bb96880372" dependencies = [ "heck", "proc-macro2", "pyo3-build-config", "quote", "syn", ] [[package]] name = "python3-dll-a" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd0b78171a90d808b319acfad166c4790d9e9759bbc14ac8273fe133673dd41b" dependencies = [ "cc", ] [[package]] name = "quote" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "rpds" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0e15515d3ce3313324d842629ea4905c25a13f81953eadb88f85516f59290a4" dependencies = [ "archery", ] [[package]] name = "rpds-py" version = "0.20.0" dependencies = [ "archery", "pyo3", "rpds", ] [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "syn" version = "2.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201fcda3845c23e8212cd466bfebf0bd20694490fc0356ae8e428e0824a915a6" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "target-lexicon" version = "0.12.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" [[package]] name = "triomphe" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6631e42e10b40c0690bf92f404ebcfe6e1fdb480391d15f17cc8e96eeed5369" [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "unindent" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" rpds_py-0.20.0/pyproject.toml010064400017510000177000000110241465445241300143570ustar 00000000000000[build-system] requires = ["maturin>=1.2,<2.0"] build-backend = "maturin" [project] name = "rpds-py" description = "Python bindings to Rust's persistent data structures (rpds)" requires-python = ">=3.8" readme = "README.rst" license = {text = "MIT"} keywords = ["data structures", "rust", "persistent"] authors = [ { name = "Julian Berman", email = "Julian+rpds@GrayVines.com" }, ] classifiers = [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Rust", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", ] dynamic = ["version"] [project.urls] Documentation = "https://rpds.readthedocs.io/" Homepage = "https://github.com/crate-py/rpds" Issues = "https://github.com/crate-py/rpds/issues/" Funding = "https://github.com/sponsors/Julian" Tidelift = "https://tidelift.com/subscription/pkg/pypi-rpds-py?utm_source=pypi-rpds-py&utm_medium=referral&utm_campaign=pypi-link" Source = "https://github.com/crate-py/rpds" [tool.black] line-length = 79 [tool.coverage.html] show_contexts = true skip_covered = false [tool.coverage.run] branch = true dynamic_context = "test_function" [tool.coverage.report] exclude_also = [ "if TYPE_CHECKING:", "\\s*\\.\\.\\.\\s*", ] fail_under = 100 show_missing = true skip_covered = true [tool.doc8] ignore = [ "D000", # see PyCQA/doc8#125 "D001", # one sentence per line, so max length doesn't make sense ] [tool.maturin] features = ["pyo3/extension-module"] [tool.pyright] reportUnnecessaryTypeIgnoreComment = true strict = ["**/*"] exclude = [ "**/tests/__init__.py", "**/tests/test_*.py", ] [tool.ruff] line-length = 79 [tool.ruff.lint] select = ["ALL"] ignore = [ "A001", # It's fine to shadow builtins "A002", "A003", "ARG", # This is all wrong whenever an interface is involved "ANN", # Just let the type checker do this "B006", # Mutable arguments require care but are OK if you don't abuse them "B008", # It's totally OK to call functions for default arguments. "B904", # raise SomeException(...) is fine. "B905", # No need for explicit strict, this is simply zip's default behavior "C408", # Calling dict is fine when it saves quoting the keys "C901", # Not really something to focus on "D105", # It's fine to not have docstrings for magic methods. "D107", # __init__ especially doesn't need a docstring "D200", # This rule makes diffs uglier when expanding docstrings "D203", # No blank lines before docstrings. "D212", # Start docstrings on the second line. "D400", # This rule misses sassy docstrings ending with ! or ? "D401", # This rule is too flaky. "D406", # Section headers should end with a colon not a newline "D407", # Underlines aren't needed "D412", # Plz spaces after section headers "EM101", # These don't bother me, it's fine there's some duplication. "EM102", "FBT", # It's worth avoiding boolean args but I don't care to enforce it "FIX", # Yes thanks, if I could it wouldn't be there "N", # These naming rules are silly "PLR0912", # These metrics are fine to be aware of but not to enforce "PLR0913", "PLR0915", "PLW2901", # Shadowing for loop variables is occasionally fine. "PT006", # pytest parametrize takes strings as well "PYI025", # wat, I'm not confused, thanks. "RET502", # Returning None implicitly is fine "RET503", "RET505", # These push you to use `if` instead of `elif`, but for no reason "RET506", "RSE102", # Ha, what, who even knew you could leave the parens off. But no. "SIM300", # Not sure what heuristic this uses, but it's easily incorrect "SLF001", # Private usage within this package itself is fine "TD", # These TODO style rules are also silly "UP007", # We support 3.8 + 3.9 ] [tool.ruff.lint.flake8-pytest-style] mark-parentheses = false [tool.ruff.lint.flake8-quotes] docstring-quotes = "double" [tool.ruff.lint.isort] combine-as-imports = true from-first = true known-first-party = ["rpds"] [tool.ruff.lint.per-file-ignores] "noxfile.py" = ["ANN", "D100", "S101", "T201"] "docs/*" = ["ANN", "D", "INP001"] "tests/*" = ["ANN", "B018", "D", "PLR", "RUF012", "S", "SIM", "TRY"] rpds_py-0.20.0/PKG-INFO000064400000010124000000000000007653ustar Metadata-Version: 2.3 Name: rpds-py Version: 0.20.0 Classifier: Development Status :: 3 - Alpha Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Rust Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: 3.11 Classifier: Programming Language :: Python :: 3.12 Classifier: Programming Language :: Python :: 3.13 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy License-File: LICENSE Summary: Python bindings to Rust's persistent data structures (rpds) Keywords: data structures,rust,persistent Author-email: Julian Berman License: MIT Requires-Python: >=3.8 Description-Content-Type: text/x-rst; charset=UTF-8 Project-URL: Documentation, https://rpds.readthedocs.io/ Project-URL: Homepage, https://github.com/crate-py/rpds Project-URL: Issues, https://github.com/crate-py/rpds/issues/ Project-URL: Funding, https://github.com/sponsors/Julian Project-URL: Tidelift, https://tidelift.com/subscription/pkg/pypi-rpds-py?utm_source=pypi-rpds-py&utm_medium=referral&utm_campaign=pypi-link Project-URL: Source, https://github.com/crate-py/rpds =========== ``rpds.py`` =========== |PyPI| |Pythons| |CI| .. |PyPI| image:: https://img.shields.io/pypi/v/rpds-py.svg :alt: PyPI version :target: https://pypi.org/project/rpds-py/ .. |Pythons| image:: https://img.shields.io/pypi/pyversions/rpds-py.svg :alt: Supported Python versions :target: https://pypi.org/project/rpds-py/ .. |CI| image:: https://github.com/crate-py/rpds/workflows/CI/badge.svg :alt: Build status :target: https://github.com/crate-py/rpds/actions?query=workflow%3ACI .. |ReadTheDocs| image:: https://readthedocs.org/projects/referencing/badge/?version=stable&style=flat :alt: ReadTheDocs status :target: https://referencing.readthedocs.io/en/stable/ Python bindings to the `Rust rpds crate `_ for persistent data structures. What's here is quite minimal (in transparency, it was written initially to support replacing ``pyrsistent`` in the `referencing library `_). If you see something missing (which is very likely), a PR is definitely welcome to add it. Installation ------------ The distribution on PyPI is named ``rpds.py`` (equivalently ``rpds-py``), and thus can be installed via e.g.: .. code:: sh $ pip install rpds-py Note that if you install ``rpds-py`` from source, you will need a Rust toolchain installed, as it is a build-time dependency. An example of how to do so in a ``Dockerfile`` can be found `here `_. If you believe you are on a common platform which should have wheels built (i.e. and not need to compile from source), feel free to file an issue or pull request modifying the GitHub action used here to build wheels via ``maturin``. Usage ----- Methods in general are named similarly to their ``rpds`` counterparts (rather than ``pyrsistent``\ 's conventions, though probably a full drop-in ``pyrsistent``\ -compatible wrapper module is a good addition at some point). .. code:: python >>> from rpds import HashTrieMap, HashTrieSet, List >>> m = HashTrieMap({"foo": "bar", "baz": "quux"}) >>> m.insert("spam", 37) == HashTrieMap({"foo": "bar", "baz": "quux", "spam": 37}) True >>> m.remove("foo") == HashTrieMap({"baz": "quux"}) True >>> s = HashTrieSet({"foo", "bar", "baz", "quux"}) >>> s.insert("spam") == HashTrieSet({"foo", "bar", "baz", "quux", "spam"}) True >>> s.remove("foo") == HashTrieSet({"bar", "baz", "quux"}) True >>> L = List([1, 3, 5]) >>> L.push_front(-1) == List([-1, 1, 3, 5]) True >>> L.rest == List([3, 5]) True