Skip to content
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

Don't consider 'object' always truthy #14510

Merged
merged 1 commit into from
Jan 23, 2023
Merged
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
1 change: 1 addition & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5151,6 +5151,7 @@ def _is_truthy_type(self, t: ProperType) -> bool:
and bool(t.type)
and not t.type.has_readable_member("__bool__")
and not t.type.has_readable_member("__len__")
and t.type.fullname != "builtins.object"
)
or isinstance(t, FunctionLike)
or (
Expand Down
26 changes: 22 additions & 4 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -803,12 +803,15 @@ from typing_extensions import TypedDict

Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not match variable name "Foo" [name-match]
[builtins fixtures/dict.pyi]

[case testTruthyBool]
# flags: --enable-error-code truthy-bool
from typing import List, Union
from typing import List, Union, Any

class Foo:
pass
class Bar:
pass

foo = Foo()
if foo: # E: "__main__.foo" has type "Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
Expand Down Expand Up @@ -836,15 +839,30 @@ if good_union:
if not good_union:
pass

bad_union: Union[Foo, object] = Foo()
if bad_union: # E: "__main__.bad_union" has type "Union[Foo, object]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
bad_union: Union[Foo, Bar] = Foo()
if bad_union: # E: "__main__.bad_union" has type "Union[Foo, Bar]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
pass
if not bad_union: # E: "__main__.bad_union" has type "Union[Foo, Bar]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
pass

# 'object' is special and is treated as potentially falsy
obj: object = Foo()
if obj:
pass
if not bad_union: # E: "__main__.bad_union" has type "object" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
if not obj:
pass

lst: List[int] = []
if lst:
pass

a: Any
if a:
pass

any_or_object: Union[object, Any]
if any_or_object:
pass
[builtins fixtures/list.pyi]

[case testTruthyFunctions]
Expand Down