|
| 1 | +# Narrowing for `isinstance` checks |
| 2 | + |
| 3 | +Narrowing for `isinstance(object, classinfo)` expressions. |
| 4 | + |
| 5 | +## `classinfo` is a single type |
| 6 | + |
| 7 | +```py |
| 8 | +x = 1 if flag else "a" |
| 9 | + |
| 10 | +if isinstance(x, int): |
| 11 | + reveal_type(x) # revealed: Literal[1] |
| 12 | + |
| 13 | +if isinstance(x, str): |
| 14 | + reveal_type(x) # revealed: Literal["a"] |
| 15 | + if isinstance(x, int): |
| 16 | + reveal_type(x) # revealed: Never |
| 17 | + |
| 18 | +if isinstance(x, (int, object)): |
| 19 | + reveal_type(x) # revealed: Literal[1] | Literal["a"] |
| 20 | +``` |
| 21 | + |
| 22 | +## `classinfo` is a tuple of types |
| 23 | + |
| 24 | +Note: `isinstance(x, (int, str))` should not be confused with |
| 25 | +`isinstance(x, tuple[(int, str)])`. The former is equivalent to |
| 26 | +`isinstance(x, int | str)`: |
| 27 | + |
| 28 | +```py |
| 29 | +x = 1 if flag else "a" |
| 30 | + |
| 31 | +if isinstance(x, (int, str)): |
| 32 | + reveal_type(x) # revealed: Literal[1] | Literal["a"] |
| 33 | + |
| 34 | +if isinstance(x, (int, bytes)): |
| 35 | + reveal_type(x) # revealed: Literal[1] |
| 36 | + |
| 37 | +if isinstance(x, (bytes, str)): |
| 38 | + reveal_type(x) # revealed: Literal["a"] |
| 39 | + |
| 40 | +# No narrowing should occur if a larger type is also |
| 41 | +# one of the possibilities: |
| 42 | +if isinstance(x, (int, object)): |
| 43 | + reveal_type(x) # revealed: Literal[1] | Literal["a"] |
| 44 | + |
| 45 | +y = 1 if flag1 else "a" if flag2 else b"b" |
| 46 | +if isinstance(y, (int, str)): |
| 47 | + reveal_type(y) # revealed: Literal[1] | Literal["a"] |
| 48 | + |
| 49 | +if isinstance(y, (int, bytes)): |
| 50 | + reveal_type(y) # revealed: Literal[1] | Literal[b"b"] |
| 51 | + |
| 52 | +if isinstance(y, (str, bytes)): |
| 53 | + reveal_type(y) # revealed: Literal["a"] | Literal[b"b"] |
| 54 | +``` |
| 55 | + |
| 56 | +## `classinfo` is a nested tuple of types |
| 57 | + |
| 58 | +```py |
| 59 | +x = 1 if flag else "a" |
| 60 | + |
| 61 | +if isinstance(x, (bool, (bytes, int))): |
| 62 | + reveal_type(x) # revealed: Literal[1] |
| 63 | +``` |
| 64 | + |
| 65 | +## Class types |
| 66 | + |
| 67 | +```py |
| 68 | +class A: ... |
| 69 | + |
| 70 | + |
| 71 | +class B: ... |
| 72 | + |
| 73 | + |
| 74 | +def get_object() -> object: ... |
| 75 | + |
| 76 | + |
| 77 | +x = get_object() |
| 78 | + |
| 79 | +if isinstance(x, A): |
| 80 | + reveal_type(x) # revealed: A |
| 81 | + if isinstance(x, B): |
| 82 | + reveal_type(x) # revealed: A & B |
| 83 | +``` |
| 84 | + |
| 85 | +## No narrowing for instances of `builtins.type` |
| 86 | + |
| 87 | +```py |
| 88 | +t = type("t", (), {}) |
| 89 | + |
| 90 | +# This isn't testing what we want it to test if we infer anything more precise here: |
| 91 | +reveal_type(t) # revealed: type |
| 92 | +x = 1 if flag else "foo" |
| 93 | + |
| 94 | +if isinstance(x, t): |
| 95 | + reveal_type(x) # revealed: Literal[1] | Literal["foo"] |
| 96 | +``` |
| 97 | + |
| 98 | +## Do not use custom `isinstance` for narrowing |
| 99 | + |
| 100 | +```py |
| 101 | +def isinstance(x, t): |
| 102 | + return True |
| 103 | + |
| 104 | + |
| 105 | +x = 1 if flag else "a" |
| 106 | +if isinstance(x, int): |
| 107 | + reveal_type(x) # revealed: Literal[1] | Literal["a"] |
| 108 | +``` |
| 109 | + |
| 110 | +## Do support narrowing if `isinstance` is aliased |
| 111 | + |
| 112 | +```py |
| 113 | +isinstance_alias = isinstance |
| 114 | + |
| 115 | +x = 1 if flag else "a" |
| 116 | +if isinstance_alias(x, int): |
| 117 | + reveal_type(x) # revealed: Literal[1] |
| 118 | +``` |
| 119 | + |
| 120 | +## Do support narrowing if `isinstance` is imported |
| 121 | + |
| 122 | +```py |
| 123 | +from builtins import isinstance as imported_isinstance |
| 124 | + |
| 125 | +x = 1 if flag else "a" |
| 126 | +if imported_isinstance(x, int): |
| 127 | + reveal_type(x) # revealed: Literal[1] |
| 128 | +``` |
| 129 | + |
| 130 | +## Do not narrow if second argument is not a type |
| 131 | + |
| 132 | +```py |
| 133 | +x = 1 if flag else "a" |
| 134 | + |
| 135 | +# TODO: this should cause us to emit a diagnostic during |
| 136 | +# type checking |
| 137 | +if isinstance(x, "a"): |
| 138 | + reveal_type(x) # revealed: Literal[1] | Literal["a"] |
| 139 | + |
| 140 | +# TODO: this should cause us to emit a diagnostic during |
| 141 | +# type checking |
| 142 | +if isinstance(x, "int"): |
| 143 | + reveal_type(x) # revealed: Literal[1] | Literal["a"] |
| 144 | +``` |
| 145 | + |
| 146 | +## Do not narrow if there are keyword arguments |
| 147 | + |
| 148 | +```py |
| 149 | +x = 1 if flag else "a" |
| 150 | + |
| 151 | +# TODO: this should cause us to emit a diagnostic |
| 152 | +# (`isinstance` has no `foo` parameter) |
| 153 | +if isinstance(x, int, foo="bar"): |
| 154 | + reveal_type(x) # revealed: Literal[1] | Literal["a"] |
| 155 | +``` |
0 commit comments