Skip to content

Overriding __bool__ in NamedTuple doesn't work #11819

Closed
@Stannislav

Description

@Stannislav

Even if __bool__ is overridden in a subclass of NamedTuple, mypy thinks instances of this subclass can't evaluate to False.

This possibility was mentioned in #3601 (comment) before, but it appears this wasn't fixed.

To Reproduce
Consider this code example.

# x.py
from typing import NamedTuple


class C(NamedTuple):
    x: int

    def __bool__(self) -> bool:
        return self.x > 0


def hello(c: C) -> None:
    if c:
        print("True branch")
    else:
        print("False branch")


hello(C(0))
hello(C(1))

Mypy reports a "Statement is unreachable"

$ mypy x.py 
x.py:15: error: Statement is unreachable  [unreachable]
Found 1 error in 1 file (checked 1 source file)

while the statement clearly is reachable:

$ python x.py
False branch
True branch

Your Environment

  • Mypy version used: 0.930
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.10.0
  • Operating system and version: macOS Big Sur

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