|
| 1 | +# Migration Guide: v1.x to v2.0 |
| 2 | + |
| 3 | +This guide helps you upgrade your code from nestedutils v1.x to v2.0. Version 2.0 introduces breaking changes that make the library safer and more predictable. |
| 4 | + |
| 5 | +## Quick Summary of Breaking Changes |
| 6 | + |
| 7 | +| Change | v1.x Behavior | v2.0 Behavior | |
| 8 | +|--------|---------------|---------------| |
| 9 | +| `get_at` missing path | Returns `None` silently | Raises `PathError` | |
| 10 | +| `get_at` default parameter | Could be positional | Must be keyword-only (`default=...`) | |
| 11 | +| `set_at` auto-create | `fill_strategy` parameter | `create=False` parameter | |
| 12 | +| `set_at` create parameter | N/A (didn't exist) | Must be keyword-only (`create=...`) | |
| 13 | +| Sparse lists | Allowed with `None` fill | Not allowed (strict sequential) | |
| 14 | +| `FillStrategy` enum | Available | Removed | |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 1. `get_at` Now Raises by Default |
| 19 | + |
| 20 | +### The Change |
| 21 | + |
| 22 | +In v1.x, `get_at` silently returned `None` when a path didn't exist. In v2.0, it raises `PathError` by default, making missing data explicit rather than hidden. |
| 23 | + |
| 24 | +### v1.x Code |
| 25 | + |
| 26 | +```python |
| 27 | +from nestedutils import get_at |
| 28 | + |
| 29 | +data = {"user": {"name": "Alice"}} |
| 30 | + |
| 31 | +# v1.x: Returns None silently - bugs can hide here! |
| 32 | +email = get_at(data, "user.email") # None |
| 33 | +age = get_at(data, "user.profile.age") # None |
| 34 | + |
| 35 | +# v1.x: default could be passed positionally |
| 36 | +value = get_at(data, "missing.path", None) # None (positional argument) |
| 37 | +``` |
| 38 | + |
| 39 | +### v2.0 Migration |
| 40 | + |
| 41 | +**Option A: Use `default` parameter for optional values** |
| 42 | + |
| 43 | +```python |
| 44 | +from nestedutils import get_at |
| 45 | + |
| 46 | +data = {"user": {"name": "Alice"}} |
| 47 | + |
| 48 | +# Explicit default - clear intent |
| 49 | +email = get_at(data, "user.email", default=None) # None |
| 50 | +email = get_at(data, "user.email", default="unknown@example.com") # fallback value |
| 51 | +``` |
| 52 | + |
| 53 | +**Option B: Use `exists_at` to check first** |
| 54 | + |
| 55 | +```python |
| 56 | +from nestedutils import get_at, exists_at |
| 57 | + |
| 58 | +if exists_at(data, "user.email"): |
| 59 | + email = get_at(data, "user.email") |
| 60 | +else: |
| 61 | + email = "default@example.com" |
| 62 | +``` |
| 63 | + |
| 64 | +**Option C: Use try/except for error handling** |
| 65 | + |
| 66 | +```python |
| 67 | +from nestedutils import get_at, PathError |
| 68 | + |
| 69 | +try: |
| 70 | + email = get_at(data, "user.email") |
| 71 | +except PathError: |
| 72 | + email = "default@example.com" |
| 73 | +``` |
| 74 | + |
| 75 | +### Why This Change? |
| 76 | + |
| 77 | +Silent `None` returns masked bugs. Consider: |
| 78 | + |
| 79 | +```python |
| 80 | +# v1.x - Bug hidden: typo in path returns None, not an error |
| 81 | +user_name = get_at(data, "usr.name") # None (typo: "usr" vs "user") |
| 82 | + |
| 83 | +# v2.0 - Bug exposed immediately |
| 84 | +user_name = get_at(data, "usr.name") # Raises PathError! |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## 2. `set_at` Parameter Changes |
| 90 | + |
| 91 | +### The Change |
| 92 | + |
| 93 | +The `fill_strategy` parameter has been replaced with a simpler `create` boolean parameter. Sparse list creation is no longer supported. **Important**: Both `get_at`'s `default` and `set_at`'s `create` are now keyword-only parameters (must use `default=...` and `create=...`, not positional arguments). |
| 94 | + |
| 95 | +### v1.x Code |
| 96 | + |
| 97 | +```python |
| 98 | +from nestedutils import set_at, FillStrategy |
| 99 | + |
| 100 | +data = {} |
| 101 | + |
| 102 | +# v1.x: Various fill strategies |
| 103 | +set_at(data, "user.name", "Alice", fill_strategy=FillStrategy.AUTO) |
| 104 | +set_at(data, "items.0", "first", fill_strategy=FillStrategy.AUTO) |
| 105 | +set_at(data, "items.5", "sixth", fill_strategy=FillStrategy.NONE) # Sparse list! |
| 106 | + |
| 107 | +# v1.x: fill_strategy could be passed as string positionally |
| 108 | +set_at(data, "user.name", "Alice", "auto") # Positional argument |
| 109 | +``` |
| 110 | + |
| 111 | +### v2.0 Migration |
| 112 | + |
| 113 | +```python |
| 114 | +from nestedutils import set_at |
| 115 | + |
| 116 | +data = {} |
| 117 | + |
| 118 | +# v2.0: Simple create=True for auto-creation |
| 119 | +set_at(data, "user.name", "Alice", create=True) |
| 120 | +set_at(data, "items.0", "first", create=True) |
| 121 | + |
| 122 | +# Sparse lists are NO LONGER ALLOWED |
| 123 | +# set_at(data, "items.5", "sixth", create=True) # Raises PathError! |
| 124 | + |
| 125 | +# Must build sequentially |
| 126 | +set_at(data, "items.1", "second", create=True) # OK - appends |
| 127 | +``` |
| 128 | + |
| 129 | +### FillStrategy Migration Table |
| 130 | + |
| 131 | +| v1.x `fill_strategy` | v2.0 Equivalent | |
| 132 | +|----------------------|-----------------| |
| 133 | +| `FillStrategy.AUTO` | `create=True` | |
| 134 | +| `FillStrategy.NONE` | Not supported (no sparse lists) | |
| 135 | +| `FillStrategy.DICT` | `create=True` (inferred from key type) | |
| 136 | +| `FillStrategy.LIST` | `create=True` (inferred from key type) | |
| 137 | +| Not specified | `create=False` (default, raises on missing) | |
| 138 | + |
| 139 | +### Why This Change? |
| 140 | + |
| 141 | +- Sparse lists (`[None, None, None, "value"]`) caused confusion and bugs |
| 142 | +- The `FillStrategy` enum added complexity without proportional benefit |
| 143 | +- `create=True/False` is more intuitive and covers most use cases |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## 3. No More Sparse Lists |
| 148 | + |
| 149 | +### The Change |
| 150 | + |
| 151 | +v1.x allowed creating lists with gaps filled by `None`. v2.0 enforces sequential list building only. |
| 152 | + |
| 153 | +### v1.x Code |
| 154 | + |
| 155 | +```python |
| 156 | +from nestedutils import set_at |
| 157 | + |
| 158 | +data = {} |
| 159 | +set_at(data, "items.5", "value", fill_strategy=FillStrategy.NONE) |
| 160 | +# Result: {"items": [None, None, None, None, None, "value"]} |
| 161 | +``` |
| 162 | + |
| 163 | +### v2.0 Migration |
| 164 | + |
| 165 | +```python |
| 166 | +from nestedutils import set_at |
| 167 | + |
| 168 | +data = {} |
| 169 | + |
| 170 | +# Build lists sequentially |
| 171 | +set_at(data, "items.0", "first", create=True) |
| 172 | +set_at(data, "items.1", "second", create=True) |
| 173 | +set_at(data, "items.2", "third", create=True) |
| 174 | +# Result: {"items": ["first", "second", "third"]} |
| 175 | + |
| 176 | +# Or use plain Python for sparse/pre-sized lists |
| 177 | +data = {"items": [None] * 6} |
| 178 | +set_at(data, "items.5", "value") # OK - index exists |
| 179 | +``` |
| 180 | + |
| 181 | +### Why This Change? |
| 182 | + |
| 183 | +Sparse lists often indicate logic errors. If you truly need sparse data, a dict with integer keys is more appropriate: |
| 184 | + |
| 185 | +```python |
| 186 | +# Instead of sparse list |
| 187 | +data = {"items": {5: "value", 10: "another"}} |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## 4. FillStrategy Enum Removed |
| 193 | + |
| 194 | +### The Change |
| 195 | + |
| 196 | +The `FillStrategy` enum has been removed from the public API. |
| 197 | + |
| 198 | +### v1.x Code |
| 199 | + |
| 200 | +```python |
| 201 | +from nestedutils import FillStrategy |
| 202 | + |
| 203 | +strategy = FillStrategy.AUTO |
| 204 | +``` |
| 205 | + |
| 206 | +### v2.0 Migration |
| 207 | + |
| 208 | +Remove all `FillStrategy` imports and usages. Use `create=True/False` instead. |
| 209 | + |
| 210 | +```python |
| 211 | +# v2.0: No FillStrategy import needed |
| 212 | +from nestedutils import set_at |
| 213 | + |
| 214 | +set_at(data, "path", value, create=True) |
| 215 | +``` |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +## 5. New Introspection Functions |
| 220 | + |
| 221 | +v2.0 adds new introspection functions that don't exist in v1.x: |
| 222 | + |
| 223 | +```python |
| 224 | +from nestedutils import get_depth, count_leaves, get_all_paths |
| 225 | + |
| 226 | +data = {"a": {"b": 1, "c": 2}, "d": [3, 4]} |
| 227 | + |
| 228 | +get_depth(data) # 2 |
| 229 | +count_leaves(data) # 4 |
| 230 | +get_all_paths(data) # [["a", "b"], ["a", "c"], ["d", 0], ["d", 1]] |
| 231 | +``` |
| 232 | + |
| 233 | +--- |
| 234 | + |
| 235 | +## Migration Checklist |
| 236 | + |
| 237 | +- [ ] **Search for `get_at` calls without `default`** - Add `default=None` if silent failure is intended |
| 238 | +- [ ] **Update positional `default` arguments** - Change `get_at(data, "path", None)` to `get_at(data, "path", default=None)` |
| 239 | +- [ ] **Remove `FillStrategy` imports** - Replace with `create=True/False` |
| 240 | +- [ ] **Remove `fill_strategy` parameters** - Replace with `create=True` |
| 241 | +- [ ] **Update positional `fill_strategy` arguments** - Change `set_at(data, "path", value, "auto")` to `set_at(data, "path", value, create=True)` |
| 242 | +- [ ] **Check for sparse list creation** - Refactor to sequential building or use dicts |
| 243 | +- [ ] **Run your test suite** - v2.0's stricter behavior will expose hidden bugs |
0 commit comments