Skip to content

Commit b1bf194

Browse files
authored
Fix model equality when using runtime extra configuration (#13062)
1 parent 17a35e3 commit b1bf194

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

pydantic/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,9 +1171,11 @@ def __eq__(self, other: Any) -> bool:
11711171

11721172
# Perform common checks first
11731173
if not (
1174-
self_type == other_type
1174+
self_type is other_type
11751175
and getattr(self, '__pydantic_private__', None) == getattr(other, '__pydantic_private__', None)
1176-
and self.__pydantic_extra__ == other.__pydantic_extra__
1176+
# We need to assume `None` and `{}` are equivalent, because extra behavior
1177+
# can be controlled at validation time:
1178+
and (self.__pydantic_extra__ or {}) == (other.__pydantic_extra__ or {})
11771179
):
11781180
return False
11791181

tests/test_main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2871,6 +2871,15 @@ class MyModel(BaseModel, extra='allow'):
28712871
assert MyModel(x=1) != MyModel()
28722872

28732873

2874+
def test_extra_equality_runtime_override() -> None:
2875+
"""https://github.com/pydantic/pydantic/issues/13051"""
2876+
2877+
class MyModel(BaseModel, extra='allow'):
2878+
a: int
2879+
2880+
assert MyModel.model_validate({'a': 1}) == MyModel.model_validate({'a': 1}, extra='forbid')
2881+
2882+
28742883
def test_equality_delegation():
28752884
from unittest.mock import ANY
28762885

0 commit comments

Comments
 (0)