Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
ee47848
[red-knot] infer function's return type
mtshiba Apr 13, 2025
dafae00
Do not infer the return type of a recursive function
mtshiba May 8, 2025
a8a35d5
The return type of an ambiguously typed method is `Unknown | <inferre…
mtshiba May 8, 2025
391ee91
infer recursive function's return type
mtshiba May 11, 2025
b9bca33
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba May 28, 2025
8745f3a
Apply suggestions from code review
mtshiba May 28, 2025
18836cb
Update types.rs
mtshiba May 29, 2025
418f7ee
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba Jun 5, 2025
b5824aa
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba Jun 8, 2025
2c0cbb4
fix according to the review comments
mtshiba Jun 8, 2025
005dde5
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba Jun 21, 2025
4d611d9
refactor: `inferred_return_ty` -> `infer_return_type`
mtshiba Jun 21, 2025
94201b6
Infer return types of (non-final) methods according to MRO
mtshiba Jun 21, 2025
7f7c230
Use `callable_type` to get the return type instead of passing a closure
mtshiba Jun 21, 2025
2c534c6
Update types.rs
mtshiba Jun 22, 2025
60f1b0f
Refactor: TypeAndRange -> Returnee
mtshiba Jul 5, 2025
db63237
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba Jul 7, 2025
c0f218e
Add `CallStack` to prevent divergence in type inference for recursive…
mtshiba Jul 7, 2025
65bdbf9
move `scipy`, `sympy` to `bad.txt`
mtshiba Jul 7, 2025
f17a650
make `TupleType::to_class_type` a tracked function
mtshiba Jul 7, 2025
4f0ce11
increase `max_diagnostics` in `sympy`
mtshiba Jul 7, 2025
d6916f8
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba Aug 1, 2025
9bc45f8
move `scipy` to `good.txt` and `manticore` to `bad.txt`
mtshiba Aug 1, 2025
1e1993f
move `ScopeInference::returnees` to `ScopeInferenceExtra`
mtshiba Aug 1, 2025
e275162
Update type.md
mtshiba Aug 2, 2025
46d21df
Avoid unspecialized type variables appearing in the return type
mtshiba Aug 5, 2025
3701c73
WIP: remove `CallStack`
mtshiba Aug 6, 2025
e4246f5
add `Divergent` type
mtshiba Aug 6, 2025
86f3c2d
divergence check
mtshiba Aug 6, 2025
496a171
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba Aug 6, 2025
38fcc41
Update ty_check_invalid_syntax.rs
mtshiba Aug 6, 2025
07286d5
Merge branch 'main' into infer-return-type
mtshiba Aug 29, 2025
893dd2b
follow changes in the main branch
mtshiba Aug 29, 2025
ec18cd1
Merge branch 'main' into infer-return-type
mtshiba Aug 30, 2025
d108854
Update infer.rs
mtshiba Aug 30, 2025
83cd6ae
fix for fuzzer-reported panic
mtshiba Aug 30, 2025
c7eeffc
use `CycleDetector` with `has_divergent_type()`
mtshiba Aug 31, 2025
f981c84
the inferred return type should be monotonically widened in fixed-poi…
mtshiba Sep 1, 2025
e398069
Merge branch 'main' into infer-return-type
mtshiba Sep 1, 2025
fc3ae6f
Update types.rs
mtshiba Sep 1, 2025
3c0034f
fix `union_add` bug
mtshiba Sep 1, 2025
0bc75d4
increase max number of `pandas` diagnostics
mtshiba Sep 1, 2025
90ec05e
No type normalization in `infer_return_type`
mtshiba Sep 1, 2025
6581f3b
move `sympy` to `good.txt`
mtshiba Sep 1, 2025
f6418a6
The return type of a generator function is `Unknown` for now
mtshiba Sep 1, 2025
acb9118
Update types.rs
mtshiba Sep 2, 2025
9b1c373
add doc comments
mtshiba Sep 2, 2025
2d1b8a3
Add unit tests for the `Divergent` type
mtshiba Sep 2, 2025
a67b434
remove `divergence_safe_{todo, unknown}`
mtshiba Sep 2, 2025
bf4c1f1
remove `function_place`
mtshiba Sep 2, 2025
bc6793c
make it clear that type inference for lambda expressions is not yet p…
mtshiba Sep 2, 2025
480e64a
revert an unnecessary change
mtshiba Sep 2, 2025
5f38dcb
simply call the tracked function instead of using `LazyCell`
mtshiba Sep 2, 2025
7326e47
Revert "revert an unnecessary change"
mtshiba Sep 2, 2025
9739daa
remove `ScopeInference::scope`
mtshiba Sep 3, 2025
866e567
revert changes in `narrow.rs`
mtshiba Sep 3, 2025
a1bd758
`ScopeInference::cycle_fallback() == Divergent`
mtshiba Sep 3, 2025
3c9703d
remove special casing of `Divergent` in union/intersection reduction
mtshiba Sep 3, 2025
7e418a7
Revert "remove special casing of `Divergent` in union/intersection re…
mtshiba Sep 3, 2025
aaa66f3
`Divergent` is not equivalent to other dynamic types
mtshiba Sep 3, 2025
eb4b078
Merge branch 'main' into infer-return-type
mtshiba Sep 3, 2025
e3b896d
add `DivergentType` to provide info about where the divergence occurs
mtshiba Sep 8, 2025
8c15e30
Merge branch 'main' into infer-return-type
mtshiba Sep 8, 2025
e1a4654
`DivergentType` has a `ScopeId`
mtshiba Sep 8, 2025
2197085
refactor
mtshiba Sep 8, 2025
8da9e9b
clarify that the inferred return type is not exposed in the signature
mtshiba Sep 8, 2025
f6cd44b
use `any_over_type` in `has_divergent_type`
mtshiba Sep 9, 2025
1d0c044
Merge branch 'main' into infer-return-type
mtshiba Sep 9, 2025
164af9e
remove unnecessary changes
mtshiba Sep 9, 2025
4f68d19
Update return_type.md
mtshiba Sep 9, 2025
fca0937
remove unnecessary type annotations
mtshiba Sep 9, 2025
fe252a0
Update crates/ty_python_semantic/resources/mdtest/function/return_typ…
mtshiba Sep 9, 2025
c76c0f8
Update return_type.md
mtshiba Sep 9, 2025
312d97a
Update return_type.md
mtshiba Sep 9, 2025
e47baf5
Merge branch 'main' into infer-return-type
mtshiba Sep 11, 2025
69953ff
normalize recursive types
mtshiba Sep 12, 2025
fde305f
Merge branch 'main' into infer-return-type
mtshiba Sep 12, 2025
b750358
remove `CycleRecovery` and set the cycle initial value to `Divergent`
mtshiba Sep 12, 2025
8a19b05
don't sort types during recursive type normalization
mtshiba Sep 12, 2025
35ffbfc
remove the `debug_assertions` attributes
mtshiba Sep 12, 2025
23e0cfe
do nothing except normalization of recursive types when `visitor.is_r…
mtshiba Sep 12, 2025
3d74f19
set the cycle initial value of `BoundMethod::into_callable_type, Clas…
mtshiba Sep 13, 2025
edad752
Merge branch 'main' into infer-return-type
mtshiba Sep 13, 2025
3e2c2c4
`DivergentType` has the kind of divergence, and the cycle initial val…
mtshiba Sep 13, 2025
74af681
union the previous cycle type in `implicit_attribute`
mtshiba Sep 14, 2025
2a73060
Add `PossiblyRecursive{Scope, Definition, Expression}`
mtshiba Sep 14, 2025
3602296
experiment: `DivergentType::scope: ScopeId -> file: File, file_scope:…
mtshiba Sep 14, 2025
48aea8a
Revert "experiment: `DivergentType::scope: ScopeId -> file: File, fil…
mtshiba Sep 14, 2025
b7fa61b
proper cycle recovery handling for `infer_expression_types`
mtshiba Sep 15, 2025
bea05a5
use `DivergentType` to protect tracked functions that may fall into d…
mtshiba Sep 16, 2025
dfc71f4
Merge branch 'main' into infer-return-type
mtshiba Sep 16, 2025
a26d6c6
Revert "use `any_over_type` in `has_divergent_type`"
mtshiba Sep 16, 2025
c210b75
separate `NormalizedVisitor` and `RecursiveTypeNormalizedVisitor`
mtshiba Sep 16, 2025
f40a29e
refactor
mtshiba Sep 17, 2025
61816b4
remove unnecessary code
mtshiba Sep 17, 2025
8593f11
prevent unintended `Divergent` types from appearing in the inference …
mtshiba Sep 17, 2025
6b9c889
fix functions that use `{Scope, Expression, Definition}Inference` to …
mtshiba Sep 17, 2025
151fa0d
Update cycle.md
mtshiba Sep 17, 2025
c61b33c
Merge branch 'main' into infer-return-type
mtshiba Sep 17, 2025
81a5247
Update function.rs
mtshiba Sep 17, 2025
caf5691
Update infer.rs
mtshiba Sep 17, 2025
4432532
Update crates/ty_python_semantic/resources/mdtest/attributes.md
mtshiba Sep 17, 2025
43d306b
Update type.md
mtshiba Sep 17, 2025
a778047
Update return_type.md
mtshiba Sep 17, 2025
6419669
Merge branch 'main' into infer-return-type
mtshiba Sep 24, 2025
e7024ae
revert unnecessary changes for the purpose of this PR
mtshiba Sep 24, 2025
b9dda53
Merge branch 'main' into infer-return-type
mtshiba Sep 24, 2025
0132164
Update types.rs
mtshiba Sep 24, 2025
c58434d
use `any_over_type` in `has_divergent_type`
mtshiba Sep 24, 2025
112599f
revert unnecessary changes for the purpose of this PR
mtshiba Sep 24, 2025
0e63890
use `CycleRecoveryAction::Fallback` even within `infer_scope_types`
mtshiba Sep 24, 2025
92f3e4c
lower the limit for `MAX_UNION_LITERALS`
mtshiba Sep 25, 2025
90d770b
Merge branch 'main' into infer-return-type
mtshiba Sep 25, 2025
706eff8
Update ty_walltime.rs
mtshiba Sep 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/ruff_benchmark/benches/ty_walltime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static PANDAS: std::sync::LazyLock<Benchmark<'static>> = std::sync::LazyLock::ne
max_dep_date: "2025-06-17",
python_version: PythonVersion::PY312,
},
3000,
3300,
)
});

Expand Down Expand Up @@ -199,7 +199,8 @@ static SYMPY: std::sync::LazyLock<Benchmark<'static>> = std::sync::LazyLock::new
max_dep_date: "2025-06-17",
python_version: PythonVersion::PY312,
},
13000,
// TODO: With better decorator support, `__slots__` support, etc., it should be possible to reduce the number of errors considerably.
70000,
)
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Regression test for https://github.com/astral-sh/ruff/issues/17371
# panicked in commit d1088545a08aeb57b67ec1e3a7f5141159efefa5
# error message:
# dependency graph cycle when querying ClassType < 'db >::into_callable_(Id(1c00))

try:
class foo[T: bar](object):
pass
bar = foo
except Exception:
bar = lambda: 0
def bar():
pass

@bar()
class bar:
pass
72 changes: 72 additions & 0 deletions crates/ty_python_semantic/resources/corpus/divergent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
def f(cond: bool):
if cond:
result = ()
result += (f(cond),)
return result

return None

reveal_type(f(True))

def f(cond: bool):
if cond:
result = ()
result += (f(cond),)
return result

return None

def f(cond: bool):
result = None
if cond:
result = ()
result += (f(cond),)

return result

reveal_type(f(True))

def f(cond: bool):
result = None
if cond:
result = [f(cond) for _ in range(1)]

return result

reveal_type(f(True))

class Foo:
def value(self):
return 1

def unwrap(value):
if isinstance(value, Foo):
foo = value
return foo.value()
elif type(value) is tuple:
length = len(value)
if length == 0:
return ()
elif length == 1:
return (unwrap(value[0]),)
else:
result = []
for item in value:
result.append(unwrap(item))
return tuple(result)
else:
raise TypeError()

def descent(x: int, y: int):
if x > y:
y, x = descent(y, x)
return x, y
if x == 1:
return (1, 0)
if y == 1:
return (0, 1)
else:
return descent(x-1, y-1)

def count_set_bits(n):
return 1 + count_set_bits(n & n - 1) if n else 0
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def outer_sync(): # `yield` from is only valid syntax inside a synchronous func
a: (yield from [1]), # error: [invalid-type-form] "`yield from` expressions are not allowed in type expressions"
): ...

async def baz(): ...
async def baz():
yield

async def outer_async(): # avoid unrelated syntax errors on `yield` and `await`
def _(
a: 1, # error: [invalid-type-form] "Int literals are not allowed in this context in a type expression"
Expand Down
6 changes: 3 additions & 3 deletions crates/ty_python_semantic/resources/mdtest/call/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _(flag: bool):

# error: [call-non-callable] "Object of type `Literal["This is a string literal"]` is not callable"
x = f(3)
reveal_type(x) # revealed: Unknown
reveal_type(x) # revealed: None | Unknown
```

## Union of binding errors
Expand All @@ -128,7 +128,7 @@ def _(flag: bool):
# error: [too-many-positional-arguments] "Too many positional arguments to function `f1`: expected 0, got 1"
# error: [too-many-positional-arguments] "Too many positional arguments to function `f2`: expected 0, got 1"
x = f(3)
reveal_type(x) # revealed: Unknown
reveal_type(x) # revealed: None
```

## One not-callable, one wrong argument
Expand All @@ -146,7 +146,7 @@ def _(flag: bool):
# error: [too-many-positional-arguments] "Too many positional arguments to function `f1`: expected 0, got 1"
# error: [call-non-callable] "Object of type `C` is not callable"
x = f(3)
reveal_type(x) # revealed: Unknown
reveal_type(x) # revealed: None | Unknown
```

## Union including a special-cased function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ match obj:

```py
class C:
def __await__(self): ...
def __await__(self):
yield

# error: [invalid-syntax] "`return` statement outside of a function"
return
Expand Down
Loading