Skip to content

Commit 8e01ad9

Browse files
authored
None.__bool__ returns Literal[False] (#11290)
Closes #11287
1 parent 332b712 commit 8e01ad9

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

mypy/checkmember.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,18 @@ def analyze_union_member_access(name: str, typ: UnionType, mx: MemberContext) ->
317317

318318

319319
def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> Type:
320-
if mx.chk.should_suppress_optional_error([typ]):
321-
return AnyType(TypeOfAny.from_error)
322320
is_python_3 = mx.chk.options.python_version[0] >= 3
323321
# In Python 2 "None" has exactly the same attributes as "object". Python 3 adds a single
324322
# extra attribute, "__bool__".
325323
if is_python_3 and name == '__bool__':
324+
literal_false = LiteralType(False, fallback=mx.named_type('builtins.bool'))
326325
return CallableType(arg_types=[],
327326
arg_kinds=[],
328327
arg_names=[],
329-
ret_type=mx.named_type('builtins.bool'),
328+
ret_type=literal_false,
330329
fallback=mx.named_type('builtins.function'))
330+
elif mx.chk.should_suppress_optional_error([typ]):
331+
return AnyType(TypeOfAny.from_error)
331332
else:
332333
return _analyze_member_access(name, mx.named_type('builtins.object'), mx)
333334

test-data/unit/check-basic.test

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,18 @@ def foo(
401401
[case testNoneHasBool]
402402
none = None
403403
b = none.__bool__()
404-
reveal_type(b) # N: Revealed type is "builtins.bool"
404+
reveal_type(b) # N: Revealed type is "Literal[False]"
405405
[builtins fixtures/bool.pyi]
406406

407+
[case testNoneHasBoolShowNoneErrorsFalse]
408+
none = None
409+
b = none.__bool__()
410+
reveal_type(b) # N: Revealed type is "Literal[False]"
411+
[builtins fixtures/bool.pyi]
412+
[file mypy.ini]
413+
\[mypy]
414+
show_none_errors = False
415+
407416
[case testAssignmentInvariantNoteForList]
408417
from typing import List
409418
x: List[int]

0 commit comments

Comments
 (0)