Commit 27bd3f5
authored
fix(base): remove misleading
## Summary
Closes #477.
`create_registry()` currently seeds `type_annotation_map` with
`DataclassProtocol: JsonB`. That entry does not provide structural
dataclass support:
- SQLAlchemy resolves `type_annotation_map` through exact and `__mro__`
dictionary lookups; it does not call `isinstance()` or otherwise perform
runtime Protocol validation.
- A concrete dataclass used as `Mapped[SomeDataclass]` does not inherit
`DataclassProtocol`, so it does not match that entry and raises
`MappedAnnotationError`.
- An exact `Mapped[DataclassProtocol]` annotation can select `JsonB`,
but it does not validate assigned values against the Protocol or
preserve the concrete dataclass type for reconstruction.
The default entry is therefore misleading: it appears to support
arbitrary concrete dataclasses, but concrete dataclass annotations must
be registered explicitly.
## Changes
- Remove the default `DataclassProtocol: JsonB` entry and its now-unused
import from `advanced_alchemy/base.py`.
- Add regression coverage for the default registry map.
- Document the supported path: register each concrete dataclass with
`custom_annotation_map={MyDataclass: JsonB}` so SQLAlchemy can resolve
the exact annotation.
## Compatibility
This changes the narrow case where a model is annotated literally as
`Mapped[DataclassProtocol]`; that exact annotation previously selected
`JsonB`. It did not validate Protocol conformance or provide concrete
dataclass reconstruction.
Concrete annotations such as `Mapped[SomeDataclass]` did not resolve
through this entry before this change and continue to require an
explicit concrete mapping.
## Supported replacement
```python
from dataclasses import dataclass
from advanced_alchemy.base import create_registry
from advanced_alchemy.types import JsonB
@DataClass
class ExtraData:
before: int
registry = create_registry(custom_annotation_map={ExtraData: JsonB})
```DataclassProtocol annotation mapping (#771)1 parent c5ea0c9 commit 27bd3f5
2 files changed
Lines changed: 37 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | 37 | | |
39 | 38 | | |
40 | 39 | | |
| |||
405 | 404 | | |
406 | 405 | | |
407 | 406 | | |
408 | | - | |
409 | 407 | | |
410 | 408 | | |
411 | 409 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
282 | 282 | | |
283 | 283 | | |
284 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
0 commit comments