mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:22:24 +00:00
[ty] Fix false positives when subscripting an object inferred as having an Intersection
type (#18920)
This commit is contained in:
parent
3220242dec
commit
e44c489273
3 changed files with 52 additions and 18 deletions
|
@ -84,3 +84,21 @@ def _(flag: bool):
|
|||
|
||||
reveal_type(a) # revealed: str | Unknown
|
||||
```
|
||||
|
||||
## Intersection of nominal-instance types
|
||||
|
||||
If a subscript operation could succeed for *any* positive element of an intersection, no diagnostic
|
||||
should be reported even if it would not succeed for some other element of the intersection.
|
||||
|
||||
```py
|
||||
class Foo: ...
|
||||
|
||||
class Bar:
|
||||
def __getitem__(self, key: str) -> int:
|
||||
return 42
|
||||
|
||||
def f(x: Foo):
|
||||
if isinstance(x, Bar):
|
||||
# TODO: should be `int`
|
||||
reveal_type(x["whatever"]) # revealed: @Todo(Subscript expressions on intersections)
|
||||
```
|
||||
|
|
|
@ -210,8 +210,12 @@ def test3(val: tuple[str] | tuple[int] | int):
|
|||
### Intersection subscript access
|
||||
|
||||
```py
|
||||
from ty_extensions import Intersection, Not
|
||||
from ty_extensions import Intersection
|
||||
|
||||
def test4(val: Intersection[tuple[str], tuple[int]]):
|
||||
reveal_type(val[0]) # revealed: str & int
|
||||
class Foo: ...
|
||||
class Bar: ...
|
||||
|
||||
def test4(val: Intersection[tuple[Foo], tuple[Bar]]):
|
||||
# TODO: should be `Foo & Bar`
|
||||
reveal_type(val[0]) # revealed: @Todo(Subscript expressions on intersections)
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue