mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-18 11:41:21 +00:00
[ty] Silence false-positive diagnostics when using typing.Dict or typing.Callable as the second argument to isinstance() (#21386)
This commit is contained in:
parent
bd8812127d
commit
03bd0619e9
5 changed files with 132 additions and 2 deletions
|
|
@ -162,3 +162,38 @@ def _(x: A | B, y: list[int]):
|
|||
reveal_type(x) # revealed: B & ~A
|
||||
reveal_type(isinstance(x, B)) # revealed: Literal[True]
|
||||
```
|
||||
|
||||
Certain special forms in the typing module are not instances of `type`, so are strictly-speaking
|
||||
disallowed as the second argument to `isinstance()` according to typeshed's annotations. However, at
|
||||
runtime they work fine as the second argument, and we implement that special case in ty:
|
||||
|
||||
```py
|
||||
import typing as t
|
||||
|
||||
# no errors emitted for any of these:
|
||||
isinstance("", t.Dict)
|
||||
isinstance("", t.List)
|
||||
isinstance("", t.Set)
|
||||
isinstance("", t.FrozenSet)
|
||||
isinstance("", t.Tuple)
|
||||
isinstance("", t.ChainMap)
|
||||
isinstance("", t.Counter)
|
||||
isinstance("", t.Deque)
|
||||
isinstance("", t.OrderedDict)
|
||||
isinstance("", t.Callable)
|
||||
isinstance("", t.Type)
|
||||
isinstance("", t.Callable | t.Deque)
|
||||
|
||||
# `Any` is valid in `issubclass()` calls but not `isinstance()` calls
|
||||
issubclass(list, t.Any)
|
||||
issubclass(list, t.Any | t.Dict)
|
||||
```
|
||||
|
||||
But for other special forms that are not permitted as the second argument, we still emit an error:
|
||||
|
||||
```py
|
||||
isinstance("", t.TypeGuard) # error: [invalid-argument-type]
|
||||
isinstance("", t.ClassVar) # error: [invalid-argument-type]
|
||||
isinstance("", t.Final) # error: [invalid-argument-type]
|
||||
isinstance("", t.Any) # error: [invalid-argument-type]
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue