{"id":18585418,"url":"https://github.com/drivendataorg/sortedcontainers-pydantic","last_synced_at":"2025-08-03T09:02:54.944Z","repository":{"id":226077769,"uuid":"767676681","full_name":"drivendataorg/sortedcontainers-pydantic","owner":"drivendataorg","description":"Adds Pydantic support to sortedcontainers.","archived":false,"fork":false,"pushed_at":"2025-04-19T00:38:28.000Z","size":111,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-06-20T17:50:22.406Z","etag":null,"topics":["pydantic","python","sorted-lists","sorted-map","sorted-sets","sortedcontainers"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drivendataorg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-03-05T17:51:49.000Z","updated_at":"2025-06-16T18:48:02.000Z","dependencies_parsed_at":"2025-04-11T00:53:30.898Z","dependency_job_id":"be6f36f0-b36c-4279-8e93-3c4efec3dc69","html_url":"https://github.com/drivendataorg/sortedcontainers-pydantic","commit_stats":null,"previous_names":["jayqi/sortedcontainers-pydantic","drivendataorg/sortedcontainers-pydantic"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/drivendataorg/sortedcontainers-pydantic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Fsortedcontainers-pydantic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Fsortedcontainers-pydantic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Fsortedcontainers-pydantic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Fsortedcontainers-pydantic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drivendataorg","download_url":"https://codeload.github.com/drivendataorg/sortedcontainers-pydantic/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Fsortedcontainers-pydantic/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266959169,"owners_count":24012462,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pydantic","python","sorted-lists","sorted-map","sorted-sets","sortedcontainers"],"created_at":"2024-11-07T00:33:56.625Z","updated_at":"2025-08-03T09:02:54.933Z","avatar_url":"https://github.com/drivendataorg.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sortedcontainers-pydantic\n\n[![PyPI](https://img.shields.io/pypi/v/sortedcontainers-pydantic.svg)](https://pypi.org/project/sortedcontainers-pydantic/)\n[![conda-forge feedstock](https://img.shields.io/conda/vn/conda-forge/sortedcontainers-pydantic.svg)](https://github.com/conda-forge/sortedcontainers-pydantic-feedstock)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/sortedcontainers-pydantic)](https://pypi.org/project/sortedcontainers-pydantic/)\n[![tests](https://github.com/drivendataorg/sortedcontainers-pydantic/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/drivendataorg/sortedcontainers-pydantic/actions/workflows/tests.yml?query=branch%3Amain)\n[![codecov](https://codecov.io/gh/drivendataorg/sortedcontainers-pydantic/branch/main/graph/badge.svg)](https://codecov.io/gh/drivendataorg/sortedcontainers-pydantic)\n\nThis package extends [sortedcontainers](https://github.com/grantjenks/python-sortedcontainers/), a fast pure-Python library for sorted mutable collections, to work with [Pydantic](https://docs.pydantic.dev/latest/)'s models, validation, and serialization.\n\nThe easiest way to get started is to simply import `SortedDict`, `SortedList`, or `SortedSet` from `sortedcontainers_pydantic` instead of from `sortedcontainers`.\n\n```python\nfrom pydantic import BaseModel, TypeAdapter\nfrom sortedcontainers_pydantic import SortedList\n\nclass MyModel(BaseModel):\n    sorted_list: SortedList[int]\n\nMyModel(sorted_list=[3.0, 1.0, 2.0])\n#\u003e MyModel(sorted_list=SortedList([1, 2, 3]))\n\nMyModel.model_validate_json('{\"sorted_list\": [3, 1, 2]}')\n#\u003e MyModel(sorted_list=SortedList([1, 2, 3]))\n\nMyModel(sorted_list=[3, 1, 2]).model_dump_json()\n#\u003e '{\"sorted_list\":[1,2,3]}'\n\nTypeAdapter(SortedList).validate_python([3, 1, 2])\n#\u003e SortedList([1, 2, 3])\n\nTypeAdapter(SortedList).validate_json(\"[3, 1, 2]\")\n#\u003e SortedList([1, 2, 3])\n```\n\n\u003csup\u003eReproducible example created by [reprexlite](https://github.com/jayqi/reprexlite) v1.0.0\u003c/sup\u003e\n\nFor additional alternative ways to declare types from this library, see the [\"Usage approaches\"](#usage-approaches) section below.\n\nThis library also supports key functions to customize sorting behavior. See the [\"Specifying a key function with `Key`\"](#specifying-a-key-function-with-key) section for further details.\n\n## Installation\n\nsortedcontainers-pydantic is available on [PyPI](https://pypi.org/project/sortedcontainers-pydantic/). You can install it with\n\n```bash\npip install sortedcontainers-pydantic\n```\n\nIt is also available on [conda-forge](https://github.com/conda-forge/sortedcontainers-pydantic-feedstock). You can install it with\n\n```bash\nconda install sortedcontainers-pydantic --channel conda-forge\n```\n\n## Usage approaches\n\nThere are three different ways you can use `sortedcontainers-pydantic`.\n\n### 1. Import from sortedcontainers_pydantic\n\nThe library has subclasses of sortedcontainers's `SortedDict`, `SortedList`, and `SortedSet` with [Pydantic's special methods](https://docs.pydantic.dev/latest/concepts/types/#customizing-validation-with-__get_pydantic_core_schema__) that enable validation and serialization. To use them, simply import classes of the same name from `sortedcontainers_pydantic`.\n\n```python\nfrom pydantic import BaseModel\nfrom sortedcontainers_pydantic import SortedList\n\nclass MyModel(BaseModel):\n    sorted_list: SortedList[int]\n\nMyModel(sorted_list=[3.0, 1.0, 2.0])\n#\u003e MyModel(sorted_list=SortedList([1, 2, 3]))\n\nMyModel.model_validate_json('{\"sorted_list\": [3, 1, 2]}')\n#\u003e MyModel(sorted_list=SortedList([1, 2, 3]))\n```\n\n### 2. Use the annotation pattern\n\n_New in sortedcontainers-pydantic v2.0.0_\n\nThe library has special annotation objects `SortedDictPydanticAnnotation`, `SortedListPydanticAnnotation`, and `SortedSetPydanticAnnotation` that can be attached to sortedcontainers's `SortedDict`, `SortedList`, and `SortedSet`, respectively, using `typing.Annotated`. This implements the [annotated pattern](https://docs.pydantic.dev/latest/concepts/types/#handling-third-party-types) supported by Pydantic.\n\n```python\nfrom typing import Annotated\n\nfrom pydantic import BaseModel\nfrom sortedcontainers import SortedList\nfrom sortedcontainers_pydantic import SortedListPydanticAnnotation\n\nclass MyModel(BaseModel):\n    sorted_list: Annotated[SortedList[int], SortedListPydanticAnnotation]\n\nMyModel(sorted_list=[3.0, 1.0, 2.0])\n#\u003e MyModel(sorted_list=SortedList([1, 2, 3]))\n```\n\nUnlike approach 1, the type being used is the original class from sortedcontainers and not a subclass.\n\n### 3. Use the wrapper type aliases\n\n_New in sortedcontainers-pydantic v2.0.0_\n\nYou can also use the wrapper types `AnnotatedSortedDict`, `AnnotatedSortedList`, or `AnnotatedSortedSet`. These are simply type aliases implementing approach 2.\n\n```python\nfrom pydantic import BaseModel\nfrom sortedcontainers_pydantic import AnnotatedSortedList\n\nAnnotatedSortedList\n#\u003e typing.Annotated[sortedcontainers.sortedlist.SortedList[~_T], \u003cclass 'sortedcontainers_pydantic.SortedListPydanticAnnotation'\u003e]\n\nclass MyModel(BaseModel):\n    sorted_list: AnnotatedSortedList[int]\n\nMyModel(sorted_list=[3.0, 1.0, 2.0])\n#\u003e MyModel(sorted_list=SortedList([1, 2, 3]))\n```\n\n## Specifying a key function with `Key`\n\n_New in sortedcontainers-pydantic v2.0.0_\n\nYou can specify a key function to control sorting. The key should be a callable that takes a single argument. It will be run on every element to generate a key for making comparisons. To specify a key, instantiate the `Key` special annotation object wrapping it, and attach it with `typing.Annotated`. This works with any of the three approaches.\n\n### Example using `Key` with approach 1\n\n```python\nfrom typing import Annotated\n\nfrom pydantic import BaseModel\nfrom sortedcontainers_pydantic import Key, SortedList\n\nclass MyModel(BaseModel):\n    sorted_list: Annotated[SortedList[int], Key(lambda x: -x)]\n\nMyModel(sorted_list=[3.0, 1.0, 2.0])\n#\u003e MyModel(sorted_list=SortedKeyList([3, 2, 1], key=\u003cfunction MyModel.\u003clambda\u003e at 0x10ae058a0\u003e))\n```\n\n### Example using `Key` with approach 2\n\n```python\nfrom typing import Annotated\n\nfrom pydantic import BaseModel\nfrom sortedcontainers import SortedList\nfrom sortedcontainers_pydantic import Key, SortedListPydanticAnnotation\n\nclass MyModel(BaseModel):\n    sorted_list: Annotated[SortedList[int], SortedListPydanticAnnotation, Key(lambda x: -x)]\n\nMyModel(sorted_list=[3.0, 1.0, 2.0])\n#\u003e MyModel(sorted_list=SortedKeyList([3, 2, 1], key=\u003cfunction MyModel.\u003clambda\u003e at 0x10aa4a520\u003e))\n```\n\n### Example using `Key` with approach 3\n\n```python\nfrom typing import Annotated\n\nfrom pydantic import BaseModel\nfrom sortedcontainers_pydantic import AnnotatedSortedList, Key\n\nclass MyModel(BaseModel):\n    sorted_list: Annotated[AnnotatedSortedList[int], Key(lambda x: -x)]\n\nMyModel(sorted_list=[3.0, 1.0, 2.0])\n#\u003e MyModel(sorted_list=SortedKeyList([3, 2, 1], key=\u003cfunction MyModel.\u003clambda\u003e at 0x10ca65080\u003e))\n```\n\n---\n\n\u003csup\u003eReproducible examples created by [reprexlite](https://github.com/jayqi/reprexlite) v1.0.0\u003c/sup\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrivendataorg%2Fsortedcontainers-pydantic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrivendataorg%2Fsortedcontainers-pydantic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrivendataorg%2Fsortedcontainers-pydantic/lists"}