pylint-dev / pylint-dev/astroid
Support falsey classes for bool_value
- Dominant language
- Python
- Stars
- 582
- Forks
- 357
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
Originally reported by: **Claudiu Popa (BitBucket: [PCManticore](http://bitbucket.org/PCManticore), GitHub: @PCManticore)**
---
`Class.bool_value` returns true for a class whose metaclass defines `__len__` to return 0 — i.e. astroid ignores the metaclass-level falseyness.
```python
from astroid import parse
cls = parse('''
class Meta(type):
def __len__(self):
return 0
class C(metaclass=Meta):
pass
''')['C']
print(cls.bool_value())
```
---
### Confirmed still reproducing on astroid 4.2.0b3 (2026-05-25)
Both the `bool_value()` API and the inference of `bool(C)` ignore the metaclass `__len__`:
```python
import astroid
ast = astroid.parse('''
class Meta(type):
def __len__(self):
return 0
class C(metaclass=Meta):
pass
''')
print(ast['C'].bool_value())
# astroid 4.2.0b3 -> True ❌ (real Python: False)
node = astroid.extract_node('''
class Meta(type):
def __len__(cls):
return 0
class C(metaclass=Meta):
pass
bool(C) #@
''')
print(node.inferred())
# astroid 4.2.0b3 -> [] ❌
# real Python -> False
```
Both `ClassDef.bool_value` and the builtin-`bool()` inference path need to consult the metaclass's `__bool__`/`__len__` (in MRO order) before falling back to the default truthy-class assumption.
---
- Bitbucket: https://bitbucket.org/logilab/astroid/issue/183
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by tracing ClassDef.bool_value and the builtin-bool inference path, using the metaclass example in the issue as the reproducer. Check how metaclass __bool__ and __len__ are resolved in MRO order; done means both bool_value() and inferred bool(C) match Python and return False for the example.
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
- 48/100