Install & Compatibility
Where this runs
tested against v2.4.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.013s · 18.4MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.7s · import 0.010s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
SortedList
✓ from sortedcontainers import SortedList
These stubs integrate with the standard `sortedcontainers` imports to provide type checking. You do not import directly from `sortedcontainers-stubs`.
SortedDict
✓ from sortedcontainers import SortedDict
These stubs integrate with the standard `sortedcontainers` imports to provide type checking. You do not import directly from `sortedcontainers-stubs`.
SortedSet
✓ from sortedcontainers import SortedSet
These stubs integrate with the standard `sortedcontainers` imports to provide type checking. You do not import directly from `sortedcontainers-stubs`.
SortedKeyDict
✓ from sortedcontainers import SortedDict
✗ from sortedcontainers.sorteddict import SortedKeyDict
The `SortedKeyDict` and `SortedKeySet` types are stub-only subclasses used to describe specialized return types for `SortedDict` and `SortedSet` constructors when a key function is provided. They do not exist at runtime and should not be imported or instantiated directly.
SortedKeySet
✓ from sortedcontainers import SortedSet
✗ from sortedcontainers.sortedset import SortedKeySet
The `SortedKeyDict` and `SortedKeySet` types are stub-only subclasses used to describe specialized return types for `SortedDict` and `SortedSet` constructors when a key function is provided. They do not exist at runtime and should not be imported or instantiated directly.
Install `sortedcontainers-stubs` and then use `sortedcontainers` as usual with type hints. Type checkers will automatically discover the stubs. This example demonstrates basic usage of `SortedList`, `SortedDict`, and `SortedSet` with explicit type annotations.
from typing import List, Tuple
from sortedcontainers import SortedList, SortedDict, SortedSet
# Example with SortedList
def process_sorted_list(data: List[int]) -> SortedList[int]:
sl = SortedList(data)
sl.add(0)
return sl
my_list: SortedList[int] = process_sorted_list([3, 1, 4, 1, 5])
print(f"SortedList: {my_list}")
# Example with SortedDict
def process_sorted_dict(data: List[Tuple[str, int]]) -> SortedDict[str, int]:
sd = SortedDict(data)
sd['apple'] = 100
return sd
my_dict: SortedDict[str, int] = process_sorted_dict([('banana', 2), ('orange', 1)])
print(f"SortedDict: {my_dict}")
# Example with SortedSet
def process_sorted_set(data: List[int]) -> SortedSet[int]:
ss = SortedSet(data)
ss.add(10)
return ss
my_set: SortedSet[int] = process_sorted_set([3, 1, 4, 1, 5])
print(f"SortedSet: {my_set}")
# To verify type checking, run a type checker like Mypy: `mypy your_script.py`
# The sortedcontainers-stubs package provides the type information for these objects.
Errors
Common errors & fixes
AttributeError: module 'sortedcontainers.sorteddict' has no attribute 'SortedKeyDict'
You are attempting to access `SortedKeyDict` (or `SortedKeySet`) at runtime. These are special stub-only classes used exclusively for type checking.
fixRemove the explicit reference to `SortedKeyDict` or `SortedKeySet`. When using `SortedDict` or `SortedSet` with a `key` argument, the type checker will correctly infer the specialized type; you should instantiate `SortedDict` or `SortedSet` directly.
error: Module 'sortedcontainers' has no attribute 'SortedList' (or 'SortedDict', 'SortedSet')
Your type checker (e.g., Mypy, Pyright) cannot find the type definitions for the `sortedcontainers` library. This usually means `sortedcontainers-stubs` is not installed or not discoverable.
fixEnsure `sortedcontainers-stubs` is installed in your environment: `pip install sortedcontainers-stubs`. Verify your type checker configuration points to the correct environment.
error: Incompatible types in assignment (expression has type "list[int]", variable has type "SortedList[int]")
You are assigning a standard Python collection (e.g., `list`, `dict`, `set`) to a variable explicitly type-hinted with a `sortedcontainers` type, or vice-versa. The stubs enforce the distinct types.
fixEnsure type consistency. If a variable is type-hinted as `SortedList[int]`, assign a `SortedList[int]` instance to it. Convert between types explicitly if necessary (e.g., `my_sorted_list = SortedList(my_regular_list)`).
Upgrade
Version history
2.4.3latest on PyPI · released Apr 23, 2025
Audit
Dependencies
sortedcontainersrequiredThese stubs provide type hints for the `sortedcontainers` library, which must be installed for runtime functionality. The major and minor versions of the stubs should ideally match the library's major and minor versions.