Skip to content

Now Type[None].__bool__ is a thing, refs #11539 #11551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ def analyze_type_type_member_access(name: str,
# Access member on metaclass object via Type[Type[C]]
if isinstance(typ.item.item, Instance):
item = typ.item.item.type.metaclass_type
elif isinstance(typ.item, NoneType) and name == '__bool__':
# Special case, `type(None).__bool__` is defined on python3.
return analyze_none_member_access(name, typ.item, mx, from_none_type=True)
if item and not mx.is_operator:
# See comment above for why operators are skipped
result = analyze_class_attribute_access(item, name, mx, override_info)
Expand All @@ -316,17 +319,30 @@ def analyze_union_member_access(name: str, typ: UnionType, mx: MemberContext) ->
return make_simplified_union(results)


def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> Type:
def analyze_none_member_access(
name: str, typ: NoneType, mx: MemberContext,
*,
from_none_type: bool = False,
) -> Type:
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=literal_false,
fallback=mx.named_type('builtins.function'))
arg_types = []
arg_kinds = []
arg_names = []
if from_none_type: # This means that `type(None).__bool__` is accessed.
arg_types.append(typ)
arg_kinds.append(ARG_POS)
arg_names.append('self')
return CallableType(
arg_types=arg_types,
arg_kinds=arg_kinds,
arg_names=arg_names,
ret_type=literal_false,
fallback=mx.named_type('builtins.function'),
)
elif mx.chk.should_suppress_optional_error([typ]):
return AnyType(TypeOfAny.from_error)
else:
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@ b = none.__bool__()
reveal_type(b) # N: Revealed type is "Literal[False]"
[builtins fixtures/bool.pyi]

[case testNoneHasBoolPython27]
# flags: --python-version 2.7
none = None
b = none.__bool__() # E: "None" has no attribute "__bool__"
reveal_type(b) # N: Revealed type is "Any"
[builtins fixtures/bool.pyi]

[case testNoneHasBoolShowNoneErrorsFalse]
none = None
b = none.__bool__()
Expand All @@ -413,6 +420,28 @@ reveal_type(b) # N: Revealed type is "Literal[False]"
\[mypy]
show_none_errors = False

[case testTypeNoneHasBool]
from typing import Type
none: Type[None]
reveal_type(none.__bool__) # N: Revealed type is "def (self: None) -> Literal[False]"
[builtins fixtures/bool.pyi]

[case testTypeNoneHasBoolPython27]
# flags: --python-version 2.7
from typing import Type
none = type(None) # type: Type[None]
none.__bool__ # E: "Type[None]" has no attribute "__bool__"
[builtins fixtures/bool.pyi]

[case testTypeNoneHasBoolShowNoneErrorsFalse]
from typing import Type
none: Type[None]
reveal_type(none.__bool__) # N: Revealed type is "def (self: None) -> 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