Skip to content

Commit 5625e16

Browse files
authored
Fix some edge-case bugs around improper use of isinstance() (#405)
These all stem from the fact that `True == 1` and `isinstance(False, int)`
1 parent c68c8d8 commit 5625e16

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

pyi.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ def _check_subscript_version_check(self, node: ast.Compare) -> None:
14331433
slc = version_info.slice
14341434
if isinstance(slc, ast.Constant):
14351435
# anything other than the integer 0 doesn't make much sense
1436-
if isinstance(slc.value, int) and slc.value == 0:
1436+
if type(slc.value) is int and slc.value == 0:
14371437
must_be_single = True
14381438
else:
14391439
self.error(node, Y003)
@@ -1445,7 +1445,7 @@ def _check_subscript_version_check(self, node: ast.Compare) -> None:
14451445
elif (
14461446
# allow only [:1] and [:2]
14471447
isinstance(slc.upper, ast.Constant)
1448-
and isinstance(slc.upper.value, int)
1448+
and type(slc.upper.value) is int
14491449
and slc.upper.value in {1, 2}
14501450
):
14511451
can_have_strict_equals = slc.upper.value
@@ -1471,8 +1471,9 @@ def _check_version_check(
14711471
) -> None:
14721472
comparator = node.comparators[0]
14731473
if must_be_single:
1474-
if not isinstance(comparator, ast.Constant) or not isinstance(
1475-
comparator.value, int
1474+
if (
1475+
not isinstance(comparator, ast.Constant)
1476+
or type(comparator.value) is not int
14761477
):
14771478
self.error(node, Y003)
14781479
elif not isinstance(comparator, ast.Tuple):

tests/sysversioninfo.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import sys
22

33
if sys.version_info[0] == 2: ...
4+
if sys.version_info[0] == True: ... # Y003 Unrecognized sys.version_info check # E712 comparison to True should be 'if cond is True:' or 'if cond:'
45
if sys.version_info[0.0] == 2: ... # Y003 Unrecognized sys.version_info check
6+
if sys.version_info[False] == 2: ... # Y003 Unrecognized sys.version_info check
57
if sys.version_info[0j] == 2: ... # Y003 Unrecognized sys.version_info check
68
if sys.version_info[0] == (2, 7): ... # Y003 Unrecognized sys.version_info check
79
if sys.version_info[0] == '2': ... # Y003 Unrecognized sys.version_info check
810
if sys.version_info[1:] >= (7, 11): ... # Y003 Unrecognized sys.version_info check
911
if sys.version_info[::-1] < (11, 7): ... # Y003 Unrecognized sys.version_info check
1012
if sys.version_info[:3] >= (2, 7): ... # Y003 Unrecognized sys.version_info check
13+
if sys.version_info[:True] >= (2, 7): ... # Y003 Unrecognized sys.version_info check
1114
if sys.version_info[:1] == (2,): ...
15+
if sys.version_info[:1] == (True,): ... # Y003 Unrecognized sys.version_info check
1216
if sys.version_info[:1] == (2, 7): ... # Y005 Version comparison must be against a length-1 tuple
1317
if sys.version_info[:2] == (2, 7): ...
1418
if sys.version_info[:2] == (2,): ... # Y005 Version comparison must be against a length-2 tuple

0 commit comments

Comments
 (0)