-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] infer function's return type #17371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mtshiba
wants to merge
114
commits into
astral-sh:main
Choose a base branch
from
mtshiba:infer-return-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 dafae00
Do not infer the return type of a recursive function
mtshiba a8a35d5
The return type of an ambiguously typed method is `Unknown | <inferre…
mtshiba 391ee91
infer recursive function's return type
mtshiba b9bca33
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba 8745f3a
Apply suggestions from code review
mtshiba 18836cb
Update types.rs
mtshiba 418f7ee
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba b5824aa
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba 2c0cbb4
fix according to the review comments
mtshiba 005dde5
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba 4d611d9
refactor: `inferred_return_ty` -> `infer_return_type`
mtshiba 94201b6
Infer return types of (non-final) methods according to MRO
mtshiba 7f7c230
Use `callable_type` to get the return type instead of passing a closure
mtshiba 2c534c6
Update types.rs
mtshiba 60f1b0f
Refactor: TypeAndRange -> Returnee
mtshiba db63237
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba c0f218e
Add `CallStack` to prevent divergence in type inference for recursive…
mtshiba 65bdbf9
move `scipy`, `sympy` to `bad.txt`
mtshiba f17a650
make `TupleType::to_class_type` a tracked function
mtshiba 4f0ce11
increase `max_diagnostics` in `sympy`
mtshiba d6916f8
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba 9bc45f8
move `scipy` to `good.txt` and `manticore` to `bad.txt`
mtshiba 1e1993f
move `ScopeInference::returnees` to `ScopeInferenceExtra`
mtshiba e275162
Update type.md
mtshiba 46d21df
Avoid unspecialized type variables appearing in the return type
mtshiba 3701c73
WIP: remove `CallStack`
mtshiba e4246f5
add `Divergent` type
mtshiba 86f3c2d
divergence check
mtshiba 496a171
Merge remote-tracking branch 'upstream/main' into infer-return-type
mtshiba 38fcc41
Update ty_check_invalid_syntax.rs
mtshiba 07286d5
Merge branch 'main' into infer-return-type
mtshiba 893dd2b
follow changes in the main branch
mtshiba ec18cd1
Merge branch 'main' into infer-return-type
mtshiba d108854
Update infer.rs
mtshiba 83cd6ae
fix for fuzzer-reported panic
mtshiba c7eeffc
use `CycleDetector` with `has_divergent_type()`
mtshiba f981c84
the inferred return type should be monotonically widened in fixed-poi…
mtshiba e398069
Merge branch 'main' into infer-return-type
mtshiba fc3ae6f
Update types.rs
mtshiba 3c0034f
fix `union_add` bug
mtshiba 0bc75d4
increase max number of `pandas` diagnostics
mtshiba 90ec05e
No type normalization in `infer_return_type`
mtshiba 6581f3b
move `sympy` to `good.txt`
mtshiba f6418a6
The return type of a generator function is `Unknown` for now
mtshiba acb9118
Update types.rs
mtshiba 9b1c373
add doc comments
mtshiba 2d1b8a3
Add unit tests for the `Divergent` type
mtshiba a67b434
remove `divergence_safe_{todo, unknown}`
mtshiba bf4c1f1
remove `function_place`
mtshiba bc6793c
make it clear that type inference for lambda expressions is not yet p…
mtshiba 480e64a
revert an unnecessary change
mtshiba 5f38dcb
simply call the tracked function instead of using `LazyCell`
mtshiba 7326e47
Revert "revert an unnecessary change"
mtshiba 9739daa
remove `ScopeInference::scope`
mtshiba 866e567
revert changes in `narrow.rs`
mtshiba a1bd758
`ScopeInference::cycle_fallback() == Divergent`
mtshiba 3c9703d
remove special casing of `Divergent` in union/intersection reduction
mtshiba 7e418a7
Revert "remove special casing of `Divergent` in union/intersection re…
mtshiba aaa66f3
`Divergent` is not equivalent to other dynamic types
mtshiba eb4b078
Merge branch 'main' into infer-return-type
mtshiba e3b896d
add `DivergentType` to provide info about where the divergence occurs
mtshiba 8c15e30
Merge branch 'main' into infer-return-type
mtshiba e1a4654
`DivergentType` has a `ScopeId`
mtshiba 2197085
refactor
mtshiba 8da9e9b
clarify that the inferred return type is not exposed in the signature
mtshiba f6cd44b
use `any_over_type` in `has_divergent_type`
mtshiba 1d0c044
Merge branch 'main' into infer-return-type
mtshiba 164af9e
remove unnecessary changes
mtshiba 4f68d19
Update return_type.md
mtshiba fca0937
remove unnecessary type annotations
mtshiba fe252a0
Update crates/ty_python_semantic/resources/mdtest/function/return_typ…
mtshiba c76c0f8
Update return_type.md
mtshiba 312d97a
Update return_type.md
mtshiba e47baf5
Merge branch 'main' into infer-return-type
mtshiba 69953ff
normalize recursive types
mtshiba fde305f
Merge branch 'main' into infer-return-type
mtshiba b750358
remove `CycleRecovery` and set the cycle initial value to `Divergent`
mtshiba 8a19b05
don't sort types during recursive type normalization
mtshiba 35ffbfc
remove the `debug_assertions` attributes
mtshiba 23e0cfe
do nothing except normalization of recursive types when `visitor.is_r…
mtshiba 3d74f19
set the cycle initial value of `BoundMethod::into_callable_type, Clas…
mtshiba edad752
Merge branch 'main' into infer-return-type
mtshiba 3e2c2c4
`DivergentType` has the kind of divergence, and the cycle initial val…
mtshiba 74af681
union the previous cycle type in `implicit_attribute`
mtshiba 2a73060
Add `PossiblyRecursive{Scope, Definition, Expression}`
mtshiba 3602296
experiment: `DivergentType::scope: ScopeId -> file: File, file_scope:…
mtshiba 48aea8a
Revert "experiment: `DivergentType::scope: ScopeId -> file: File, fil…
mtshiba b7fa61b
proper cycle recovery handling for `infer_expression_types`
mtshiba bea05a5
use `DivergentType` to protect tracked functions that may fall into d…
mtshiba dfc71f4
Merge branch 'main' into infer-return-type
mtshiba a26d6c6
Revert "use `any_over_type` in `has_divergent_type`"
mtshiba c210b75
separate `NormalizedVisitor` and `RecursiveTypeNormalizedVisitor`
mtshiba f40a29e
refactor
mtshiba 61816b4
remove unnecessary code
mtshiba 8593f11
prevent unintended `Divergent` types from appearing in the inference …
mtshiba 6b9c889
fix functions that use `{Scope, Expression, Definition}Inference` to …
mtshiba 151fa0d
Update cycle.md
mtshiba c61b33c
Merge branch 'main' into infer-return-type
mtshiba 81a5247
Update function.rs
mtshiba caf5691
Update infer.rs
mtshiba 4432532
Update crates/ty_python_semantic/resources/mdtest/attributes.md
mtshiba 43d306b
Update type.md
mtshiba a778047
Update return_type.md
mtshiba 6419669
Merge branch 'main' into infer-return-type
mtshiba e7024ae
revert unnecessary changes for the purpose of this PR
mtshiba b9dda53
Merge branch 'main' into infer-return-type
mtshiba 0132164
Update types.rs
mtshiba c58434d
use `any_over_type` in `has_divergent_type`
mtshiba 112599f
revert unnecessary changes for the purpose of this PR
mtshiba 0e63890
use `CycleRecoveryAction::Fallback` even within `infer_scope_types`
mtshiba 92f3e4c
lower the limit for `MAX_UNION_LITERALS`
mtshiba 90d770b
Merge branch 'main' into infer-return-type
mtshiba 706eff8
Update ty_walltime.rs
mtshiba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
crates/ty_python_semantic/resources/corpus/cycle_into_callable.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.