represents a scalar value-holding InstrumentedAttribute.
| 1208 | |
| 1209 | |
| 1210 | class _ScalarAttributeImpl(_AttributeImpl): |
| 1211 | """represents a scalar value-holding InstrumentedAttribute.""" |
| 1212 | |
| 1213 | default_accepts_scalar_loader = True |
| 1214 | uses_objects = False |
| 1215 | supports_population = True |
| 1216 | collection = False |
| 1217 | dynamic = False |
| 1218 | |
| 1219 | __slots__ = ( |
| 1220 | "_default_scalar_value", |
| 1221 | "_replace_token", |
| 1222 | "_append_token", |
| 1223 | "_remove_token", |
| 1224 | ) |
| 1225 | |
| 1226 | def __init__(self, *arg, default_scalar_value=None, **kw): |
| 1227 | super().__init__(*arg, **kw) |
| 1228 | self._default_scalar_value = default_scalar_value |
| 1229 | self._replace_token = self._append_token = AttributeEventToken( |
| 1230 | self, OP_REPLACE |
| 1231 | ) |
| 1232 | self._remove_token = AttributeEventToken(self, OP_REMOVE) |
| 1233 | |
| 1234 | def _default_value( |
| 1235 | self, state: InstanceState[Any], dict_: _InstanceDict |
| 1236 | ) -> Any: |
| 1237 | """Produce an empty value for an uninitialized scalar attribute.""" |
| 1238 | |
| 1239 | assert self.key not in dict_, ( |
| 1240 | "_default_value should only be invoked for an " |
| 1241 | "uninitialized or expired attribute" |
| 1242 | ) |
| 1243 | value = self._default_scalar_value |
| 1244 | for fn in self.dispatch.init_scalar: |
| 1245 | ret = fn(state, value, dict_) |
| 1246 | if ret is not ATTR_EMPTY: |
| 1247 | value = ret |
| 1248 | |
| 1249 | return value |
| 1250 | |
| 1251 | def delete(self, state: InstanceState[Any], dict_: _InstanceDict) -> None: |
| 1252 | if self.dispatch._active_history: |
| 1253 | old = self.get(state, dict_, PASSIVE_RETURN_NO_VALUE) |
| 1254 | else: |
| 1255 | old = dict_.get(self.key, NO_VALUE) |
| 1256 | |
| 1257 | if self.dispatch.remove: |
| 1258 | self.fire_remove_event(state, dict_, old, self._remove_token) |
| 1259 | state._modified_event(dict_, self, old) |
| 1260 | |
| 1261 | existing = dict_.pop(self.key, NO_VALUE) |
| 1262 | if ( |
| 1263 | existing is NO_VALUE |
| 1264 | and old is NO_VALUE |
| 1265 | and not state.expired |
| 1266 | and self.key not in state.expired_attributes |
| 1267 | ): |
no outgoing calls
no test coverage detected