|
4 | 4 |
|
5 | 5 | <summary>Documentation of older versions is available on GitHub.</summary> |
6 | 6 |
|
| 7 | +▸ [0.12.0](https://github.com/tfpf/pysorteddict/blob/v0.12.0/docs/documentation.md) |
7 | 8 | ▸ [0.11.0](https://github.com/tfpf/pysorteddict/blob/v0.11.0/docs/documentation.md) |
8 | 9 | ▸ [0.10.0](https://github.com/tfpf/pysorteddict/blob/v0.10.0/docs/documentation.md) |
9 | 10 | ▸ [0.9.0](https://github.com/tfpf/pysorteddict/blob/v0.9.0/docs/documentation.md) |
@@ -39,41 +40,45 @@ or implicitly using the wildcard (though this is not recommended). |
39 | 40 | from pysorteddict import * |
40 | 41 | ``` |
41 | 42 |
|
42 | | -The following key types are supported. |
| 43 | +The following key types are always supported. |
43 | 44 |
|
44 | 45 | * `bytes` |
45 | 46 | * `float` |
46 | 47 | * `int` |
47 | 48 | * `str` |
48 | | -* `decimal.Decimal` |
49 | 49 |
|
50 | | -### Constructor |
| 50 | +The following key types are supported if they are importable (which they should always be—failure to import them may be |
| 51 | +a sign of a corrupt or damaged Python installation). |
51 | 52 |
|
52 | | -#### `SortedDict(*args, **kwargs) -> SortedDict` |
53 | | - |
54 | | -Create an empty sorted dictionary. `args` and `kwargs` are ignored. |
| 53 | +* `decimal.Decimal` |
55 | 54 |
|
56 | 55 | <details class="warning"> |
57 | 56 |
|
58 | | -<summary>This method may raise exceptions.</summary> |
59 | | - |
60 | | -If any of the supported key types which are not built-in (only `decimal.Decimal` as of this version) cannot be imported |
61 | | -(which might be a symptom of a corrupt or damaged Python installation), raises `ImportError`. |
| 57 | +<summary>Shadowing these standard library types with custom types may lead to undefined behaviour.</summary> |
62 | 58 |
|
63 | 59 | ```python |
| 60 | +from pathlib import Path |
| 61 | +with Path(__file__).with_name("decimal.py").open("w") as writer: |
| 62 | + print("import math", file=writer) |
| 63 | + print('Decimal = type("Decimal", (float,), {"is_nan": lambda self: math.isnan(self)})', file=writer) |
64 | 64 | from pysorteddict import * |
| 65 | +from decimal import Decimal |
65 | 66 | d = SortedDict() |
| 67 | +d[Decimal(0)] = None |
66 | 68 | ``` |
67 | 69 |
|
68 | | -```text |
69 | | -Traceback (most recent call last): |
70 | | - File "…", line 2, in <module> |
71 | | - d = SortedDict() |
72 | | -ImportError: failed to import the `decimal.Decimal` type |
73 | | -``` |
| 70 | +Here, the imported `Decimal` is actually `float` with an `is_nan` method defined. This will work. However, comparisons |
| 71 | +between two such `Decimal`s can be made to error out (by overriding `Decimal.__eq__` and friends). Errors in the |
| 72 | +comparator of a C++ `std::map` (the underlying type of a sorted dictionary) invoke undefined behaviour. |
74 | 73 |
|
75 | 74 | </details> |
76 | 75 |
|
| 76 | +### Constructor |
| 77 | + |
| 78 | +#### `SortedDict(*args, **kwargs) -> SortedDict` |
| 79 | + |
| 80 | +Create an empty sorted dictionary. `args` and `kwargs` are ignored. |
| 81 | + |
77 | 82 | ### Properties |
78 | 83 |
|
79 | 84 | #### `d.key_type: type | None` |
|
0 commit comments