Skip to content

(🎁) Narrow type based on method return type of union #13530

Open
@KotlinIsland

Description

@KotlinIsland

I would expect the type of either to be narrowed to Maybe in the if block.

from typing import Literal

class Maybe:
    def should(self) -> bool:
        ...


class No:
    def should(self) -> Literal[False]:
        ...


no: No

if no.should():
    reveal_type(no)  # error: unreachable

either: Maybe | No

if either.should():
    reveal_type(either)   # Maybe | No

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @KotlinIsland@AlexWaygood

        Issue actions

          (🎁) Narrow type based on method return type of union · Issue #13530 · python/mypy