Repository navigation
Conversation
|
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
If a function contains a yield expression, i.e. if it is a generator, current type inference is incorrect. But supporting this requires solving another issues, so I will leave it as a TODO for now. As for return type inference for normal functions, I think the work is completed. |
3ba99d1 to
35f4a75
Compare
|
Sorry that I haven't gotten around to reviewing this PR yet. There's just been a lot to do, and providing this feature is lower on my priority list than some other features. But I realize that places a burden on you to keep it up to date with (rapidly changing) main branch. But I will try to find time to review it soon. |
0a16b46 to
126aef8
Compare
91c66c1 to
22e8594
Compare
22e8594 to
391ee91
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unsupported-operator |
35,575 | 0 | 73 |
possibly-missing-attribute |
15,046 | 86 | 339 |
invalid-argument-type |
3,481 | 82 | 239 |
non-subscriptable |
2,222 | 0 | 0 |
invalid-assignment |
1,638 | 11 | 33 |
not-iterable |
1,341 | 0 | 8 |
unresolved-attribute |
1,209 | 12 | 3 |
no-matching-overload |
728 | 0 | 0 |
call-non-callable |
703 | 2 | 0 |
invalid-return-type |
363 | 2 | 26 |
too-many-positional-arguments |
290 | 3 | 0 |
unknown-argument |
146 | 8 | 0 |
unsupported-base |
145 | 0 | 0 |
invalid-await |
115 | 0 | 6 |
invalid-key |
115 | 0 | 0 |
unused-ignore-comment |
20 | 83 | 0 |
missing-argument |
44 | 4 | 0 |
index-out-of-bounds |
43 | 4 | 0 |
parameter-already-assigned |
2 | 19 | 0 |
division-by-zero |
8 | 0 | 0 |
invalid-context-manager |
7 | 0 | 0 |
deprecated |
3 | 2 | 0 |
invalid-type-form |
2 | 0 | 0 |
possibly-unresolved-reference |
0 | 2 | 0 |
invalid-exception-caught |
1 | 0 | 0 |
invalid-method-override |
1 | 0 | 0 |
| Total | 63,248 | 320 | 727 |
|
Given the many new diagnostics, I think we should consider adding a new I also suggest running the |
800bfc8 to
0a80f98
Compare
| if self | ||
| .signature_type | ||
| .as_bound_method() | ||
| .is_none_or(|method| !method.is_init(self.db)) |
## Summary From #17371 In #17371, `DivergentType` was introduced to prevent type inference for recursive functions from diverging and causing panics. This turned out to be useful for other divergent type inferences (astral-sh/ruff#17371 (comment)), so I extracted the introduction part of `DivergentType` into this PR so that we can use it without waiting for the merge of #17371. Note that this PR only introduces `DivergentType` and does not actually address divergent type inference yet. Please refer to https://github.com/astral-sh/ruff/pull/17371/files#diff-20b910c6e20faa962bb1642e111db1cbad8e66ace089bdd966ac9d7f9fa99ff2R542-R622 etc. when implementing handling of divergence suppression using this type. ## Test Plan --------- Co-authored-by: Carl Meyer <[email protected]>
## Summary <img width="331" height="155" alt="image" src="https://github.com/user-attachments/assets/8e1b5c40-57b7-44ec-a197-3770e9fc3236" align=right /> This PR adds return type inference as a new feature to ty. The feature is currently *disabled by default*. Users can activate the feature by setting `analysis.return-type-inference` to `"always"`. In a standalone function, the inferred return type is simply the union of the types of all reachable `return`ed expressions, as shown in the image on the right. If control flow can reach the end of the function scope, we add `None` to the union. In a (non-final) method on a (non-final) class, we additionally union with `Unknown` to allow child classes to widen the intended return type. For example, imagine ```py class TaskBase: def compute(self): return None class IntTask(TaskBase): def compute(self): return 42 ``` Here, we infer a return type of `None | Unknown` for `TaskBase.compute`. Otherwise, if we inferred `None`, we would prohibit child classes from returning something else, which seems undesirable. Another interesting scenario is when the base class method is annotated, but the child class method is not: ```py class Base: def method(self) -> int: return 0 class Child(Base): def method(self): return 1 ``` Here, we respect the base class definition and infer `Child.method` as returning `int`, not `int | Unknown`. This work is based on #17371 by @mtshiba (thank you!) There are a few notable things that are _not_ implemented here: - The type of the function itself currently stays unchanged, that is: we do not infer a signature with the inferred return type. Concretely: `my_function()` may be inferred as `int`, but `my_function` itself would still be inferred as `def my_function() -> Unknown`. We have existing tests with TODOs for this and I plan to tackle this in a follow up. - We do not yet have return type inference for generator functions or async functions - We do not attempt to solve the problem of "escaping" inner types here (see tests with TODOs) - We do not add LSP/playground features related to this feature, but plan to do so soon (display return type hints). - There is a limitation around `functools.wraps` that's present in the ecosystem report. This will hopefully be solved by python/typeshed#16515 - There is a [limitation around `NotImplemented`](astral-sh/ty#1150 (comment)) which hasn't been resolved yet. The solution is described [here](astral-sh/ty#1150 (comment)). - Fixpoint iteration currently uses `Divergent` as a provisional value. I did think about using the new μ-types, but that currently leaves too many questions unanswered: How would we represent an anonymous μ-type? How would the (provisional) recursive marker `R` interact with other types during inference (especially since those operations require "closed" recursive types, and `R` would still be open)? closes astral-sh/ty#128 ## Test Plan - Changed assertions in the previously reviewed & merged mdtests - Studied the ecosystem impact and addressed many problems --------- Co-authored-by: Shunsuke Shibayama <[email protected]>
Summary
This PR closes astral-sh/ty#128.
FunctionType::infer_return_typeis added to infer the return type of a function when its return type is not specified.TODOs
- [ ] infer generator's return type- [ ] infer coroutine's return type- [ ] infer lambda function's return typeTest Plan
New test cases are added to
mdtest/function/return_type.md.