python / python/mypy

bool(obj) does not infer from obj.__bool__()

Open
#15,523 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-narrowing
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

(Although there are other issues that allude to this problem, I haven't been able to locate an issue which specifically addresses truthiness where __bool__() is defined and @final. The search term "truthy bool" may be helpful for finding related issues.)

Implicit truthiness does not (always?) take an object's __bool__() into account, even when that method is @final.

To Reproduce

This example is posted to mypy-play.net.

from typing import *

@final
class SomeClass:
    def __bool__(self) -> Literal[True]:
        return True

instance: SomeClass

reveal_type(instance.__bool__())    # 'Literal[True]' - correct
reveal_type(bool(instance))         # 'bool' - incorrect (should be 'Literal[True]')

if instance:
    reveal_type(instance)           # 'SomeClass' - correct
else:
    assert_never(instance)          # ...'SomeClass' - incorrect (should be silent)

(Whether the class or method is decorated @final makes no difference.)

Expected Behavior

if instance: ... and bool(instnace) should take into account that SomeClass has a @final __bool__() with return type Literal[True], and consistently treat instances of SomeClass as definitely truthy.

In general, implicit truthiness for an object should take into account the truthiness of the return type of any @final __bool__() the object's class defines.

Comments inline in the above code highlight differences from expected behaviour.

Actual Behavior

main.py:10: note: Revealed type is "Literal[True]"
main.py:11: note: Revealed type is "builtins.bool"
main.py:14: note: Revealed type is "__main__.SomeClass"
main.py:16: error: Argument 1 to "assert_never" has incompatible type "SomeClass"; expected "NoReturn"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.4.0, master (2023-06-26)
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the main.py reproducer from the issue and compare the handling of instance.bool(), bool(instance), and truthiness narrowing in if statements. Trace the relevant type-inference and narrowing entry points, then add coverage for a final bool() returning Literal[True]. Done means bool(instance) reveals Literal[True] and the unreachable else branch no longer produces an assert_never error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.