-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better type deduction for more expressions:
* Deduce type of walrus operator (`:=`) expressions * Deduce type of awaited expressions * Deduce type of `cast()` functions * Deduce `set` literals as type `set` * Deduce type for unary operations * Deduce type of index (`x[0]`) expressions * Deduce return type when calling lambda expressions
- Loading branch information
Showing
6 changed files
with
137 additions
and
32 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -39,6 +39,10 @@ def func() -> bool: | |
|
||
_ = bool(func()) | ||
|
||
s = {1} | ||
_ = set(s) | ||
_ = set({1}) | ||
|
||
|
||
# these will not | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# These are a variety of checks to ensure Refurb is able to deduce types from | ||
# complex expressions. | ||
|
||
_ = bool([True][0]) | ||
|
||
|
||
async def async_wrapper(): | ||
import asyncio | ||
|
||
async def return_bool() -> bool: | ||
return True | ||
|
||
task = asyncio.create_task(return_bool()) | ||
|
||
_ = bool(await return_bool()) | ||
_ = bool(await task) | ||
|
||
|
||
lambda_return_bool = lambda: True | ||
_ = bool(lambda_return_bool()) | ||
_ = bool((lambda: True)()) # TODO: error message should include parens around lambda | ||
|
||
bool_value = True | ||
|
||
_ = bool(not bool_value) | ||
_ = bool(not False) | ||
|
||
_ = int(-1) | ||
_ = int(+1) | ||
_ = int(~1) | ||
|
||
_ = bool(walrus := True) | ||
|
||
from typing import cast | ||
|
||
_ = bool(cast(bool, 123)) | ||
|
||
|
||
# These types are not able to be deduced (yet) | ||
_ = int(1 or 2) |
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,12 @@ | ||
test/data/type_deduce.py:4:5 [FURB123]: Replace `bool([True][0])` with `[True][0]` | ||
test/data/type_deduce.py:15:9 [FURB123]: Replace `bool(await return_bool())` with `await return_bool()` | ||
test/data/type_deduce.py:16:9 [FURB123]: Replace `bool(await task)` with `await task` | ||
test/data/type_deduce.py:20:5 [FURB123]: Replace `bool(lambda_return_bool())` with `lambda_return_bool()` | ||
test/data/type_deduce.py:21:5 [FURB123]: Replace `bool(lambda: True())` with `lambda: True()` | ||
test/data/type_deduce.py:25:5 [FURB123]: Replace `bool(not bool_value)` with `not bool_value` | ||
test/data/type_deduce.py:26:5 [FURB123]: Replace `bool(not False)` with `not False` | ||
test/data/type_deduce.py:28:5 [FURB123]: Replace `int(-1)` with `-1` | ||
test/data/type_deduce.py:29:5 [FURB123]: Replace `int(+1)` with `+1` | ||
test/data/type_deduce.py:30:5 [FURB123]: Replace `int(~1)` with `~1` | ||
test/data/type_deduce.py:32:5 [FURB123]: Replace `bool(walrus := True)` with `walrus := True` | ||
test/data/type_deduce.py:36:5 [FURB123]: Replace `bool(cast(bool, 123))` with `cast(bool, 123)` |