diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2022-01-20 22:44:00 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2022-01-20 22:44:00 +0000 |
| commit | 297aebf2b686d17ad275d22e2dcacfb5884510ab (patch) | |
| tree | 3b7a6cb6884c3d274c2b6c9ad3aec788e8341292 /lib/sqlalchemy | |
| parent | dda5c43cab88daad02bc871cf40bf4984e94a031 (diff) | |
| parent | 769dc1d2cb217c66918bec01718aa657a0239040 (diff) | |
| download | sqlalchemy-297aebf2b686d17ad275d22e2dcacfb5884510ab.tar.gz | |
Merge "repair mapper sort" into main
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/session.py | 20 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/typing.py | 7 |
2 files changed, 17 insertions, 10 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index aa642e65e..cf47ee729 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -3389,14 +3389,24 @@ class Session(_SessionClassMethods): """ - def key(state): - return (state.mapper, state.key is not None) - obj_states = (attributes.instance_state(obj) for obj in objects) + if not preserve_order: - obj_states = sorted(obj_states, key=key) + # the purpose of this sort is just so that common mappers + # and persistence states are grouped together, so that groupby + # will return a single group for a particular type of mapper. + # it's not trying to be deterministic beyond that. + obj_states = sorted( + obj_states, + key=lambda state: (id(state.mapper), state.key is not None), + ) - for (mapper, isupdate), states in itertools.groupby(obj_states, key): + def grouping_key(state): + return (state.mapper, state.key is not None) + + for (mapper, isupdate), states in itertools.groupby( + obj_states, grouping_key + ): self._bulk_save_mappings( mapper, states, diff --git a/lib/sqlalchemy/util/typing.py b/lib/sqlalchemy/util/typing.py index e2d42db6e..5767d258b 100644 --- a/lib/sqlalchemy/util/typing.py +++ b/lib/sqlalchemy/util/typing.py @@ -6,6 +6,8 @@ from typing import Type from typing import TypeVar from typing import Union +from typing_extensions import NotRequired # noqa + from . import compat _T = TypeVar("_T", bound=Any) @@ -26,11 +28,6 @@ else: from typing_extensions import Concatenate # noqa from typing_extensions import ParamSpec # noqa -if compat.py311: - from typing import NotRequired # noqa -else: - from typing_extensions import NotRequired # noqa - _T = TypeVar("_T") |
