Skip to content

Commit

Permalink
None.__bool__ returns Literal[False] (#11290)
Browse files Browse the repository at this point in the history
Closes #11287
  • Loading branch information
sobolevn authored Oct 11, 2021
1 parent 332b712 commit 8e01ad9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,18 @@ def analyze_union_member_access(name: str, typ: UnionType, mx: MemberContext) ->


def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> Type:
if mx.chk.should_suppress_optional_error([typ]):
return AnyType(TypeOfAny.from_error)
is_python_3 = mx.chk.options.python_version[0] >= 3
# In Python 2 "None" has exactly the same attributes as "object". Python 3 adds a single
# extra attribute, "__bool__".
if is_python_3 and name == '__bool__':
literal_false = LiteralType(False, fallback=mx.named_type('builtins.bool'))
return CallableType(arg_types=[],
arg_kinds=[],
arg_names=[],
ret_type=mx.named_type('builtins.bool'),
ret_type=literal_false,
fallback=mx.named_type('builtins.function'))
elif mx.chk.should_suppress_optional_error([typ]):
return AnyType(TypeOfAny.from_error)
else:
return _analyze_member_access(name, mx.named_type('builtins.object'), mx)

Expand Down
11 changes: 10 additions & 1 deletion test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,18 @@ def foo(
[case testNoneHasBool]
none = None
b = none.__bool__()
reveal_type(b) # N: Revealed type is "builtins.bool"
reveal_type(b) # N: Revealed type is "Literal[False]"
[builtins fixtures/bool.pyi]

[case testNoneHasBoolShowNoneErrorsFalse]
none = None
b = none.__bool__()
reveal_type(b) # N: Revealed type is "Literal[False]"
[builtins fixtures/bool.pyi]
[file mypy.ini]
\[mypy]
show_none_errors = False

[case testAssignmentInvariantNoteForList]
from typing import List
x: List[int]
Expand Down

0 comments on commit 8e01ad9

Please sign in to comment.