diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2023-05-09 21:54:03 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2023-05-09 21:54:03 +0000 |
| commit | f71c73696ac7a664391513ae4c21a56d18a3c3bf (patch) | |
| tree | 8e5ac68f3826653009b0536a123afa812d46b49e /lib/sqlalchemy | |
| parent | cef8adac8ae48c5c6c36138ed0b59067c1ef78ed (diff) | |
| parent | 8ee129d988c2499766b1f09c5e21383b88dcc204 (diff) | |
| download | sqlalchemy-f71c73696ac7a664391513ae4c21a56d18a3c3bf.tar.gz | |
Merge "guard against duplicate mutable event listeners" into main
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/ext/mutable.py | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/sqlalchemy/ext/mutable.py b/lib/sqlalchemy/ext/mutable.py index 7d23f9fda..0f82518aa 100644 --- a/lib/sqlalchemy/ext/mutable.py +++ b/lib/sqlalchemy/ext/mutable.py @@ -693,14 +693,28 @@ class Mutable(MutableBase): ) -> None: if mapper.non_primary: return + _APPLIED_KEY = "_ext_mutable_listener_applied" + for prop in mapper.column_attrs: if ( - schema_event_check - and hasattr(prop.expression, "info") - and prop.expression.info.get("_ext_mutable_orig_type") # type: ignore # noqa: E501 # TODO: https://github.com/python/mypy/issues/1424#issuecomment-1272354487 - is sqltype - ) or (prop.columns[0].type is sqltype): - cls.associate_with_attribute(getattr(class_, prop.key)) + # all Mutable types refer to a Column that's mapped, + # since this is the only kind of Core target the ORM can + # "mutate" + isinstance(prop.expression, Column) + and ( + ( + schema_event_check + and prop.expression.info.get( + "_ext_mutable_orig_type" + ) + is sqltype + ) + or prop.expression.type is sqltype + ) + ): + if not prop.expression.info.get(_APPLIED_KEY, False): + prop.expression.info[_APPLIED_KEY] = True + cls.associate_with_attribute(getattr(class_, prop.key)) event.listen(Mapper, "mapper_configured", listen_for_type) @@ -724,7 +738,6 @@ class MutableComposite(MutableBase): """Subclasses should call this method whenever change events occur.""" for parent, key in self._parents.items(): - prop = parent.mapper.get_property(key) for value, attr_name in zip( prop._composite_values_from_instance(self), @@ -781,7 +794,6 @@ class MutableDict(Mutable, Dict[_KT, _VT]): self.changed() if TYPE_CHECKING: - # from https://github.com/python/mypy/issues/14858 @overload |
