Skip to content

Commit 9991c6b

Browse files
committed
Add deepcopy tests for BooleanExpression subclasses
Pydantic v2's BaseModel.__deepcopy__ calls cls.__new__(cls) with no args, but And, Or, and Not define __new__ with required positional arguments. These tests reproduce the TypeError on deepcopy.
1 parent b67b724 commit 9991c6b

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

tests/expressions/test_expressions.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
# pylint:disable=redefined-outer-name,eval-used
1818

19+
import copy
1920
import pickle
2021
import uuid
2122
from decimal import Decimal
@@ -1292,6 +1293,55 @@ def test_bind_ambiguous_name() -> None:
12921293
assert "Invalid schema, multiple fields for name foo.bar: 2 and 3" in str(exc_info)
12931294

12941295

1296+
# --- deepcopy tests ---
1297+
1298+
1299+
def test_deepcopy_and() -> None:
1300+
expr = And(EqualTo("x", 1), EqualTo("y", 2))
1301+
copied = copy.deepcopy(expr)
1302+
assert copied == expr
1303+
1304+
1305+
def test_deepcopy_or() -> None:
1306+
expr = Or(EqualTo("x", 1), EqualTo("y", 2))
1307+
copied = copy.deepcopy(expr)
1308+
assert copied == expr
1309+
1310+
1311+
def test_deepcopy_not() -> None:
1312+
expr = Not(EqualTo("x", 1))
1313+
copied = copy.deepcopy(expr)
1314+
assert copied == expr
1315+
1316+
1317+
def test_deepcopy_equal_to() -> None:
1318+
expr = EqualTo("x", 1)
1319+
copied = copy.deepcopy(expr)
1320+
assert copied == expr
1321+
1322+
1323+
def test_deepcopy_always_true() -> None:
1324+
copied = copy.deepcopy(AlwaysTrue())
1325+
assert copied is AlwaysTrue()
1326+
1327+
1328+
def test_deepcopy_always_false() -> None:
1329+
copied = copy.deepcopy(AlwaysFalse())
1330+
assert copied is AlwaysFalse()
1331+
1332+
1333+
def test_deepcopy_always_true_then_pickle() -> None:
1334+
copied = copy.deepcopy(AlwaysTrue())
1335+
restored = pickle.loads(pickle.dumps(copied))
1336+
assert restored is AlwaysTrue()
1337+
1338+
1339+
def test_deepcopy_nested_expression() -> None:
1340+
expr = And(Or(EqualTo("a", 1), EqualTo("b", 2)), Not(EqualTo("c", 3)))
1341+
copied = copy.deepcopy(expr)
1342+
assert copied == expr
1343+
1344+
12951345
# __ __ ___
12961346
# | \/ |_ _| _ \_ _
12971347
# | |\/| | || | _/ || |

0 commit comments

Comments
 (0)