add an attribute to an existing declarative class. This runs through the logic to determine MapperProperty, adds it to the Mapper, adds a column to the mapped Table, etc.
(
cls: Type[Any], key: str, value: MapperProperty[Any]
)
| 2202 | |
| 2203 | |
| 2204 | def _add_attribute( |
| 2205 | cls: Type[Any], key: str, value: MapperProperty[Any] |
| 2206 | ) -> None: |
| 2207 | """add an attribute to an existing declarative class. |
| 2208 | |
| 2209 | This runs through the logic to determine MapperProperty, |
| 2210 | adds it to the Mapper, adds a column to the mapped Table, etc. |
| 2211 | |
| 2212 | """ |
| 2213 | |
| 2214 | if "__mapper__" in cls.__dict__: |
| 2215 | mapped_cls = cast("MappedClassProtocol[Any]", cls) |
| 2216 | |
| 2217 | def _table_or_raise(mc: MappedClassProtocol[Any]) -> Table: |
| 2218 | if isinstance(mc.__table__, Table): |
| 2219 | return mc.__table__ |
| 2220 | raise exc.InvalidRequestError( |
| 2221 | f"Cannot add a new attribute to mapped class {mc.__name__!r} " |
| 2222 | "because it's not mapped against a table." |
| 2223 | ) |
| 2224 | |
| 2225 | if isinstance(value, Column): |
| 2226 | _undefer_column_name(key, value) |
| 2227 | _table_or_raise(mapped_cls).append_column( |
| 2228 | value, replace_existing=True |
| 2229 | ) |
| 2230 | mapped_cls.__mapper__.add_property(key, value) |
| 2231 | elif isinstance(value, _MapsColumns): |
| 2232 | mp = value.mapper_property_to_assign |
| 2233 | for col, _ in value.columns_to_assign: |
| 2234 | _undefer_column_name(key, col) |
| 2235 | _table_or_raise(mapped_cls).append_column( |
| 2236 | col, replace_existing=True |
| 2237 | ) |
| 2238 | if not mp: |
| 2239 | mapped_cls.__mapper__.add_property(key, col) |
| 2240 | if mp: |
| 2241 | mapped_cls.__mapper__.add_property(key, mp) |
| 2242 | elif isinstance(value, MapperProperty): |
| 2243 | mapped_cls.__mapper__.add_property(key, value) |
| 2244 | elif isinstance(value, QueryableAttribute) and value.key != key: |
| 2245 | # detect a QueryableAttribute that's already mapped being |
| 2246 | # assigned elsewhere in userland, turn into a synonym() |
| 2247 | value = SynonymProperty(value.key) |
| 2248 | mapped_cls.__mapper__.add_property(key, value) |
| 2249 | else: |
| 2250 | type.__setattr__(cls, key, value) |
| 2251 | mapped_cls.__mapper__._expire_memoizations() |
| 2252 | else: |
| 2253 | type.__setattr__(cls, key, value) |
| 2254 | |
| 2255 | |
| 2256 | def _del_attribute(cls: Type[Any], key: str) -> None: |
no test coverage detected