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

Foo and Union[Foo, bool] fail to unify, when working with NamedTuple and recursively typed values #3069

Closed
mikeyhew opened this issue Mar 28, 2017 · 4 comments

Comments

@mikeyhew
Copy link

Obviously, this example is infinitely recursive. The original code that produced the error was more complicated so I distilled it down to the simplest example that still triggers the error.

from typing import Union, NamedTuple

class Foo(NamedTuple):
    bar: Union[Foo, bool]

def make_foo() -> Union[Foo, bool]:
    return Foo(make_foo())

# foo.py:7: error: Incompatible return value type (got "Foo", expected "Union[Foo, bool]")

Replacing the body of make_foo with the following does not get rid of the error, but produces an extra error which may or may not be unrelated.

bar: Union[Foo, bool] = make_foo()
return Foo(bar)

# foo.py:7: error: Incompatible types in assignment (expression has type "Union[Foo, bool]", variable has type "Union[Foo, bool]")
# foo.py:8: error: Incompatible return value type (got "Foo", expected "Union[Foo, bool]")````
@gvanrossum
Copy link
Member

Maybe it thinks that there are two different types named Foo?

You can debug this more by putting calls to reveal_type() in various places (search the mypy docs for how).

@pkch
Copy link
Contributor

pkch commented Mar 28, 2017

The changes required to fix this will likely cause merge conflicts with #3025; so it's probably better to fix it either after that PR is merged in, or inside it.

@mikeyhew mikeyhew changed the title Foo and Union[Foo, Bool] fail to unify, when working with NamedTuple and recursively typed values Foo and Union[Foo, bool] fail to unify, when working with NamedTuple and recursively typed values Mar 28, 2017
@mikeyhew
Copy link
Author

Using reveal_type, the types still look like they should be compatible.

reveal_type(make_foo)
# Revealed type is 'def () -> Union[Tuple[Union[foo.Foo, builtins.bool], fallback=foo.Foo], builtins.bool]'

reveal_type(Foo(True))
# Revealed type is 'Tuple[Union[foo.Foo, builtins.bool], fallback=foo.Foo]'

@ilevkivskyi
Copy link
Member

This is also fixed by #3952 both examples now work as expected, however a warning about forward references not fully supported is (expectedly) emitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants