Initialize a collection attribute and return the collection adapter. Discards any existing collection which may be there.
(
state: InstanceState[Any], dict_: _InstanceDict, key: str
)
| 2718 | |
| 2719 | |
| 2720 | def init_state_collection( |
| 2721 | state: InstanceState[Any], dict_: _InstanceDict, key: str |
| 2722 | ) -> CollectionAdapter: |
| 2723 | """Initialize a collection attribute and return the collection adapter. |
| 2724 | |
| 2725 | Discards any existing collection which may be there. |
| 2726 | |
| 2727 | """ |
| 2728 | attr = state.manager[key].impl |
| 2729 | |
| 2730 | if TYPE_CHECKING: |
| 2731 | assert isinstance(attr, _HasCollectionAdapter) |
| 2732 | |
| 2733 | old = dict_.pop(key, None) # discard old collection |
| 2734 | if old is not None: |
| 2735 | old_collection = old._sa_adapter |
| 2736 | attr._dispose_previous_collection(state, old, old_collection, False) |
| 2737 | |
| 2738 | user_data = attr._default_value(state, dict_) |
| 2739 | adapter: CollectionAdapter = attr.get_collection( |
| 2740 | state, dict_, user_data, passive=PassiveFlag.PASSIVE_NO_FETCH |
| 2741 | ) |
| 2742 | adapter._reset_empty() |
| 2743 | |
| 2744 | return adapter |
| 2745 | |
| 2746 | |
| 2747 | def set_committed_value(instance: object, key: str, value: Any) -> None: |
no test coverage detected