Skip to content

Commit 92b28f4

Browse files
ddfishergvanrossum
authored andcommitted
Add more truthiness tests (#2040)
1 parent d1d52ea commit 92b28f4

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

test-data/unit/check-expressions.test

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,6 @@ i = None # type: str
310310
j = not b and i
311311
if j:
312312
reveal_type(j) # E: Revealed type is 'builtins.str'
313-
314-
315313
[builtins fixtures/bool.pyi]
316314

317315
[case testRestrictedTypeOr]
@@ -321,7 +319,13 @@ i = None # type: str
321319
j = b or i
322320
if not j:
323321
reveal_type(j) # E: Revealed type is 'builtins.str'
322+
[builtins fixtures/bool.pyi]
323+
324+
[case testAndOr]
324325

326+
s = ""
327+
b = bool()
328+
reveal_type(s and b or b) # E: Revealed type is 'builtins.bool'
325329
[builtins fixtures/bool.pyi]
326330

327331
[case testNonBooleanOr]

test-data/unit/check-isinstance.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,3 +1165,24 @@ reveal_type(l) # E: Revealed type is 'builtins.list[builtins.int*]'
11651165
reveal_type(g) # E: Revealed type is 'typing.Iterator[builtins.int*]'
11661166
reveal_type(d) # E: Revealed type is 'builtins.dict[builtins.int*, builtins.int*]'
11671167
[builtins fixtures/isinstancelist.pyi]
1168+
1169+
[case testIsinstanceInWrongOrderInBooleanOp]
1170+
class A:
1171+
m = 1
1172+
def f(x: object) -> None:
1173+
if x.m and isinstance(x, A) or False: # E: "object" has no attribute "m"
1174+
pass
1175+
[builtins fixtures/isinstance.pyi]
1176+
[out]
1177+
main: note: In function "f":
1178+
1179+
[case testIsinstanceAndOr]
1180+
class A:
1181+
a = None # type: A
1182+
1183+
def f(x: object) -> None:
1184+
b = isinstance(x, A) and x.a or A()
1185+
reveal_type(b) # E: Revealed type is '__main__.A'
1186+
[builtins fixtures/isinstance.pyi]
1187+
[out]
1188+
main: note: In function "f":

test-data/unit/check-optional.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ else:
7777
reveal_type(x) # E: Revealed type is 'builtins.int'
7878
[builtins fixtures/bool.pyi]
7979

80+
[case testOrCases]
81+
from typing import Optional
82+
x = None # type: Optional[str]
83+
y1 = x or 'a'
84+
reveal_type(y1) # E: Revealed type is 'builtins.str'
85+
y2 = x or 1
86+
reveal_type(y2) # E: Revealed type is 'Union[builtins.str, builtins.int]'
87+
z1 = 'a' or x
88+
reveal_type(z1) # E: Revealed type is 'Union[builtins.str, builtins.None]'
89+
z2 = 1 or x
90+
reveal_type(z2) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.None]'
91+
92+
[case testAndCases]
93+
from typing import Optional
94+
x = None # type: Optional[str]
95+
y1 = x and 'b'
96+
reveal_type(y1) # E: Revealed type is 'Union[builtins.str, builtins.None]'
97+
y2 = x and 1 # x could be '', so...
98+
reveal_type(y2) # E: Revealed type is 'Union[builtins.str, builtins.None, builtins.int]'
99+
z1 = 'b' and x
100+
reveal_type(z1) # E: Revealed type is 'Union[builtins.str, builtins.None]'
101+
z2 = 1 and x
102+
reveal_type(z2) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.None]'
103+
80104
[case testLambdaReturningNone]
81105
f = lambda: None
82106
x = f()

0 commit comments

Comments
 (0)