Repository navigation
Compare unsaved model instances by identity - #2420
Merged
waketzheng merged 6 commits intoNov 19, 2026
Merged
Conversation
Two unsaved instances both have a primary key of None, so __eq__ reported them equal and list.remove() or index() acted on the wrong object. An instance without a primary key now equals only itself; hashing already refused such instances.
Yasser-Ameur
marked this pull request as ready for review
November 17, 2026 15:42
waketzheng
reviewed
Nov 18, 2026
Merging this PR will not alter performance1 warning
Comparing |
…nsaved-instances-not-equal
Extend the equality tests to pin down the boundaries of the identity-or-pk comparison rule: - `id=0` and `id=""` are valid primary keys, so they must participate in the pk comparison (`pk is not None`, not truthiness). - An unsaved instance and a subclass with a custom `__eq__` exercise both sides of the comparison protocol: the subclass's `__eq__` runs first for a strict subclass, and `SubClass` covers the parent's `type(other) is not self_type: return False` branch. - An unrelated type with a permissive `__eq__` verifies that returning `NotImplemented` actually defers to the other operand, including the case where the other side reads `pk` from an unsaved instance. Rename `first == first` / `second in [second]` to use an explicit alias, so the assertions read as "same object" rather than a self-comparison typo.
waketzheng
approved these changes
Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two unsaved instances of the same model compare equal, because both primary keys are
NoneandModel.__eq__only compares the keys:Model.__eq__now returnsself is otherwhen the instance has no primary key, and compares keys as before otherwise. This is the rule Django uses.Motivation and Context
__hash__already raises for an instance without a primary key, so hashing treats it as having no identity yet while equality treated all of them as the same object.list.remove(),index(),count()andinthen act on the wrong instance when a list holds objects that are not saved yet.Behaviour to be aware of: anything that has no key compares by identity now. Besides two fresh instances, that covers two
clone()copies and two rows loaded withonly()without the key, which used to be equal to each other. Models whose key has a default at construction (a UUID key) are not affected. I found no place insidetortoise/that relied on the old result.How Has This Been Tested?
Four tests next to
test_eqintests/test_model_methods.py.test_unsaved_instances_are_not_equalfails on develop; the other three pin what does not change (same key still equal, UUID default key, custom string key). SQLite suite on Python 3.14: 2241 passed, 150 skipped, 2 xfailed.ruff format --checkandruff checkare clean.Checklist: