mirror of
https://github.com/astral-sh/ruff.git
synced 2026-05-06 08:56:57 -04:00
[ty] do not union Unknown into unannotated container types (#23718)
## Summary Part of https://github.com/astral-sh/ty/issues/1240 Stop unioning `Unknown` into the types of un-annotated container literals. We discussed perhaps continuing to union `Unknown` if the inferred type is a singleton type like `None`. I'd like to explore this as a separate change so we can see the ecosystem impact more clearly. ## Test Plan Adjusted many mdtest expectations. There's one test case that regresses with this change, because we don't fully support union type contexts (it can require a lot of repeat inference in pathological cases). So `x10: list[int | str] | list[int | None] = [1, 2, 3]` previously passed only because we inferred the RHS as `list[Unknown | int]` -- now we infer it as `list[int]` and the assignment fails due to invariance. I've kept this test as a TODO since it's not trivial to fix. Mypy errors in the same way we now do, suggesting it's not necessarily a huge priority either. ## Ecosystem This change is expected to cause new diagnostics and some false positives, since we are replacing very-forgiving gradual types with non-gradual inference heuristics. Many of these issues could be solved or significantly mitigated by https://github.com/astral-sh/ty/issues/1473, depending how far we are able to go with that, and particularly whether we can afford to apply it also to container literals which are not empty at construction. The downside of broad application of this approach is that in some cases it could cause us to widen container types when the user actually just made a mistake and added the wrong thing to a container, and would prefer an error at that location. Some categories of new error that show up in the ecosystem report: ### Implicit TypedDicts These are cases where the dictionary is heterogeneous and would ideally be typed as a `TypedDict` but isn't, for example: ```py def make_person(photo: bytes | None): person = {"name": "Pat", age: 29} if photo is not None: person["photo"] = photo ``` We (and pyrefly, and pyright in strict mode) error on the last line here because we already inferred `dict[str, str | int]`, so we can't add a `bytes` value. Mypy prefers common-base joins over union joins, so it infers `dict[str, object]`, which avoids the error adding a `bytes` value. This means the value type is less precise, which theoretically means potentially more errors using values from the dict later. But in practice with this heterogeneous pattern, either `object` or the union will cause similar problems when using values from the dict -- in either case you'd probably have to cast or narrow. Pyright (in non-strict mode) has a special case where it falls back to `Unknown` when it sees heterogenous value types, so it infers this as `dict[str, Unknown]`. I think we could consider either the mypy or pyright approaches here, but we don't need to do it in this PR; we can file an issue and consider it as a follow-up. Another symptom of this same root cause is repetitive diagnostics arising from a large union inferred as value type; the same fixes would address this. ### Negative intersections, particularly with e.g. `~AlwaysFalsy` or `~None`. Example: ```py class A: ... def _(a: A | None) -> dict[str, A]: if a: d = {"a": a} return d return {} ``` We error on `return d` because "expected `dict[str, A]`, found `dict[str, A & ~AlwaysFalsy]`". This is an issue specific to intersection types, so no other type checker has this problem. I think when we "promote literals" (we may need to give this operation a broader name -- it's really "type promotion to give a better inferred type when invariance means too-precise is bad") we should also eliminate all negative types from intersections. I would prefer to do this as a separate PR for easier review and better visibility of ecosystem impact, but I think it's high priority to land soon after this PR (ideally before a release). ### Overly-precise inference for singleton `None` This did show up, to the tune of ~100 new diagnostics ([example](https://github.com/pytorch/ignite/blob/b73a4c20e991b3e14949f2a69651ed2a7219f2fd/tests/ignite/engine/test_engine.py#L158)), so I think it is worth addressing as a follow-up.
This commit is contained in:
@@ -128,7 +128,7 @@ static COLOUR_SCIENCE: Benchmark = Benchmark::new(
|
||||
max_dep_date: "2025-06-17",
|
||||
python_version: PythonVersion::PY310,
|
||||
},
|
||||
400,
|
||||
350,
|
||||
);
|
||||
|
||||
static FREQTRADE: Benchmark = Benchmark::new(
|
||||
@@ -171,7 +171,7 @@ static PANDAS: Benchmark = Benchmark::new(
|
||||
max_dep_date: "2025-06-17",
|
||||
python_version: PythonVersion::PY312,
|
||||
},
|
||||
4500,
|
||||
4600,
|
||||
);
|
||||
|
||||
static PYDANTIC: Benchmark = Benchmark::new(
|
||||
@@ -215,7 +215,7 @@ static TANJUN: Benchmark = Benchmark::new(
|
||||
max_dep_date: "2025-06-17",
|
||||
python_version: PythonVersion::PY312,
|
||||
},
|
||||
150,
|
||||
120,
|
||||
);
|
||||
|
||||
static STATIC_FRAME: Benchmark = Benchmark::new(
|
||||
|
||||
@@ -4547,11 +4547,11 @@ def function():
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @"
|
||||
list[Unknown | int]
|
||||
assert_snapshot!(test.hover(), @r###"
|
||||
list[int]
|
||||
---------------------------------------------
|
||||
```python
|
||||
list[Unknown | int]
|
||||
list[int]
|
||||
```
|
||||
---------------------------------------------
|
||||
info[hover]: Hovered content is
|
||||
@@ -4562,7 +4562,7 @@ def function():
|
||||
| |
|
||||
| source
|
||||
|
|
||||
");
|
||||
"###);
|
||||
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
|
||||
+379
-766
File diff suppressed because it is too large
Load Diff
@@ -445,13 +445,14 @@ reveal_type(x8) # revealed: Literal[True]
|
||||
x9: int | str = f2(True)
|
||||
reveal_type(x9) # revealed: Literal[True]
|
||||
|
||||
# TODO: We could choose a concrete type here.
|
||||
# TODO: Should not error. We could choose a concrete type here (pyright arbitrarily picks the
|
||||
# first), or keep the union (pyrefly does this). Mypy infers `list[int]` and errors.
|
||||
# error: [invalid-assignment]
|
||||
x10: list[int | str] | list[int | None] = [1, 2, 3]
|
||||
reveal_type(x10) # revealed: list[Unknown | int]
|
||||
reveal_type(x10) # revealed: list[int | str] | list[int | None]
|
||||
|
||||
# TODO: And here similarly.
|
||||
x11: Sequence[int | str] | Sequence[int | None] = [1, 2, 3]
|
||||
reveal_type(x11) # revealed: list[Unknown | int]
|
||||
reveal_type(x11) # revealed: list[int]
|
||||
```
|
||||
|
||||
## Annotations influence generic call argument inference
|
||||
@@ -480,12 +481,12 @@ reveal_type(x2) # revealed: TD
|
||||
|
||||
# error: [missing-typed-dict-key] "Missing required key 'x' in TypedDict `TD` constructor"
|
||||
# error: [invalid-key] "Unknown key "y" for TypedDict `TD`"
|
||||
# error: [invalid-assignment] "Object of type `TD | dict[Unknown | str, Unknown | int]` is not assignable to `TD`"
|
||||
# error: [invalid-assignment] "Object of type `TD | dict[str, int]` is not assignable to `TD`"
|
||||
x3: TD = first([{"y": 0}, {"x": 1}])
|
||||
|
||||
# error: [missing-typed-dict-key] "Missing required key 'x' in TypedDict `TD` constructor"
|
||||
# error: [invalid-key] "Unknown key "y" for TypedDict `TD`"
|
||||
# error: [invalid-assignment] "Object of type `TD | None | dict[Unknown | str, Unknown | int]` is not assignable to `TD | None`"
|
||||
# error: [invalid-assignment] "Object of type `TD | None | dict[str, int]` is not assignable to `TD | None`"
|
||||
x4: TD | None = first([{"y": 0}, {"x": 1}])
|
||||
```
|
||||
|
||||
@@ -708,19 +709,16 @@ x6: Iterable[list[Any]] = [[1, 2, 3]]
|
||||
reveal_type(x6) # revealed: list[list[Any]]
|
||||
|
||||
x7: Sequence[Any] = [i for i in [1, 2, 3]]
|
||||
# TODO: This should infer `list[int]`.
|
||||
reveal_type(x7) # revealed: list[Unknown | int]
|
||||
reveal_type(x7) # revealed: list[int]
|
||||
|
||||
x8: MutableSequence[Any] = [i for i in [1, 2, 3]]
|
||||
reveal_type(x8) # revealed: list[Any]
|
||||
|
||||
x9: Iterable[Any] = [i for i in [1, 2, 3]]
|
||||
# TODO: This should infer `list[int]`.
|
||||
reveal_type(x9) # revealed: list[Unknown | int]
|
||||
reveal_type(x9) # revealed: list[int]
|
||||
|
||||
x10: Iterable[Iterable[Any]] = [[i] for i in [1, 2, 3]]
|
||||
# TODO: This should infer `list[list[int]]`.
|
||||
reveal_type(x10) # revealed: list[list[Unknown | int]]
|
||||
reveal_type(x10) # revealed: list[list[int]]
|
||||
|
||||
x11: list[Iterable[Any]] = [[i] for i in [1, 2, 3]]
|
||||
reveal_type(x11) # revealed: list[Iterable[Any]]
|
||||
|
||||
@@ -2643,8 +2643,8 @@ class C3:
|
||||
def replace_with(self, other: "C3"):
|
||||
self.x = [self.x[0].flip()]
|
||||
|
||||
# TODO: should be `Unknown | list[Unknown | Sub] | list[Unknown | Base]`
|
||||
reveal_type(C3(Sub()).x) # revealed: Unknown | list[Unknown | Sub] | list[Divergent]
|
||||
# TODO: should be `Unknown | list[Sub] | list[Base]`
|
||||
reveal_type(C3(Sub()).x) # revealed: Unknown | list[Sub] | list[Divergent]
|
||||
```
|
||||
|
||||
And cycles between many attributes:
|
||||
@@ -2702,8 +2702,8 @@ class ManyCycles2:
|
||||
self.x3 = [1]
|
||||
|
||||
def f1(self: "ManyCycles2"):
|
||||
# TODO: should be Unknown | list[Unknown | int] | list[Divergent]
|
||||
reveal_type(self.x3) # revealed: Unknown | list[Unknown | int] | list[Unknown] | list[Divergent]
|
||||
# TODO: should be Unknown | list[int] | list[Divergent]
|
||||
reveal_type(self.x3) # revealed: Unknown | list[int] | list[Unknown] | list[Divergent]
|
||||
|
||||
self.x1 = [self.x2] + [self.x3]
|
||||
self.x2 = [self.x1] + [self.x3]
|
||||
|
||||
@@ -64,7 +64,7 @@ d3: dict[str, int] = {"x": 1}
|
||||
d4: TD = dict(x=1)
|
||||
d5: TD = dict(x="1") # error: [invalid-argument-type]
|
||||
|
||||
reveal_type(d1) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type(d1) # revealed: dict[str, int]
|
||||
reveal_type(d2) # revealed: TD
|
||||
reveal_type(d3) # revealed: dict[str, int]
|
||||
reveal_type(d4) # revealed: TD
|
||||
|
||||
@@ -541,7 +541,7 @@ type("Foo", Base, {})
|
||||
# error: 17 [invalid-base] "Invalid class base with type `Literal[2]`"
|
||||
type("Foo", (1, 2), {})
|
||||
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter 3 (`namespace`) of `type()`: Expected `dict[str, Any]`, found `dict[Unknown | bytes, Unknown | int]`"
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter 3 (`namespace`) of `type()`: Expected `dict[str, Any]`, found `dict[bytes, int]`"
|
||||
type("Foo", (Base,), {b"attr": 1})
|
||||
```
|
||||
|
||||
|
||||
@@ -853,12 +853,12 @@ Type inference accounts for parameter type annotations across all signatures in
|
||||
```py
|
||||
from typing import TypedDict, overload
|
||||
|
||||
class T(TypedDict):
|
||||
class TD(TypedDict):
|
||||
x: int
|
||||
|
||||
def _(flag: bool):
|
||||
if flag:
|
||||
def f(x: T) -> int:
|
||||
def f(x: TD) -> int:
|
||||
return 1
|
||||
|
||||
else:
|
||||
@@ -867,7 +867,7 @@ def _(flag: bool):
|
||||
x = f({"x": 1})
|
||||
reveal_type(x) # revealed: int
|
||||
|
||||
# error: [invalid-argument-type] "Argument to function `f` is incorrect: Expected `T`, found `dict[str, int] & dict[Unknown | str, Unknown | int]`"
|
||||
# error: [invalid-argument-type] "Argument to function `f` is incorrect: Expected `TD`, found `dict[str, int]`"
|
||||
f({"y": 1})
|
||||
```
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ The type of the expression being iterated over is immutable, and so should not b
|
||||
|
||||
```py
|
||||
# TODO: This should reveal `Literal["a", "b"]`
|
||||
# revealed: Unknown | str
|
||||
# revealed: str
|
||||
x = [reveal_type(string) for string in ["a", "b"]]
|
||||
```
|
||||
|
||||
@@ -140,16 +140,16 @@ The type of the comprehension expression itself should reflect the inferred elem
|
||||
```py
|
||||
from typing import TypedDict, Literal
|
||||
|
||||
# revealed: list[Unknown | int]
|
||||
# revealed: list[int]
|
||||
reveal_type([x for x in range(10)])
|
||||
|
||||
# revealed: set[Unknown | int]
|
||||
# revealed: set[int]
|
||||
reveal_type({x for x in range(10)})
|
||||
|
||||
# revealed: dict[Unknown | int, Unknown | str]
|
||||
# revealed: dict[int, str]
|
||||
reveal_type({x: str(x) for x in range(10)})
|
||||
|
||||
# revealed: list[Unknown | tuple[int, Unknown | str]]
|
||||
# revealed: list[tuple[int, str]]
|
||||
reveal_type([(x, y) for x in range(5) for y in ["a", "b", "c"]])
|
||||
|
||||
squares: list[int | None] = [x**2 for x in range(10)]
|
||||
@@ -162,18 +162,17 @@ Inference for comprehensions takes the type context into account:
|
||||
from typing import Sequence
|
||||
|
||||
# Without type context:
|
||||
reveal_type([x for x in [1, 2, 3]]) # revealed: list[Unknown | int]
|
||||
reveal_type({x: "a" for x in [1, 2, 3]}) # revealed: dict[Unknown | int, Unknown | str]
|
||||
reveal_type({str(x): x for x in [1, 2, 3]}) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type({x for x in [1, 2, 3]}) # revealed: set[Unknown | int]
|
||||
reveal_type([x for x in [1, 2, 3]]) # revealed: list[int]
|
||||
reveal_type({x: "a" for x in [1, 2, 3]}) # revealed: dict[int, str]
|
||||
reveal_type({str(x): x for x in [1, 2, 3]}) # revealed: dict[str, int]
|
||||
reveal_type({x for x in [1, 2, 3]}) # revealed: set[int]
|
||||
|
||||
# With type context:
|
||||
x1: list[int] = [x for x in [1, 2, 3]]
|
||||
reveal_type(x1) # revealed: list[int]
|
||||
|
||||
x2: Sequence[int] = [x for x in [1, 2, 3]]
|
||||
# TODO: This should reveal `list[int]`.
|
||||
reveal_type(x2) # revealed: list[Unknown | int]
|
||||
reveal_type(x2) # revealed: list[int]
|
||||
|
||||
x3: dict[int, str] = {x: str(x) for x in [1, 2, 3]}
|
||||
reveal_type(x3) # revealed: dict[int, str]
|
||||
@@ -186,7 +185,7 @@ This also works for nested comprehensions:
|
||||
|
||||
```py
|
||||
table = [[(x, y) for x in range(3)] for y in range(3)]
|
||||
reveal_type(table) # revealed: list[Unknown | list[Unknown | tuple[int, int]]]
|
||||
reveal_type(table) # revealed: list[list[tuple[int, int]]]
|
||||
|
||||
table_with_content: list[list[tuple[int, int, str | None]]] = [[(x, y, None) for x in range(3)] for y in range(3)]
|
||||
reveal_type(table_with_content) # revealed: list[list[tuple[int, int, str | None]]]
|
||||
@@ -216,9 +215,9 @@ y4: list[Person] = [{"misspelled": n} for n in ["Alice", "Bob"]]
|
||||
We promote literals to avoid overly-precise types in invariant positions:
|
||||
|
||||
```py
|
||||
reveal_type([x for x in ("a", "b", "c")]) # revealed: list[Unknown | str]
|
||||
reveal_type({x for x in (1, 2, 3)}) # revealed: set[Unknown | int]
|
||||
reveal_type({k: 0 for k in ("a", "b", "c")}) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type([x for x in ("a", "b", "c")]) # revealed: list[str]
|
||||
reveal_type({x for x in (1, 2, 3)}) # revealed: set[int]
|
||||
reveal_type({k: 0 for k in ("a", "b", "c")}) # revealed: dict[str, int]
|
||||
```
|
||||
|
||||
Type context can prevent this promotion from happening:
|
||||
|
||||
@@ -152,7 +152,7 @@ class Cyclic:
|
||||
if isinstance(self.data, str):
|
||||
self.data = {"url": self.data}
|
||||
|
||||
# revealed: Unknown | str | dict[Unknown, Unknown] | dict[Unknown | str, Unknown | str]
|
||||
# revealed: Unknown | str | dict[Unknown, Unknown] | dict[str, str]
|
||||
reveal_type(Cyclic("").data)
|
||||
```
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ def delete():
|
||||
del d # error: [unresolved-reference] "Name `d` used when not defined"
|
||||
|
||||
delete()
|
||||
reveal_type(d) # revealed: list[Unknown | int]
|
||||
reveal_type(d) # revealed: list[int]
|
||||
|
||||
def delete_element():
|
||||
# When the `del` target isn't a name, it doesn't force local resolution.
|
||||
@@ -62,7 +62,7 @@ def delete_global():
|
||||
|
||||
delete_global()
|
||||
# Again, the variable should have been removed, but we don't check it.
|
||||
reveal_type(d) # revealed: list[Unknown | int]
|
||||
reveal_type(d) # revealed: list[int]
|
||||
|
||||
def delete_nonlocal():
|
||||
e = 2
|
||||
|
||||
@@ -406,8 +406,8 @@ def head[T](my_list: MyList[T]) -> T:
|
||||
def get_value[K, V](my_dict: MyDict[K, V], key: K) -> V:
|
||||
return my_dict[key]
|
||||
|
||||
reveal_type(head([1, 2])) # revealed: Unknown | int
|
||||
reveal_type(head(["a", "b"])) # revealed: Unknown | str
|
||||
reveal_type(head([1, 2])) # revealed: int
|
||||
reveal_type(head(["a", "b"])) # revealed: str
|
||||
|
||||
d: dict[str, int] = {"a": 1}
|
||||
reveal_type(get_value(d, "a")) # revealed: int
|
||||
|
||||
@@ -1033,7 +1033,7 @@ reveal_type(generic_context(c.generic_method))
|
||||
|
||||
reveal_type(c.generic_method) # revealed: [T](value: T) -> T
|
||||
reveal_type(c.generic_method(100)) # revealed: Literal[100]
|
||||
reveal_type(c.generic_method([1, 2, 3])) # revealed: list[Unknown | int]
|
||||
reveal_type(c.generic_method([1, 2, 3])) # revealed: list[int]
|
||||
```
|
||||
|
||||
## Callable protocols with `ParamSpec` and class constructors
|
||||
|
||||
@@ -784,7 +784,7 @@ class A: ...
|
||||
from subexporter import *
|
||||
|
||||
# TODO: we could potentially infer `list[str] | tuple[str, ...]` here
|
||||
reveal_type(__all__) # revealed: list[Unknown | str]
|
||||
reveal_type(__all__) # revealed: list[str]
|
||||
|
||||
__all__.append("B")
|
||||
|
||||
|
||||
@@ -9,13 +9,13 @@ reveal_type({}) # revealed: dict[Unknown, Unknown]
|
||||
## Basic dict
|
||||
|
||||
```py
|
||||
reveal_type({1: 1, 2: 1}) # revealed: dict[Unknown | int, Unknown | int]
|
||||
reveal_type({1: 1, 2: 1}) # revealed: dict[int, int]
|
||||
```
|
||||
|
||||
## Dict of tuples
|
||||
|
||||
```py
|
||||
reveal_type({1: (1, 2), 2: (3, 4)}) # revealed: dict[Unknown | int, Unknown | tuple[int, int]]
|
||||
reveal_type({1: (1, 2), 2: (3, 4)}) # revealed: dict[int, tuple[int, int]]
|
||||
```
|
||||
|
||||
## Unpacked dict
|
||||
@@ -26,7 +26,7 @@ from typing import Mapping, KeysView
|
||||
a = {"a": 1, "b": 2}
|
||||
b = {"c": 3, "d": 4}
|
||||
c = {**a, **b}
|
||||
reveal_type(c) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type(c) # revealed: dict[str, int]
|
||||
|
||||
# revealed: list[int | str]
|
||||
# revealed: list[int | str]
|
||||
@@ -41,9 +41,9 @@ class HasKeysAndGetItem:
|
||||
return 42
|
||||
|
||||
def _(a: dict[str, int], b: Mapping[str, int], c: HasKeysAndGetItem, d: object):
|
||||
reveal_type({**a}) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type({**b}) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type({**c}) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type({**a}) # revealed: dict[str, int]
|
||||
reveal_type({**b}) # revealed: dict[str, int]
|
||||
reveal_type({**c}) # revealed: dict[str, int]
|
||||
|
||||
# error: [invalid-argument-type] "Argument expression after ** must be a mapping type: Found `object`"
|
||||
reveal_type({**d}) # revealed: dict[Unknown, Unknown]
|
||||
@@ -59,20 +59,20 @@ def b(_: int) -> int:
|
||||
return 1
|
||||
|
||||
x = {1: a, 2: b}
|
||||
reveal_type(x) # revealed: dict[Unknown | int, Unknown | ((_: int) -> int)]
|
||||
reveal_type(x) # revealed: dict[int, (_: int) -> int]
|
||||
```
|
||||
|
||||
## Mixed dict
|
||||
|
||||
```py
|
||||
# revealed: dict[Unknown | str, Unknown | int | tuple[int, int] | tuple[int, int, int]]
|
||||
# revealed: dict[str, int | tuple[int, int] | tuple[int, int, int]]
|
||||
reveal_type({"a": 1, "b": (1, 2), "c": (1, 2, 3)})
|
||||
```
|
||||
|
||||
## Dict comprehensions
|
||||
|
||||
```py
|
||||
# revealed: dict[Unknown | int, Unknown | int]
|
||||
# revealed: dict[int, int]
|
||||
reveal_type({x: y for x, y in enumerate(range(42))})
|
||||
```
|
||||
|
||||
@@ -85,7 +85,7 @@ individual keys:
|
||||
from typing import TypedDict
|
||||
|
||||
x1 = {"a": 1, "b": "2"}
|
||||
reveal_type(x1) # revealed: dict[Unknown | str, Unknown | int | str]
|
||||
reveal_type(x1) # revealed: dict[str, int | str]
|
||||
reveal_type(x1["a"]) # revealed: Literal[1]
|
||||
reveal_type(x1["b"]) # revealed: Literal["2"]
|
||||
|
||||
@@ -107,7 +107,7 @@ reveal_type(x3[2]) # revealed: TD
|
||||
|
||||
x4 = {"a": 1, "b": {"c": 2, "d": "3"}}
|
||||
reveal_type(x4["a"]) # revealed: Literal[1]
|
||||
reveal_type(x4["b"]) # revealed: dict[Unknown | str, Unknown | int | str]
|
||||
reveal_type(x4["b"]) # revealed: dict[str, int | str]
|
||||
reveal_type(x4["b"]["c"]) # revealed: Literal[2]
|
||||
reveal_type(x4["b"]["d"]) # revealed: Literal["3"]
|
||||
|
||||
@@ -119,6 +119,6 @@ reveal_type(x5["b"]["d"]) # revealed: TD
|
||||
|
||||
x6 = x7 = {"a": 1}
|
||||
# TODO: This should reveal `Literal[1]`.
|
||||
reveal_type(x6["a"]) # revealed: Unknown | int
|
||||
reveal_type(x7["a"]) # revealed: Unknown | int
|
||||
reveal_type(x6["a"]) # revealed: int
|
||||
reveal_type(x7["a"]) # revealed: int
|
||||
```
|
||||
|
||||
@@ -9,7 +9,7 @@ reveal_type([]) # revealed: list[Unknown]
|
||||
## List of tuples
|
||||
|
||||
```py
|
||||
reveal_type([(1, 2), (3, 4)]) # revealed: list[Unknown | tuple[int, int]]
|
||||
reveal_type([(1, 2), (3, 4)]) # revealed: list[tuple[int, int]]
|
||||
```
|
||||
|
||||
## List of functions
|
||||
@@ -22,24 +22,24 @@ def b(_: int) -> int:
|
||||
return 1
|
||||
|
||||
x = [a, b]
|
||||
reveal_type(x) # revealed: list[Unknown | ((_: int) -> int)]
|
||||
reveal_type(x) # revealed: list[(_: int) -> int]
|
||||
```
|
||||
|
||||
The inferred `Callable` type is function-like, i.e. we can still access attributes like `__name__`:
|
||||
|
||||
```py
|
||||
reveal_type(x[0].__name__) # revealed: Unknown | str
|
||||
reveal_type(x[0].__name__) # revealed: str
|
||||
```
|
||||
|
||||
## Mixed list
|
||||
|
||||
```py
|
||||
# revealed: list[Unknown | int | tuple[int, int] | tuple[int, int, int]]
|
||||
# revealed: list[int | tuple[int, int] | tuple[int, int, int]]
|
||||
reveal_type([1, (1, 2), (1, 2, 3)])
|
||||
```
|
||||
|
||||
## List comprehensions
|
||||
|
||||
```py
|
||||
reveal_type([x for x in range(42)]) # revealed: list[Unknown | int]
|
||||
reveal_type([x for x in range(42)]) # revealed: list[int]
|
||||
```
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
## Basic set
|
||||
|
||||
```py
|
||||
reveal_type({1, 2}) # revealed: set[Unknown | int]
|
||||
reveal_type({1, 2}) # revealed: set[int]
|
||||
```
|
||||
|
||||
## Set of tuples
|
||||
|
||||
```py
|
||||
reveal_type({(1, 2), (3, 4)}) # revealed: set[Unknown | tuple[int, int]]
|
||||
reveal_type({(1, 2), (3, 4)}) # revealed: set[tuple[int, int]]
|
||||
```
|
||||
|
||||
## Set of functions
|
||||
@@ -22,18 +22,18 @@ def b(_: int) -> int:
|
||||
return 1
|
||||
|
||||
x = {a, b}
|
||||
reveal_type(x) # revealed: set[Unknown | ((_: int) -> int)]
|
||||
reveal_type(x) # revealed: set[(_: int) -> int]
|
||||
```
|
||||
|
||||
## Mixed set
|
||||
|
||||
```py
|
||||
# revealed: set[Unknown | int | tuple[int, int] | tuple[int, int, int]]
|
||||
# revealed: set[int | tuple[int, int] | tuple[int, int, int]]
|
||||
reveal_type({1, (1, 2), (1, 2, 3)})
|
||||
```
|
||||
|
||||
## Set comprehensions
|
||||
|
||||
```py
|
||||
reveal_type({x for x in range(42)}) # revealed: set[Unknown | int]
|
||||
reveal_type({x for x in range(42)}) # revealed: set[int]
|
||||
```
|
||||
|
||||
@@ -65,9 +65,9 @@ reveal_type(promote(f)) # revealed: list[(_: int) -> int]
|
||||
The elements of invariant collection literals, i.e. lists, dictionaries, and sets, are promoted:
|
||||
|
||||
```py
|
||||
reveal_type([1, 2, 3]) # revealed: list[Unknown | int]
|
||||
reveal_type({"a": 1, "b": 2, "c": 3}) # revealed: dict[Unknown | str, Unknown | int]
|
||||
reveal_type({"a", "b", "c"}) # revealed: set[Unknown | str]
|
||||
reveal_type([1, 2, 3]) # revealed: list[int]
|
||||
reveal_type({"a": 1, "b": 2, "c": 3}) # revealed: dict[str, int]
|
||||
reveal_type({"a", "b", "c"}) # revealed: set[str]
|
||||
```
|
||||
|
||||
Covariant collection literals are not promoted:
|
||||
@@ -146,7 +146,7 @@ reveal_type(x1) # revealed: tuple[tuple[tuple[Literal[1]]]]
|
||||
reveal_type(promote(x1)) # revealed: list[tuple[tuple[tuple[int]]]]
|
||||
|
||||
x2 = ([1, 2], [(3,), (4,)], ["5", "6"])
|
||||
reveal_type(x2) # revealed: tuple[list[Unknown | int], list[Unknown | tuple[int]], list[Unknown | str]]
|
||||
reveal_type(x2) # revealed: tuple[list[int], list[tuple[int]], list[str]]
|
||||
```
|
||||
|
||||
However, this promotion should not take place if the literal type appears in contravariant position,
|
||||
@@ -158,7 +158,7 @@ def in_negated_position(non_zero_number: int):
|
||||
raise ValueError()
|
||||
|
||||
reveal_type(non_zero_number) # revealed: int & ~Literal[0]
|
||||
reveal_type([non_zero_number]) # revealed: list[Unknown | (int & ~Literal[0])]
|
||||
reveal_type([non_zero_number]) # revealed: list[int & ~Literal[0]]
|
||||
```
|
||||
|
||||
## Literal annotations are respected
|
||||
@@ -389,15 +389,15 @@ def promote[T](x: T) -> list[T]:
|
||||
|
||||
x1 = "hello"
|
||||
reveal_type(x1) # revealed: Literal["hello"]
|
||||
reveal_type([x1]) # revealed: list[Unknown | str]
|
||||
reveal_type([x1]) # revealed: list[str]
|
||||
|
||||
x2: Literal["hello"] = "hello"
|
||||
reveal_type(x2) # revealed: Literal["hello"]
|
||||
reveal_type([x2]) # revealed: list[Unknown | Literal["hello"]]
|
||||
reveal_type([x2]) # revealed: list[Literal["hello"]]
|
||||
|
||||
x3: tuple[Literal["hello"]] = ("hello",)
|
||||
reveal_type(x3) # revealed: tuple[Literal["hello"]]
|
||||
reveal_type([x3]) # revealed: list[Unknown | tuple[Literal["hello"]]]
|
||||
reveal_type([x3]) # revealed: list[tuple[Literal["hello"]]]
|
||||
|
||||
def f() -> Literal["hello"]:
|
||||
return "hello"
|
||||
@@ -407,18 +407,18 @@ def id[T](x: T) -> T:
|
||||
|
||||
reveal_type(f()) # revealed: Literal["hello"]
|
||||
reveal_type((f(),)) # revealed: tuple[Literal["hello"]]
|
||||
reveal_type([f()]) # revealed: list[Unknown | Literal["hello"]]
|
||||
reveal_type([id(f())]) # revealed: list[Unknown | Literal["hello"]]
|
||||
reveal_type([f()]) # revealed: list[Literal["hello"]]
|
||||
reveal_type([id(f())]) # revealed: list[Literal["hello"]]
|
||||
|
||||
def _(x: tuple[Literal["hello"]]):
|
||||
reveal_type(x) # revealed: tuple[Literal["hello"]]
|
||||
reveal_type([x]) # revealed: list[Unknown | tuple[Literal["hello"]]]
|
||||
reveal_type([x]) # revealed: list[tuple[Literal["hello"]]]
|
||||
|
||||
type X = Literal["hello"]
|
||||
|
||||
x4: X = "hello"
|
||||
reveal_type(x4) # revealed: Literal["hello"]
|
||||
reveal_type([x4]) # revealed: list[Unknown | Literal["hello"]]
|
||||
reveal_type([x4]) # revealed: list[Literal["hello"]]
|
||||
|
||||
class MyEnum(Enum):
|
||||
A = 1
|
||||
@@ -427,7 +427,7 @@ class MyEnum(Enum):
|
||||
|
||||
def _(x: Literal[MyEnum.A, MyEnum.B]):
|
||||
reveal_type(x) # revealed: Literal[MyEnum.A, MyEnum.B]
|
||||
reveal_type([x]) # revealed: list[Unknown | Literal[MyEnum.A, MyEnum.B]]
|
||||
reveal_type([x]) # revealed: list[Literal[MyEnum.A, MyEnum.B]]
|
||||
```
|
||||
|
||||
Literal promotability is respected by unions:
|
||||
@@ -440,29 +440,29 @@ def _(flag: bool):
|
||||
unpromotable1: Literal["age"] | None = "age" if flag else None
|
||||
|
||||
reveal_type(unpromotable1 or promotable1) # revealed: Literal["age"]
|
||||
reveal_type([unpromotable1 or promotable1]) # revealed: list[Unknown | Literal["age"]]
|
||||
reveal_type([unpromotable1 or promotable1]) # revealed: list[Literal["age"]]
|
||||
|
||||
promotable2 = "age" if flag else None
|
||||
unpromotable2: Literal["age"] = "age"
|
||||
|
||||
reveal_type(promotable2 or unpromotable2) # revealed: Literal["age"]
|
||||
reveal_type([promotable2 or unpromotable2]) # revealed: list[Unknown | Literal["age"]]
|
||||
reveal_type([promotable2 or unpromotable2]) # revealed: list[Literal["age"]]
|
||||
|
||||
promotable3 = True
|
||||
unpromotable3: Literal[True] | None = True if flag else None
|
||||
|
||||
reveal_type(unpromotable3 or promotable3) # revealed: Literal[True]
|
||||
reveal_type([unpromotable3 or promotable3]) # revealed: list[Unknown | Literal[True]]
|
||||
reveal_type([unpromotable3 or promotable3]) # revealed: list[Literal[True]]
|
||||
|
||||
promotable4 = True if flag else None
|
||||
unpromotable4: Literal[True] = True
|
||||
|
||||
reveal_type(promotable4 or unpromotable4) # revealed: Literal[True]
|
||||
reveal_type([promotable4 or unpromotable4]) # revealed: list[Unknown | Literal[True]]
|
||||
reveal_type([promotable4 or unpromotable4]) # revealed: list[Literal[True]]
|
||||
|
||||
type X = Literal[b"bar"]
|
||||
|
||||
def _(x1: X | None, x2: X):
|
||||
reveal_type([x1, x2]) # revealed: list[Unknown | Literal[b"bar"] | None]
|
||||
reveal_type([x1 or x2]) # revealed: list[Unknown | Literal[b"bar"]]
|
||||
reveal_type([x1, x2]) # revealed: list[Literal[b"bar"] | None]
|
||||
reveal_type([x1 or x2]) # revealed: list[Literal[b"bar"]]
|
||||
```
|
||||
|
||||
@@ -9,11 +9,11 @@ A list can be indexed into with:
|
||||
|
||||
```py
|
||||
x = [1, 2, 3]
|
||||
reveal_type(x) # revealed: list[Unknown | int]
|
||||
reveal_type(x) # revealed: list[int]
|
||||
|
||||
reveal_type(x[0]) # revealed: Unknown | int
|
||||
reveal_type(x[0]) # revealed: int
|
||||
|
||||
reveal_type(x[0:1]) # revealed: list[Unknown | int]
|
||||
reveal_type(x[0:1]) # revealed: list[int]
|
||||
|
||||
# error: [invalid-argument-type]
|
||||
reveal_type(x["a"]) # revealed: Unknown
|
||||
|
||||
@@ -55,7 +55,7 @@ def f(x: Iterable[int], y: list[str], z: Never, aa: list[Never], bb: LiskovUncom
|
||||
|
||||
reveal_type(tuple((1, 2))) # revealed: tuple[Literal[1], Literal[2]]
|
||||
|
||||
reveal_type(tuple([1])) # revealed: tuple[Unknown | int, ...]
|
||||
reveal_type(tuple([1])) # revealed: tuple[int, ...]
|
||||
|
||||
x1: tuple[int, ...] = tuple([1])
|
||||
reveal_type(x1) # revealed: tuple[int, ...]
|
||||
@@ -555,7 +555,7 @@ reveal_type((42, *[], 56, *[])) # revealed: tuple[Literal[42], Literal[56]]
|
||||
tup: Sequence[str] = (*{"foo": 42, "bar": 56},)
|
||||
|
||||
# TODO: `tuple[str, str]` would be better, given the type annotation
|
||||
reveal_type(tup) # revealed: tuple[Unknown | str, Unknown | str]
|
||||
reveal_type(tup) # revealed: tuple[str, str]
|
||||
|
||||
def f(x: list[int]):
|
||||
reveal_type((42, 56, *x, 97)) # revealed: tuple[Literal[42], Literal[56], *tuple[int, ...], Literal[97]]
|
||||
|
||||
@@ -213,8 +213,8 @@ reveal_type(d) # revealed: Literal[2]
|
||||
|
||||
```py
|
||||
a, b = [1, 2]
|
||||
reveal_type(a) # revealed: Unknown | int
|
||||
reveal_type(b) # revealed: Unknown | int
|
||||
reveal_type(a) # revealed: int
|
||||
reveal_type(b) # revealed: int
|
||||
```
|
||||
|
||||
### Simple unpacking
|
||||
|
||||
@@ -10313,13 +10313,15 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If a valid type annotation was not provided, avoid restricting the type of the
|
||||
// collection by unioning the inferred type with `Unknown`.
|
||||
let elt_tcx = elt_tcx.unwrap_or(Type::unknown());
|
||||
|
||||
builder
|
||||
.infer(&constraints, Type::TypeVar(elt_ty), elt_tcx)
|
||||
.ok()?;
|
||||
// If there is no applicable context for this element type variable, we infer from the
|
||||
// literal elements directly. This violates the gradual guarantee (we don't know that
|
||||
// our inference is compatible with subsequent additions to the collection), but it
|
||||
// matches the behavior of other type checkers and is usually the desired behavior.
|
||||
if let Some(elt_tcx) = elt_tcx {
|
||||
builder
|
||||
.infer(&constraints, Type::TypeVar(elt_ty), elt_tcx)
|
||||
.ok()?;
|
||||
}
|
||||
}
|
||||
|
||||
for elts in elts {
|
||||
|
||||
@@ -71,7 +71,7 @@ class Repository(NamedTuple):
|
||||
git_clone_command.extend(
|
||||
[
|
||||
f"https://github.com/{self.org}/{self.repo}",
|
||||
checkout_dir,
|
||||
str(checkout_dir),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -28,7 +28,13 @@ TOOLS_TO_BENCHMARK: Final = [
|
||||
Pyrefly(),
|
||||
]
|
||||
|
||||
SEVERITY_LABELS: Final = {1: "Error", 2: "Warning", 3: "Info", 4: "Hint"}
|
||||
SEVERITY_LABELS: Final = {
|
||||
None: "Unknown",
|
||||
1: "Error",
|
||||
2: "Warning",
|
||||
3: "Info",
|
||||
4: "Hint",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", params=ALL_PROJECTS, ids=lambda p: p.name)
|
||||
|
||||
Reference in New Issue
Block a user