-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autotyping-like return type inference for annotation rules (#8643)
## Summary This PR adds (unsafe) fixes to the flake8-annotations rules that enforce missing return types, offering to automatically insert type annotations for functions with literal return values. The logic is smart enough to generate simplified unions (e.g., `float` instead of `int | float`) and deal with implicit returns (`return` without a value). Closes #1640 (though we could open a separate issue for referring parameter types). Closes #8213. ## Test Plan `cargo test`
- Loading branch information
1 parent
23c819b
commit bf2cc3f
Showing
18 changed files
with
580 additions
and
140 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
crates/ruff_linter/resources/test/fixtures/flake8_annotations/auto_return_type.py
This file contains 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,32 @@ | ||
def func(): | ||
return 1 | ||
|
||
|
||
def func(): | ||
return 1.5 | ||
|
||
|
||
def func(x: int): | ||
if x > 0: | ||
return 1 | ||
else: | ||
return 1.5 | ||
|
||
|
||
def func(): | ||
return True | ||
|
||
|
||
def func(x: int): | ||
if x > 0: | ||
return None | ||
else: | ||
return | ||
|
||
|
||
def func(x: int): | ||
return 1 or 2.5 if x > 0 else 1.5 or "str" | ||
|
||
|
||
def func(x: int): | ||
return 1 + 2.5 if x > 0 else 1.5 or "str" |
This file contains 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 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.