You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are two reasons I'm proposing this change. First, we know that
many subclasses of 'object' can be falsy. Second, mypy sometimes
simplifies `object | Any` into just `object`. The latter was
considered always truthy, while the prior one wasn't. Now both of them
are treated consistently. An alternative fix would be to not simplify
unions like `object | Any`, but this seems a bit ad hoc.
Fixes#14480. This doesn't just fix the regression but fixes a more
general issue.
Copy file name to clipboardExpand all lines: test-data/unit/check-errorcodes.test
+22-4Lines changed: 22 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -803,12 +803,15 @@ from typing_extensions import TypedDict
803
803
804
804
Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not match variable name "Foo" [name-match]
805
805
[builtins fixtures/dict.pyi]
806
+
806
807
[case testTruthyBool]
807
808
# flags: --enable-error-code truthy-bool
808
-
from typing import List, Union
809
+
from typing import List, Union, Any
809
810
810
811
class Foo:
811
812
pass
813
+
class Bar:
814
+
pass
812
815
813
816
foo = Foo()
814
817
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]
@@ -836,15 +839,30 @@ if good_union:
836
839
if not good_union:
837
840
pass
838
841
839
-
bad_union: Union[Foo, object] = Foo()
840
-
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]
842
+
bad_union: Union[Foo, Bar] = Foo()
843
+
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]
844
+
pass
845
+
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]
846
+
pass
847
+
848
+
# 'object' is special and is treated as potentially falsy
849
+
obj: object = Foo()
850
+
if obj:
841
851
pass
842
-
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]
0 commit comments