Spurious error for code that fails truthy-bool even when not enabled
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
It seems that code failing to pass the truthy-bool check can have spurious errors that occur, even when that optional error code isn't enabled.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=53000744586f03d23bb53f807d5a0ea0
class Truthy:
pass
# def __bool__(self) -> bool: return True
def foo(truthies: list[Truthy]) -> None:
sum(1 if x else 0 for x in truthies)
Expected Behavior
This code should typecheck fine: sum on ints is sensible!
Uncommenting the __bool__ definition (so that the truthy-bool error no longer applies, if it was enabled) passes type checking.
Actual Behavior
main.py:4: error: Generator has incompatible item type "int"; expected "bool" [misc]
(Note that the truthy-bool error doesn't occur, as expected, because it isn't enabled.)
When running with that enabled one sees:
$ mypy testing.py --enable-error-code=truthy-bool
testing.py:7: error: Generator has incompatible item type "int"; expected "bool" [misc]
testing.py:7: error: "x" has type "Truthy" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
Your Environment
- Mypy version used: 1.12.1
- Mypy command-line flags: N/A see playground
- Mypy configuration options from
mypy.ini(and other config files): N/A see playground - Python version used: 3.12
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked mypy-play reproducer and run the example with and without --enable-error-code=truthy-bool. Trace the generator expression's reported item-type error and verify that the code typechecks without the optional error code while preserving the intended truthy-bool diagnostic when enabled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100