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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions