Alex Waygood
73b1fce74a
[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
2025-11-10 08:46:31 +00:00
..
2025-05-21 11:06:27 -04:00
2025-08-27 13:21:47 +01:00
2025-10-03 17:14:51 -04:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-08-05 07:41:37 -07:00
2025-08-29 19:48:45 +01:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-10-19 10:58:25 +01:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-10-19 10:58:25 +01:00
2025-06-27 12:40:33 +00:00
2025-06-02 10:52:26 +00:00
2025-06-02 10:52:26 +00:00
2025-10-16 13:25:08 +00:00
2025-08-26 11:01:16 -04:00
2025-08-26 11:01:16 -04:00
2025-08-26 11:01:16 -04:00
2025-07-23 08:24:12 -07:00
2025-10-12 18:39:32 +00:00
2025-07-18 23:50:29 +00:00
2025-08-25 17:01:51 +00:00
2025-08-29 13:22:23 +00:00
2025-07-08 20:29:07 +02:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-23 13:16:21 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-06-24 20:27:37 +00:00
2025-08-25 19:39:05 +01:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-08-21 22:00:44 +01:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-07-28 22:06:33 +00:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-08-21 22:00:44 +01:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-08-14 14:38:33 -07:00
2025-08-14 14:38:33 -07:00
2025-10-15 11:13:32 +02:00
2025-08-14 14:38:33 -07:00
2025-09-23 14:26:55 +00:00
2025-08-14 14:38:33 -07:00
2025-08-14 14:38:33 -07:00
2025-06-09 00:40:05 +01:00
2025-05-21 15:38:56 -04:00
2025-06-09 00:40:05 +01:00
2025-11-10 08:46:31 +00:00
2025-11-10 08:46:31 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-10-09 21:08:37 +00:00
2025-05-13 18:43:19 +02:00
2025-08-21 22:00:44 +01:00
2025-10-27 11:19:12 +00:00
2025-05-21 18:02:39 -04:00
2025-10-27 11:19:12 +00:00
2025-08-01 12:20:32 -04:00
2025-09-05 10:12:40 +02:00
2025-08-18 12:03:01 +00:00
2025-05-15 11:39:14 -04:00
2025-05-15 11:39:14 -04:00
2025-05-14 11:13:41 -04:00
2025-05-14 11:13:41 -04:00
2025-05-14 11:13:41 -04:00
2025-05-14 11:13:41 -04:00
2025-05-13 18:43:19 +02:00
2025-10-12 18:39:32 +00:00
2025-10-07 21:52:57 +00:00
2025-05-13 18:43:19 +02:00
2025-10-29 21:22:38 +00:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-10-07 21:52:57 +00:00
2025-10-12 18:39:32 +00:00
2025-10-18 15:01:46 +01:00
2025-05-13 18:43:19 +02:00
2025-10-12 18:39:32 +00:00
2025-10-12 18:39:32 +00:00
2025-05-29 23:17:18 +00:00
2025-07-28 15:52:59 +02:00
2025-07-28 15:52:59 +02:00
2025-05-14 08:26:48 +02:00
2025-05-29 23:17:18 +00:00
2025-05-29 23:17:18 +00:00
2025-10-07 12:23:11 -04:00
2025-08-29 08:44:18 +00:00
2025-05-13 18:43:19 +02:00
2025-10-13 19:30:49 -04:00
2025-10-13 19:30:49 -04:00
2025-10-16 11:56:32 -04:00
2025-10-17 17:13:40 +00:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-06-20 08:36:49 +05:30
2025-06-20 08:36:49 +05:30
2025-10-27 11:19:12 +00:00
2025-10-23 09:34:39 +02:00
2025-08-05 07:41:37 -07:00
2025-08-21 22:00:44 +01:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-07-24 08:24:26 +00:00
2025-11-03 16:57:49 -05:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-06-24 18:02:05 -07:00
2025-10-08 11:21:26 +01:00
2025-10-17 11:50:58 +00:00
2025-08-21 22:00:44 +01:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-06-27 15:29:04 -04:00
2025-08-26 11:01:16 -04:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-08-26 11:01:16 -04:00
2025-05-13 18:43:19 +02:00
2025-05-21 11:06:27 -04:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-05-13 18:43:19 +02:00
2025-10-16 11:56:32 -04:00