Skip to content

Commit a66b84a

Browse files
AlexanderGrooffbrandonwillard
authored andcommitted
Reify object from base type
The currect assumption that `object` can be used to create a new object is not always valid. One example is the issue I'm currently facing: ``` >>> reify(ast.Num(n=var('x')), {var('x'): 123}) ... 55 def _reify_object_dict(o, s): ---> 56 obj = object.__new__(type(o)) 57 58 d = yield _reify(o.__dict__, s) TypeError: object.__new__(Num) is not safe, use Num.__new__() ```
1 parent f25b20d commit a66b84a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

tests/test_more.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ast import Num
12
from collections.abc import Mapping
23

34
from unification import var
@@ -29,6 +30,13 @@ def test_unify_object():
2930
assert stream_eval(_unify_object(Foo(1, 2), Foo(1, x), {})) == {x: 2}
3031

3132

33+
def test_unify_nonstandard_object():
34+
x = var()
35+
assert stream_eval(_unify_object(Num(n=1), Num(n=1), {})) == {}
36+
assert stream_eval(_unify_object(Num(n=1), Num(n=2), {})) is False
37+
assert stream_eval(_unify_object(Num(n=1), Num(n=x), {})) == {x: 1}
38+
39+
3240
def test_reify_object():
3341
x = var()
3442
obj = stream_eval(_reify_object(Foo(1, x), {x: 4}))

unification/more.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _reify_object(o, s):
5353

5454

5555
def _reify_object_dict(o, s):
56-
obj = object.__new__(type(o))
56+
obj = type(o).__new__(type(o))
5757

5858
d = yield _reify(o.__dict__, s)
5959

0 commit comments

Comments
 (0)