From e573752a986dec84216d948a1497b7d789d039ea Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 25 May 2019 18:04:58 -0400 Subject: Hold implicitly created collections in a pending area Accessing a collection-oriented attribute on a newly created object no longer mutates ``__dict__``, but still returns an empty collection as has always been the case. This allows collection-oriented attributes to work consistently in comparison to scalar attributes which return ``None``, but also don't mutate ``__dict__``. In order to accommodate for the collection being mutated, the same empty collection is returned each time once initially created, and when it is mutated (e.g. an item appended, added, etc.) it is then moved into ``__dict__``. This removes the last of mutating side-effects on read-only attribute access within the ORM. Fixes: #4519 Change-Id: I06a058d24e6eb24b5c6b6092d3f8b31cf9c244ae --- lib/sqlalchemy/orm/state.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/sqlalchemy/orm/state.py') diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index c6252b6b8..f6c06acc8 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -310,6 +310,10 @@ class InstanceState(interfaces.InspectionAttrInfo): def _pending_mutations(self): return {} + @util.memoized_property + def _empty_collections(self): + return {} + @util.memoized_property def mapper(self): """Return the :class:`.Mapper` used for this mapped object.""" -- cgit v1.2.1