Skip to content

Commit 6712919

Browse files
committed
[1/2] Support 'not isinstance'
If we have an expression of the form "not instance" then we can recurse and just switch the else and if expressions. We might as well take this opportunity.
1 parent bc0323e commit 6712919

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

mypy/checker.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,10 +2097,23 @@ def find_isinstance_check(node: Node,
20972097
if kind == ISINSTANCE_ALWAYS_TRUE:
20982098
kind = ISINSTANCE_OVERLAPPING
20992099
return (var, type, AnyType(), kind)
2100+
elif isinstance(node, UnaryExpr) and node.op == 'not':
2101+
(var, type, elsetype, kind) = find_isinstance_check(node.expr, type_map, weak)
2102+
return (var, elsetype, type, invert_isinstance_kind(kind))
2103+
21002104
# Not a supported isinstance check
21012105
return None, AnyType(), AnyType(), -1
21022106

21032107

2108+
def invert_isinstance_kind(kind: int) -> int:
2109+
if kind == ISINSTANCE_ALWAYS_TRUE:
2110+
return ISINSTANCE_ALWAYS_FALSE
2111+
elif kind == ISINSTANCE_ALWAYS_FALSE:
2112+
return ISINSTANCE_ALWAYS_TRUE
2113+
else:
2114+
return kind
2115+
2116+
21042117
def get_isinstance_type(node: Node, type_map: Dict[Node, Type]) -> Type:
21052118
type = type_map[node]
21062119
if isinstance(type, FunctionLike):

0 commit comments

Comments
 (0)