Skip to content

Commit

Permalink
Reify object from base type
Browse files Browse the repository at this point in the history
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__()
```
  • Loading branch information
AlexanderGrooff committed Jan 9, 2021
1 parent f25b20d commit 6aab8e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/test_more.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ast import Num
from collections.abc import Mapping

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


def test_unify_nonstandard_object():
x = var()
assert stream_eval(_unify_object(Num(n=1), Num(n=1), {})) == {}
assert stream_eval(_unify_object(Num(n=1), Num(n=2), {})) is False
assert stream_eval(_unify_object(Num(n=1), Num(n=x), {})) == {x: 1}


def test_reify_object():
x = var()
obj = stream_eval(_reify_object(Foo(1, x), {x: 4}))
Expand Down
2 changes: 1 addition & 1 deletion unification/more.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _reify_object(o, s):


def _reify_object_dict(o, s):
obj = object.__new__(type(o))
obj = type(o).__new__(type(o))

d = yield _reify(o.__dict__, s)

Expand Down

0 comments on commit 6aab8e2

Please sign in to comment.