]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
KeyFuncDict regression fixes and dataclass fixes
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 4 Mar 2023 20:31:41 +0000 (15:31 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 5 Mar 2023 22:58:39 +0000 (17:58 -0500)
commit4b5e7e4f5bc262ac5b4cb8f93a594bfa1507b9e6
tree492e713863d1462c137dca6e7670e01ae2a34668
parent15a9aad96232dd3353084453a8789dbd714c33b7
KeyFuncDict regression fixes and dataclass fixes

adapt None-key warning for non-mapped attributes

Fixed multiple regressions due to :ticket:`8372`, involving
:func:`_orm.attribute_mapped_collection` (now called
:func:`_orm.attribute_keyed_dict`).

First, the collection was no longer usable with "key" attributes that were
not themselves ordinary mapped attributes; attributes linked to descriptors
and/or association proxy attributes have been fixed.

Second, if an event or other operation needed access to the "key" in order
to populate the dictionary from an mapped attribute that was not
loaded, this also would raise an error inappropriately, rather than
trying to load the attribute as was the behavior in 1.4.  This is also
fixed.

For both cases, the behavior of :ticket:`8372` has been expanded.
:ticket:`8372` introduced an error that raises when the derived key that
would be used as a mapped dictionary key is effectively unassigned. In this
change, a warning only is emitted if the effective value of the ".key"
attribute is ``None``, where it cannot be unambiguously determined if this
``None`` was intentional or not. ``None`` will be not supported as mapped
collection dictionary keys going forward (as it typically refers to NULL
which means "unknown"). Setting
:paramref:`_orm.attribute_keyed_dict.ignore_unpopulated_attribute` will now
cause such ``None`` keys to be ignored as well.

Add value constructors to dictionary collections

Added constructor arguments to the built-in mapping collection types
including :class:`.KeyFuncDict`, :func:`_orm.attribute_keyed_dict`,
:func:`_orm.column_keyed_dict` so that these dictionary types may be
constructed in place given the data up front; this provides further
compatibility with tools such as Python dataclasses ``.asdict()`` which
relies upon invoking these classes directly as ordinary dictionary classes.

Fixes: #9418
Fixes: #9424
Change-Id: Ib16c4e690b7ac3fcc34df2f139cad61c6c4b2b19
doc/build/changelog/changelog_20.rst
doc/build/changelog/unreleased_20/9418.rst [new file with mode: 0644]
doc/build/changelog/unreleased_20/9424.rst [new file with mode: 0644]
lib/sqlalchemy/orm/mapped_collection.py
lib/sqlalchemy/util/preloaded.py
test/ext/test_associationproxy.py
test/orm/test_collection.py