ruff
73b1fce7 - [ty] Add diagnostics for `isinstance()` and `issubclass()` calls that use invalid PEP-604 unions for their second argument (#21343)

Commit
61 days ago
[ty] Add diagnostics for `isinstance()` and `issubclass()` calls that use invalid PEP-604 unions for their second argument (#21343) ## Summary This PR adds extra validation for `isinstance()` and `issubclass()` calls that use `UnionType` instances for their second argument. According to typeshed's annotations, any `UnionType` is accepted for the second argument, but this isn't true at runtime: at runtime, all elements in the `UnionType` must either be class objects or be `None` in order for the `isinstance()` or `issubclass()` call to reliably succeed: ```pycon % uvx python3.14 Python 3.14.0 (main, Oct 10 2025, 12:54:13) [Clang 20.1.4 ] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from typing import LiteralString >>> import types >>> type(LiteralString | int) is types.UnionType True >>> isinstance(42, LiteralString | int) Traceback (most recent call last): File "<python-input-5>", line 1, in <module> isinstance(42, LiteralString | int) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 559, in __instancecheck__ raise TypeError(f"{self} cannot be used with isinstance()") TypeError: typing.LiteralString cannot be used with isinstance() ``` ## Test Plan Added mdtests/snapshots
Author
Parents
Loading