[ty] Add tests: types.UnionType in isinstance/issubclass (#21537)
Some checks are pending
CI / mkdocs (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (${{ github.repository == 'astral-sh/ruff' && 'depot-windows-2022-16' || 'windows-latest' }}) (push) Blocked by required conditions
CI / cargo test (macos-latest) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

Add some tests documenting the fact that we don't support
`types.UnionType` in `isinstance`/`issubclass` at the moment.
This commit is contained in:
David Peter 2025-11-20 12:59:36 +01:00 committed by GitHub
parent 29c24bc8a6
commit 02c102da88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 54 additions and 0 deletions

View file

@ -147,6 +147,33 @@ def _(x: int | str | bytes):
reveal_type(x) # revealed: (int & Unknown) | (str & Unknown) | (bytes & Unknown)
```
## `classinfo` is a `types.UnionType`
Python 3.10 added the ability to use `Union[int, str]` as the second argument to `isinstance()`:
```py
from typing import Union
IntOrStr = Union[int, str]
reveal_type(IntOrStr) # revealed: types.UnionType
def _(x: int | str | bytes | memoryview | range):
# TODO: no error
# error: [invalid-argument-type]
if isinstance(x, IntOrStr):
# TODO: Should be `int | str`
reveal_type(x) # revealed: int | str | bytes | memoryview[int] | range
# TODO: no error
# error: [invalid-argument-type]
elif isinstance(x, Union[bytes, memoryview]):
# TODO: Should be `bytes | memoryview[int]`
reveal_type(x) # revealed: int | str | bytes | memoryview[int] | range
else:
# TODO: Should be `range`
reveal_type(x) # revealed: int | str | bytes | memoryview[int] | range
```
## `classinfo` is a `typing.py` special form
Certain special forms in `typing.py` are aliases to classes elsewhere in the standard library; these

View file

@ -200,6 +200,33 @@ def _(x: type[int | str | bytes]):
reveal_type(x) # revealed: (type[int] & Unknown) | (type[str] & Unknown) | (type[bytes] & Unknown)
```
## `classinfo` is a `types.UnionType`
Python 3.10 added the ability to use `Union[int, str]` as the second argument to `issubclass()`:
```py
from typing import Union
IntOrStr = Union[int, str]
reveal_type(IntOrStr) # revealed: types.UnionType
def f(x: type[int | str | bytes | range]):
# TODO: No error
# error: [invalid-argument-type]
if issubclass(x, IntOrStr):
# TODO: Should be `type[int] | type[str]`
reveal_type(x) # revealed: type[int] | type[str] | type[bytes] | <class 'range'>
# TODO: No error
# error: [invalid-argument-type]
elif issubclass(x, Union[bytes, memoryview]):
# TODO: Should be `type[bytes]`
reveal_type(x) # revealed: type[int] | type[str] | type[bytes] | <class 'range'>
else:
# TODO: Should be `<class 'range'>`
reveal_type(x) # revealed: type[int] | type[str] | type[bytes] | <class 'range'>
```
## Special cases
### Emit a diagnostic if the first argument is of wrong type