rpds_py-0.27.1/Cargo.toml000064400000000623000000000000010521ustar [package] name = "rpds-py" version = "0.27.1" edition = "2021" [lib] name = "rpds" crate-type = ["cdylib"] [dependencies] rpds = "1.1.1" archery = "1.2.1" [dependencies.pyo3] version = "0.25.1" # 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.27.1/.github/SECURITY.md010064400017510000166000000011601505357214200145760ustar 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.27.1/.github/dependabot.yml010064400017510000166000000003201505357214200156320ustar 00000000000000version: 2 updates: - package-ecosystem: "github-actions" directory: "/" schedule: interval: "weekly" - package-ecosystem: "cargo" directory: "/" schedule: interval: "weekly" rpds_py-0.27.1/.github/release.yml010064400017510000166000000001141505357214200151460ustar 00000000000000changelog: exclude: authors: - dependabot - pre-commit-ci rpds_py-0.27.1/.github/workflows/CI.yml010064400017510000166000000263551505357214200160750ustar 00000000000000name: CI on: push: branches-ignore: - "wip*" tags: - "v[0-9].*" pull_request: schedule: # Daily at 5:33 - cron: "33 5 * * *" workflow_dispatch: permissions: {} jobs: list: runs-on: ubuntu-latest outputs: noxenvs: ${{ steps.noxenvs-matrix.outputs.noxenvs }} steps: - uses: actions/checkout@v5 with: persist-credentials: false - uses: astral-sh/setup-uv@4959332f0f014c5280e7eac8b70c90cb574c9f9b with: enable-cache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] - id: noxenvs-matrix run: | echo >>$GITHUB_OUTPUT noxenvs=$( uvx 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@v5 with: persist-credentials: false - 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.9 3.10 3.11 3.12 3.13 3.13t 3.14 3.14t pypy3.9 pypy3.10 pypy3.11 allow-prereleases: true - uses: astral-sh/setup-uv@4959332f0f014c5280e7eac8b70c90cb574c9f9b with: enable-cache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] - name: Run nox run: uvx nox -s "${{ matrix.noxenv }}" -- ${{ matrix.posargs }} # zizmor: ignore[template-injection] manylinux: needs: test runs-on: ubuntu-latest strategy: fail-fast: false matrix: target: [ x86_64, x86, aarch64, armv7, s390x, ppc64le, riscv64gc-unknown-linux-gnu, ] steps: - uses: actions/checkout@v5 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: | 3.9 3.10 3.11 3.12 3.13 3.13t 3.14 3.14t pypy3.9 pypy3.10 pypy3.11 allow-prereleases: true - name: Build wheels uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.9 3.10 3.11 3.12 3.13 3.13t 3.14 3.14t pypy3.9 pypy3.10 pypy3.11' sccache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] 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: fail-fast: false matrix: target: - aarch64-unknown-linux-musl - i686-unknown-linux-musl - x86_64-unknown-linux-musl steps: - uses: actions/checkout@v5 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: | 3.9 3.10 3.11 3.12 3.13 3.13t 3.14 3.14t pypy3.9 pypy3.10 pypy3.11 allow-prereleases: true - name: Build wheels uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.9 3.10 3.11 3.12 3.13 3.13t 3.14 3.14t pypy3.9 pypy3.10 pypy3.11' manylinux: musllinux_1_2 sccache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] - 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: fail-fast: false matrix: target: [x64, x86] # x86 is not supported by pypy steps: - uses: actions/checkout@v5 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: | 3.9 3.10 3.11 3.12 3.13 3.14 ${{ matrix.target == 'x64' && 'pypy3.9' || '' }} ${{ matrix.target == 'x64' && 'pypy3.10' || '' }} allow-prereleases: true architecture: ${{ matrix.target }} - name: Build wheels uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.9 3.10 3.11 3.12 3.13 3.14' --interpreter ${{ matrix.target == 'x64' && 'pypy3.9 pypy3.10' || '' }} sccache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] - name: Upload wheels uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }}-${{ matrix.target }} path: dist windows-arm: needs: test runs-on: windows-11-arm strategy: fail-fast: false matrix: target: - aarch64-pc-windows-msvc steps: - uses: actions/checkout@v5 with: persist-credentials: false # Install each python version seperatly so that the paths can be passed to maturin. (otherwise finds pre-installed x64 versions) - uses: actions/setup-python@v5 id: cp311 with: python-version: 3.11 allow-prereleases: true architecture: arm64 - uses: actions/setup-python@v5 id: cp312 with: python-version: 3.12 allow-prereleases: true architecture: arm64 - uses: actions/setup-python@v5 id: cp313 with: python-version: 3.13 allow-prereleases: true architecture: arm64 - uses: actions/setup-python@v5 id: cp314 with: python-version: 3.14 allow-prereleases: true architecture: arm64 # rust toolchain is not currently installed on windopws arm64 images: https://github.com/actions/partner-runner-images/issues/77 - name: Setup rust id: setup-rust run: | Invoke-WebRequest https://static.rust-lang.org/rustup/dist/aarch64-pc-windows-msvc/rustup-init.exe -OutFile .\rustup-init.exe .\rustup-init.exe -y Add-Content $env:GITHUB_PATH "$env:USERPROFILE\.cargo\bin" - name: Build wheels uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4 with: target: ${{ matrix.target }} args: --release --out dist --interpreter ${{ steps.cp311.outputs.python-path }} ${{ steps.cp312.outputs.python-path }} ${{ steps.cp313.outputs.python-path }} ${{ steps.cp314.outputs.python-path }} sccache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] - name: Upload wheels uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }}-${{ matrix.target }} path: dist # free-threaded and normal builds share a site-packages folder on Windows so # we must build free-threaded separately windows-free-threaded: needs: test runs-on: windows-latest strategy: fail-fast: false matrix: target: [x64, x86] # x86 is not supported by pypy steps: - uses: actions/checkout@v5 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: | 3.13t 3.14t allow-prereleases: true architecture: ${{ matrix.target }} - name: Build wheels uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.13t 3.14t' sccache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] - name: Upload wheels uses: actions/upload-artifact@v4 with: name: dist-${{ github.job }}-${{ matrix.target }}-free-threaded path: dist macos: needs: test runs-on: macos-latest strategy: fail-fast: false matrix: target: [x86_64, aarch64] steps: - uses: actions/checkout@v5 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: | 3.9 3.10 3.11 3.12 3.13 3.13t 3.14 3.14t pypy3.9 pypy3.10 pypy3.11 allow-prereleases: true - name: Build wheels uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4 with: target: ${{ matrix.target }} args: --release --out dist --interpreter '3.9 3.10 3.11 3.12 3.13 3.13t 3.14 3.14t pypy3.9 pypy3.10 pypy3.11' sccache: ${{ github.ref_type != 'tag' }} # zizmor: ignore[cache-poisoning] - 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@v5 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: 3.13 - name: Build an sdist uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4 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, windows-arm, windows-free-threaded, 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@v5 with: pattern: dist-* merge-multiple: true path: dist/ - name: Publish to PyPI uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4 with: skip-existing: true print-hash: true packages-dir: dist - name: Create a GitHub Release if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') uses: softprops/action-gh-release@72f2c25fcb47643c292f7107632f7a47c1df5cd8 with: files: | dist/* generate_release_notes: true rpds_py-0.27.1/.github/workflows/zizmor.yml010064400017510000166000000014701505357214200171230ustar 00000000000000name: GitHub Actions Security Analysis with zizmor 🌈 on: push: branches: ["main"] pull_request: branches: ["**"] permissions: {} jobs: zizmor: runs-on: ubuntu-latest permissions: security-events: write steps: - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 with: persist-credentials: false - uses: astral-sh/setup-uv@4959332f0f014c5280e7eac8b70c90cb574c9f9b # v6.6.0 - name: Run zizmor 🌈 run: uvx zizmor --format=sarif . > results.sarif env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Upload SARIF file uses: github/codeql-action/upload-sarif@3c3833e0f8c1c83d449a7478aa59c036a9165498 # v3.29.11 with: sarif_file: results.sarif category: zizmor rpds_py-0.27.1/.github/zizmor.yml010064400017510000166000000002151505357214200150620ustar 00000000000000rules: template-injection: ignore: # our matrix is dynamically generated via `nox -l` but with no user input - CI.yml:71:9 rpds_py-0.27.1/.gitignore010064400017510000166000000013241505357214200134370ustar 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.27.1/.pre-commit-config.yaml010064400017510000166000000015551505357214200157360ustar 00000000000000ci: skip: # pre-commit.ci doesn't have Rust installed - fmt - clippy - zizmor repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.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: 25.1.0 hooks: - id: black - repo: https://github.com/pre-commit/mirrors-prettier rev: "v4.0.0-alpha.8" hooks: - id: prettier - repo: https://github.com/woodruffw/zizmor rev: v0.8.0 hooks: - id: zizmor rpds_py-0.27.1/.readthedocs.yml010064400017510000166000000003611505357214200145350ustar 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.27.1/Cargo.lock010064400017510000166000000126631505357214200133640ustar 00000000000000# This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "archery" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eae2ed21cd55021f05707a807a5fc85695dafb98832921f6cfa06db67ca5b869" dependencies = [ "triomphe", ] [[package]] name = "autocfg" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "cc" version = "1.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0fc897dc1e865cc67c0e05a836d9d3f1df3cbe442aa4a9473b18e12624a4951" dependencies = [ "shlex", ] [[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.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd" [[package]] name = "libc" version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" [[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.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "portable-atomic" version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" [[package]] name = "proc-macro2" version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] [[package]] name = "pyo3" version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8970a78afe0628a3e3430376fc5fd76b6b45c4d43360ffd6cdd40bdde72b682a" dependencies = [ "indoc", "libc", "memoffset", "once_cell", "portable-atomic", "pyo3-build-config", "pyo3-ffi", "pyo3-macros", "unindent", ] [[package]] name = "pyo3-build-config" version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "458eb0c55e7ece017adeba38f2248ff3ac615e53660d7c71a238d7d2a01c7598" dependencies = [ "once_cell", "python3-dll-a", "target-lexicon", ] [[package]] name = "pyo3-ffi" version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7114fe5457c61b276ab77c5055f206295b812608083644a5c5b2640c3102565c" dependencies = [ "libc", "pyo3-build-config", ] [[package]] name = "pyo3-macros" version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8725c0a622b374d6cb051d11a0983786448f7785336139c3c94f5aa6bef7e50" dependencies = [ "proc-macro2", "pyo3-macros-backend", "quote", "syn", ] [[package]] name = "pyo3-macros-backend" version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4109984c22491085343c05b0dbc54ddc405c3cf7b4374fc533f5c3313a572ccc" dependencies = [ "heck", "proc-macro2", "pyo3-build-config", "quote", "syn", ] [[package]] name = "python3-dll-a" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d381ef313ae70b4da5f95f8a4de773c6aa5cd28f73adec4b4a31df70b66780d8" dependencies = [ "cc", ] [[package]] name = "quote" version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] [[package]] name = "rpds" version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7f89f654d51fffdd6026289d07d1fd523244d46ae0a8bc22caa6dd7f9e8cb0b" dependencies = [ "archery", ] [[package]] name = "rpds-py" version = "0.27.1" dependencies = [ "archery", "pyo3", "rpds", ] [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "syn" version = "2.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "target-lexicon" version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e502f78cdbb8ba4718f566c418c52bc729126ffd16baee5baa718cf25dd5a69a" [[package]] name = "triomphe" version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef8f7726da4807b58ea5c96fdc122f80702030edc33b35aff9190a51148ccc85" [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "unindent" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3" rpds_py-0.27.1/LICENSE010064400017510000166000000020411505357214200124510ustar 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.27.1/README.rst010064400017510000166000000052161505357214200131420ustar 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.27.1/docs/api.rst010064400017510000166000000002511505357214200137000ustar 00000000000000API Reference ============= .. automodule:: rpds :members: :undoc-members: :imported-members: :special-members: __iter__, __getitem__, __len__, __rmatmul__ rpds_py-0.27.1/docs/conf.py010064400017510000166000000032151505357214200136770ustar 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.27.1/docs/index.rst010064400017510000166000000040061505357214200142400ustar 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.27.1/docs/requirements.in010064400017510000166000000001761505357214200154560ustar 00000000000000file:.#egg=rpds-py furo pygments-github-lexers sphinx-copybutton sphinx>5 sphinxcontrib-spelling>5 sphinxext-opengraph url.py rpds_py-0.27.1/docs/requirements.txt010064400017510000166000000035361505357214200156720ustar 00000000000000# This file was autogenerated by uv via the following command: # uv pip compile --output-file /Users/julian/Development/rpds.py/docs/requirements.txt docs/requirements.in alabaster==1.0.0 # via sphinx babel==2.17.0 # via sphinx beautifulsoup4==4.13.4 # via furo certifi==2025.6.15 # via requests charset-normalizer==3.4.2 # via requests docutils==0.21.2 # via sphinx furo==2024.8.6 # via -r docs/requirements.in idna==3.10 # via requests imagesize==1.4.1 # via sphinx jinja2==3.1.6 # via sphinx markupsafe==3.0.2 # via jinja2 packaging==25.0 # via sphinx pyenchant==3.2.2 # via sphinxcontrib-spelling pygments==2.19.2 # via # furo # pygments-github-lexers # sphinx pygments-github-lexers==0.0.5 # via -r docs/requirements.in requests==2.32.4 # via # sphinx # sphinxcontrib-spelling roman-numerals-py==3.1.0 # via sphinx rpds-py @ file:.#egg=rpds-py # via -r docs/requirements.in snowballstemmer==3.0.1 # via sphinx soupsieve==2.7 # via beautifulsoup4 sphinx==8.2.3 # 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==2.0.0 # via sphinx sphinxcontrib-devhelp==2.0.0 # via sphinx sphinxcontrib-htmlhelp==2.1.0 # via sphinx sphinxcontrib-jsmath==1.0.1 # via sphinx sphinxcontrib-qthelp==2.0.0 # via sphinx sphinxcontrib-serializinghtml==2.0.0 # via sphinx sphinxcontrib-spelling==8.0.1 # via -r docs/requirements.in sphinxext-opengraph==0.10.0 # via -r docs/requirements.in typing-extensions==4.14.0 # via beautifulsoup4 url-py==0.15.0 # via -r docs/requirements.in urllib3==2.5.0 # via requests rpds_py-0.27.1/docs/spelling-wordlist.txt010064400017510000166000000000231505357214200166150ustar 00000000000000iter len toolchain rpds_py-0.27.1/noxfile.py010064400017510000166000000116251505357214200134720ustar 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.9", "3.10", "pypy3.10", "3.11", "pypy3.11", "3.12", "3.13", "3.13t", "3.14", "3.14t", ] LATEST = "3.13" 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. """ # Really we want --profile=test here (for # https://github.com/crate-py/rpds/pull/87#issuecomment-2291409297) # but it produces strange symbol errors saying: # dynamic module does not define module export function (PyInit_rpds) # so OK, dev it is. session.install( "--config-settings", "build-args=--profile=dev", "--no-cache", "-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", "--parallel-threads=10", *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.27.1/rpds.pyi010064400017510000166000000050121505357214200131400ustar 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.27.1/src/lib.rs010064400017510000166000001240561505357214200133620ustar 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::*, BoundObject, 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)).wrapping_mul(3644798167) } #[derive(Debug)] struct Key { hash: isize, inner: PyObject, } impl<'py> IntoPyObject<'py> for Key { type Target = PyAny; type Output = Bound<'py, Self::Target>; type Error = std::convert::Infallible; fn into_pyobject(self, py: Python<'py>) -> Result { Ok(self.inner.into_bound(py)) } } impl<'a, 'py> IntoPyObject<'py> for &'a Key { type Target = PyAny; type Output = Borrowed<'a, 'py, Self::Target>; type Error = std::convert::Infallible; fn into_pyobject(self, py: Python<'py>) -> Result { Ok(self.inner.bind_borrowed(py)) } } 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<'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.try_iter()? { let (k, v) = 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__<'py>(&self, other: &Self, op: CompareOp, py: Python<'py>) -> PyResult { match op { CompareOp::Eq => (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_pyobject(py) .map_err(Into::into) .map(BoundObject::into_any) .map(BoundObject::unbind), CompareOp::Ne => (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_pyobject(py) .map_err(Into::into) .map(BoundObject::into_any) .map(BoundObject::unbind), _ => 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().wrapping_add(1).wrapping_mul(1927868237); // dispense patterns in the hash value hash_val ^= (hash_val >> 11) ^ (hash_val >> 25); hash_val = hash_val.wrapping_mul(69069).wrapping_add(907133923); Ok(hash_val as isize) } fn __reduce__(slf: PyRef) -> (Bound<'_, PyType>, (Vec<(Key, PyObject)>,)) { ( HashTrieMapPy::type_object(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 { HashTrieMapPy::extract_bound(&value)? .into_pyobject(py) .map(BoundObject::into_any) .map(BoundObject::unbind) } } #[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.try_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(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? != slf.inner.size() { return Ok(false); } for each in other.try_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(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(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(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? >= slf.inner.size() { return Ok(false); } for each in other.try_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(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? > slf.inner.size() { return Ok(false); } for each in other.try_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) -> PyResult { let contents = self.inner.into_iter().map(|(k, _)| { Ok(k.clone_ref(py) .inner .into_pyobject(py)? .call_method0("__repr__") .and_then(|r| r.extract()) .unwrap_or("".to_owned())) }); let contents = contents.collect::, PyErr>>()?; Ok(format!("keys_view({{{}}})", contents.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.try_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.try_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) -> PyResult { let contents = self.inner.into_iter().map(|(_, v)| { Ok(v.into_pyobject(py)? .call_method0("__repr__") .and_then(|r| r.extract()) .unwrap_or("".to_owned())) }); let contents = contents.collect::, PyErr>>()?; Ok(format!("values_view([{}])", contents.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(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) -> PyResult { let contents = self.inner.into_iter().map(|(k, v)| { let tuple = PyTuple::new(py, [k.inner.clone_ref(py), v.clone_ref(py)])?; Ok(format!("{:?}", tuple)) }); let contents = contents.collect::, PyErr>>()?; Ok(format!("items_view([{}])", contents.join(", "))) } fn __lt__(slf: PyRef<'_, Self>, other: &Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import(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(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(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(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(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? >= slf.inner.size() { return Ok(false); } for each in other.try_iter()? { let kv = each?; let k = kv.get_item(0)?; match slf.inner.get(&Key::extract_bound(&k)?) { Some(value) => { let pair = PyTuple::new(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(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? > slf.inner.size() { return Ok(false); } for each in other.try_iter()? { let kv = each?; let k = kv.get_item(0)?; match slf.inner.get(&Key::extract_bound(&k)?) { Some(value) => { let pair = PyTuple::new(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.try_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(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(py, [k.inner.clone_ref(py), v.clone_ref(py)])?; inner.insert_mut(Key::extract_bound(&pair)?); } for each in other.try_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.try_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) -> PyResult { let contents = self.inner.into_iter().map(|k| { Ok(k.clone_ref(py) .into_pyobject(py)? .call_method0("__repr__") .and_then(|r| r.extract()) .unwrap_or("".to_owned())) }); let contents = contents.collect::, PyErr>>()?; Ok(format!("HashTrieSet({{{}}})", contents.join(", "))) } fn __eq__(slf: PyRef<'_, Self>, other: Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? != slf.inner.size() { return Ok(false); } for each in other.try_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().wrapping_add(1).wrapping_mul(1927868237); // dispense patterns in the hash value hash_val ^= (hash_val >> 11) ^ (hash_val >> 25); hash_val = hash_val.wrapping_mul(69069).wrapping_add(907133923); Ok(hash_val as isize) } fn __lt__(slf: PyRef<'_, Self>, other: Bound<'_, PyAny>, py: Python) -> PyResult { let abc = PyModule::import(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(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(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? >= slf.inner.size() { return Ok(false); } for each in other.try_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(py, "collections.abc")?; if !other.is_instance(&abc.getattr("Set")?)? || other.len()? > slf.inner.size() { return Ok(false); } for each in other.try_iter()? { if !slf.inner.contains(&Key::extract_bound(&each?)?) { return Ok(false); } } Ok(true) } fn __reduce__(slf: PyRef) -> (Bound<'_, PyType>, (Vec,)) { ( HashTrieSetPy::type_object(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.try_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(ob.py(), "builtins")?.getattr("reversed")?; let rob: Bound<'_, PyIterator> = reversed.call1((ob,))?.try_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) -> PyResult { let contents = self.inner.into_iter().map(|k| { Ok(k.into_pyobject(py)? .call_method0("__repr__") .and_then(|r| r.extract()) .unwrap_or("".to_owned())) }); let contents = contents.collect::, PyErr>>()?; Ok(format!("List([{}])", contents.join(", "))) } fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyResult { match op { CompareOp::Eq => (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_pyobject(py) .map_err(Into::into) .map(BoundObject::into_any) .map(BoundObject::unbind), CompareOp::Ne => (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_pyobject(py) .map_err(Into::into) .map(BoundObject::into_any) .map(BoundObject::unbind), _ => 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(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.try_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_pyobject(py)?.unbind()); } } } 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) -> PyResult { let contents = self.inner.into_iter().map(|k| { Ok(k.into_pyobject(py)? .call_method0("__repr__") .and_then(|r| r.extract()) .unwrap_or("".to_owned())) }); let contents = contents.collect::, PyErr>>()?; Ok(format!("Queue([{}])", contents.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(gil_used = false)] #[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(py, "collections.abc")?; abc.getattr("Set")? .call_method1("register", (HashTrieSetPy::type_object(py),))?; abc.getattr("MappingView")? .call_method1("register", (KeysView::type_object(py),))?; abc.getattr("MappingView")? .call_method1("register", (ValuesView::type_object(py),))?; abc.getattr("MappingView")? .call_method1("register", (ItemsView::type_object(py),))?; abc.getattr("KeysView")? .call_method1("register", (KeysView::type_object(py),))?; abc.getattr("ValuesView")? .call_method1("register", (ValuesView::type_object(py),))?; abc.getattr("ItemsView")? .call_method1("register", (ItemsView::type_object(py),))?; Ok(()) } rpds_py-0.27.1/tests/__init__.py010064400017510000166000000000001505357214200147100ustar 00000000000000rpds_py-0.27.1/tests/requirements.in010064400017510000166000000000561505357214200156650ustar 00000000000000file:.#egg=rpds-py pytest pytest-run-parallel rpds_py-0.27.1/tests/requirements.txt010064400017510000166000000010071505357214200160730ustar 00000000000000# This file was autogenerated by uv via the following command: # uv pip compile --output-file /Users/julian/Development/rpds.py/tests/requirements.txt tests/requirements.in iniconfig==2.1.0 # via pytest packaging==25.0 # via pytest pluggy==1.6.0 # via pytest pygments==2.19.2 # via pytest pytest==8.4.1 # via # -r tests/requirements.in # pytest-run-parallel pytest-run-parallel==0.4.4 # via -r tests/requirements.in rpds-py @ file:.#egg=rpds-py # via -r tests/requirements.in rpds_py-0.27.1/tests/test_hash_trie_map.py010064400017510000166000000357761505357214200170470ustar 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 sysconfig import pytest from rpds import HashTrieMap # see https://github.com/python/cpython/issues/127065, # remove this when the CPython bug is fixed in a released version if bool(sysconfig.get_config_var("Py_GIL_DISABLED")): def methodcaller(name, /, *args, **kwargs): def caller(obj): return getattr(obj, name)(*args, **kwargs) return caller 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.27.1/tests/test_hash_trie_set.py010064400017510000166000000137161505357214200170530ustar 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.27.1/tests/test_list.py010064400017510000166000000103431505357214200151760ustar 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.27.1/tests/test_queue.py010064400017510000166000000071121505357214200153470ustar 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.27.1/pyproject.toml010064400017510000166000000110471505357214200143660ustar 00000000000000[build-system] requires = ["maturin>=1.9,<2.0"] build-backend = "maturin" [project] name = "rpds-py" description = "Python bindings to Rust's persistent data structures (rpds)" requires-python = ">=3.9" readme = "README.rst" license = "MIT" license-files = ["LICENSE"] keywords = ["data structures", "rust", "persistent"] authors = [ { name = "Julian Berman", email = "Julian+rpds@GrayVines.com" }, ] classifiers = [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "Operating System :: OS Independent", "Programming Language :: Rust", "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.14", "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" Upstream = "https://github.com/orium/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", "A005", "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.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.27.1/PKG-INFO000064400000010142000000000000007663ustar Metadata-Version: 2.4 Name: rpds-py Version: 0.27.1 Classifier: Development Status :: 3 - Alpha Classifier: Intended Audience :: Developers Classifier: Operating System :: OS Independent Classifier: Programming Language :: Rust 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.14 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-Expression: MIT Requires-Python: >=3.9 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 Project-URL: Upstream, https://github.com/orium/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