Skip to content

Commit 02c102d

Browse files
authored
[ty] Add tests: types.UnionType in isinstance/issubclass (#21537)
## Summary Add some tests documenting the fact that we don't support `types.UnionType` in `isinstance`/`issubclass` at the moment.
1 parent 29c24bc commit 02c102d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

crates/ty_python_semantic/resources/mdtest/narrow/isinstance.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,33 @@ def _(x: int | str | bytes):
147147
reveal_type(x) # revealed: (int & Unknown) | (str & Unknown) | (bytes & Unknown)
148148
```
149149

150+
## `classinfo` is a `types.UnionType`
151+
152+
Python 3.10 added the ability to use `Union[int, str]` as the second argument to `isinstance()`:
153+
154+
```py
155+
from typing import Union
156+
157+
IntOrStr = Union[int, str]
158+
159+
reveal_type(IntOrStr) # revealed: types.UnionType
160+
161+
def _(x: int | str | bytes | memoryview | range):
162+
# TODO: no error
163+
# error: [invalid-argument-type]
164+
if isinstance(x, IntOrStr):
165+
# TODO: Should be `int | str`
166+
reveal_type(x) # revealed: int | str | bytes | memoryview[int] | range
167+
# TODO: no error
168+
# error: [invalid-argument-type]
169+
elif isinstance(x, Union[bytes, memoryview]):
170+
# TODO: Should be `bytes | memoryview[int]`
171+
reveal_type(x) # revealed: int | str | bytes | memoryview[int] | range
172+
else:
173+
# TODO: Should be `range`
174+
reveal_type(x) # revealed: int | str | bytes | memoryview[int] | range
175+
```
176+
150177
## `classinfo` is a `typing.py` special form
151178

152179
Certain special forms in `typing.py` are aliases to classes elsewhere in the standard library; these

crates/ty_python_semantic/resources/mdtest/narrow/issubclass.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,33 @@ def _(x: type[int | str | bytes]):
200200
reveal_type(x) # revealed: (type[int] & Unknown) | (type[str] & Unknown) | (type[bytes] & Unknown)
201201
```
202202

203+
## `classinfo` is a `types.UnionType`
204+
205+
Python 3.10 added the ability to use `Union[int, str]` as the second argument to `issubclass()`:
206+
207+
```py
208+
from typing import Union
209+
210+
IntOrStr = Union[int, str]
211+
212+
reveal_type(IntOrStr) # revealed: types.UnionType
213+
214+
def f(x: type[int | str | bytes | range]):
215+
# TODO: No error
216+
# error: [invalid-argument-type]
217+
if issubclass(x, IntOrStr):
218+
# TODO: Should be `type[int] | type[str]`
219+
reveal_type(x) # revealed: type[int] | type[str] | type[bytes] | <class 'range'>
220+
# TODO: No error
221+
# error: [invalid-argument-type]
222+
elif issubclass(x, Union[bytes, memoryview]):
223+
# TODO: Should be `type[bytes]`
224+
reveal_type(x) # revealed: type[int] | type[str] | type[bytes] | <class 'range'>
225+
else:
226+
# TODO: Should be `<class 'range'>`
227+
reveal_type(x) # revealed: type[int] | type[str] | type[bytes] | <class 'range'>
228+
```
229+
203230
## Special cases
204231

205232
### Emit a diagnostic if the first argument is of wrong type

0 commit comments

Comments
 (0)