Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NamedTuples reify into tuples #29

Open
rlouf opened this issue Jun 28, 2022 · 3 comments
Open

NamedTuples reify into tuples #29

rlouf opened this issue Jun 28, 2022 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@rlouf
Copy link
Contributor

rlouf commented Jun 28, 2022

While we can unify namedtuples with the same structure, reify returns a tuple when passed a namedtuple:

from typing import NamedTuple
from unification import unify, reify

class Test(NamedTuple):
    a: int
    b: int

test1 = Test(1, 4)
test2 = Test(var(), var())

res = unify(test1, test2)
print(res)

reified = reify(test2, res)
print(reified)
# (1, 4)

Trying to access elements by name obviously fails:

print(reified.a)

We would expect reify to return a namedtuple with the same structure instead.

@brandonwillard brandonwillard added bug Something isn't working help wanted Extra attention is needed enhancement New feature or request and removed bug Something isn't working labels Jul 6, 2022
@brandonwillard
Copy link
Member

There's only explicit dispatch constructor/_reify support for tuple, and since NamedTuple's type is a subclass of tuple, it's being interpreted as the latter.

We could attempt to add broader support for subclasses by assuming that their constructors take the same arguments as their base class constructors, but that's bound to fail in some cases, and failure as a default is probably worse than a successful base class result.

@brandonwillard brandonwillard changed the title Namedtuples reify into tuples NamedTuples reify into tuples Jul 6, 2022
@jarble
Copy link

jarble commented Jul 10, 2022

@brandonwillard Is this reify method documented anywhere? The README doesn't seem to mention it.

@brandonwillard
Copy link
Member

unification.core._reify is the dispatch function underlying unification.core.reify. It needs to be overloaded in order to add support for new types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants