Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature requests: recognise that some types always evaluate to False #8692

Description

@reinhrst
  • Are you reporting a bug, or opening a feature request?
    Feature Request
  • Please insert below the code you are checking with mypy,
import typing as t


class NotPresent:
    def __bool__(self) -> t.Literal[False]:
        return False


x: t.Union[NotPresent, t.Sequence[int]] = [1]
reveal_type(x)
if x:
    print(x[0])
  • What is the actual behavior/output?
    test.py:12:11: error: Value of type "Union[NotPresent, Sequence[int]]" is not indexable

  • What is the behavior/output you expect?
    The if x branch should recognise that x cannot be of type NotPresent (as it would do if it was None rather than NotPresent)

  • What are the versions of mypy and Python you are using?
    0.770 & master

  • What are the mypy flags you are using? (For example --strict-optional)
    None

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

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions