Skip to content

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

Closed
@mikeyhew

Description

@mikeyhew

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]")````

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions