File tree Expand file tree Collapse file tree 3 files changed +16
-8
lines changed Expand file tree Collapse file tree 3 files changed +16
-8
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Unreleased
4
4
5
+ - Do not error on boolean operations on values typed
6
+ as ` object ` (#388 )
5
7
- Support some imports from stub-only modules (#386 )
6
8
- Support type evaluation functions in stubs (#386 )
7
9
- Support ` TypedDict ` in stubs (#386 )
Original file line number Diff line number Diff line change @@ -142,7 +142,7 @@ def _get_boolability_no_mvv(value: Value) -> Boolability:
142
142
return Boolability .value_always_true_mutable
143
143
else :
144
144
return Boolability .value_always_false_mutable
145
- type_boolability = _get_type_boolability (type (value .val ))
145
+ type_boolability = _get_type_boolability (type (value .val ), is_exact = True )
146
146
if boolean_value :
147
147
if type_boolability is Boolability .boolable :
148
148
return Boolability .value_always_true
@@ -169,7 +169,11 @@ def _get_boolability_no_mvv(value: Value) -> Boolability:
169
169
assert False , f"unhandled value { value !r} "
170
170
171
171
172
- def _get_type_boolability (typ : type ) -> Boolability :
172
+ def _get_type_boolability (typ : type , * , is_exact : bool = False ) -> Boolability :
173
+ # Special-case object as boolable because it could easily be a subtype
174
+ # that does support __bool__.
175
+ if typ is object and not is_exact :
176
+ return Boolability .boolable
173
177
if safe_hasattr (typ , "__len__" ):
174
178
return Boolability .boolable
175
179
dunder_bool = safe_getattr (typ , "__bool__" , None )
Original file line number Diff line number Diff line change @@ -86,7 +86,7 @@ def test_get_boolability() -> None:
86
86
# TypedValue
87
87
assert Boolability .boolable == get_boolability (TypedValue (HasLen ))
88
88
assert Boolability .erroring_bool == get_boolability (future )
89
- assert Boolability .type_always_true == get_boolability (TypedValue (object ))
89
+ assert Boolability .boolable == get_boolability (TypedValue (object ))
90
90
assert Boolability .boolable == get_boolability (TypedValue (int ))
91
91
92
92
# MultiValuedValue and AnnotatedValue
@@ -166,12 +166,14 @@ def __len__(self):
166
166
167
167
@assert_passes ()
168
168
def test_object ():
169
+ obj = object ()
170
+
169
171
def capybara ():
170
- True if object () else False # E: type_always_true
171
- object () and False # E: type_always_true
172
- [] and object () and False # E: type_always_true
173
- object () or True # E: type_always_true
174
- not object () # E: type_always_true
172
+ True if obj else False # E: type_always_true
173
+ obj and False # E: type_always_true
174
+ [] and obj and False # E: type_always_true
175
+ obj or True # E: type_always_true
176
+ not obj # E: type_always_true
175
177
176
178
@assert_passes ()
177
179
def test_async_yield_or (self ):
You can’t perform that action at this time.
0 commit comments