Skip to content

enum incorrectly narrowed to Never when instance check inside method #2299

Closed

Description

@DetachHead

image

from enum import Enum, auto


class Foo(Enum):
    bar = auto()
    baz = auto()

    def foo(self) -> None:
        if self is Foo.bar:
            reveal_type(self)  # Type of "self" is "Never"


def foo(value: Foo) -> None:
    if value is Foo.bar:
        reveal_type(value)  # Type of "value" is "Literal[Foo.bar]"

VS Code extension or command-line
vscode, pylance v2021.9.1

Activity

  1. DetachHead commented on Sep 14, 2021

    @DetachHead
    ContributorAuthor

    also why does pyright call it Never? shouldn't it be NoReturn?

  2. erictraut commented on Sep 14, 2021

    @erictraut
    Collaborator

    Never is effectively the same as NoReturn, but NoReturn has a specific meaning in Python that would be confusing in this context since most people associate it with the return type of a function that never returns (hence the name). There has been discussion in the typing-sig of officially exporting a Never symbol from typing because it better reflects what happens when a type is narrowed to an empty union. The name Never is borrowed from TypeScript, where it appears as a built-in type.

  3. erictraut commented on Sep 14, 2021

    @erictraut
    Collaborator

    I agree this is a bug in the a is b type narrowing logic. Thanks for reporting it.

  4. erictraut commented on Sep 14, 2021

    @erictraut
    Collaborator

    This will be fixed in the next release.

  5. erictraut commented on Sep 16, 2021

    @erictraut
    Collaborator

    This is included in pyright 1.1.169. It will also be included in the next release of pylance.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions