Skip to content

Dropping types while joining Unions with Any #9425

Closed
@hauntsaninja

Description

@hauntsaninja

The revealed type in maybenotokay is probably a bug, especially given the revealed type in alsookay.
Dropping Any in defnotokay seems definitely not okay.

from typing import Any, Union

class A: ...
class B: ...
class C: ...
class D: ...

def okay(x: Union[A, B], y: Union[C, D], b: bool):
    reveal_type(x if b else y)  # Union[main.A, main.B, main.C, main.D]

def maybenotokay(x: Union[A, B], y: Union[Any, D], b: bool):
    reveal_type(x if b else y)  # Union[Any, main.D]
    
def alsookay(x: Union[A, Any], y: Union[C, D], b: bool):
    reveal_type(x if b else y)  # Union[main.A, Any, main.C, main.D]

def defnotokay(x: Union[A, Any], y: Union[A, D], b: bool):
    reveal_type(x if b else y)  # Union[main.A, main.D]

Thanks to an example in https://dimvar.blogspot.com/2020/09/trying-out-mypy.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions