-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description This should fix Issue #13061 . Mypy should now raise an error when it encounters unbound plain TypeVar. It should not get triggered when the return type contains a TypeVar (e.g. List[T]). It should not get triggered when a same TypeVar is present in the scope (e.g. inner function returns T, and T is an argument of outer function). ## Test Plan 5 tests were added: - a plain unbound typevar triggering the new error - a typevar inner function not triggering the error - a function returning an iterable of typevars, not triggering the error - a plain bound typevar, not triggering the error - nested bound typevars, not triggering the error We also changed the other functions triggering our error for the tests to pass. This is our 1st contribution to Mypy. Please, guide us if there is anything we can improve in this PR. This PR was made with @anilbey * Add simple unbound typevar check and rudimentary test * Add test for unbound func returning iterables of TypeVars * Add appropriate error message and make the new test pass * add CollectArgTypes to get set of argument types * lint fix * fix type error * extract check_unbound_return_typevar as method * check if return type is instantiated in typ.variables * Fix some tests that are affected by new implementation * add testInnerFunctionTypeVar test * fix testErrorCodeNeedTypeAnnotation test * add TYPE_VAR error code to unbound error failure * Fix the tests * move new tests in new test file * add 'check-typevar-unbound.test' to testcheck * add more nested tests for unbound type * microoptimise check_unbound_return_typevar check ret_type first * add missing builtins fixture to failing test Co-authored-by: Jaquier Aurélien Tristan <ajaquier@bb-fvfcv295mnhx.epfl.ch> Co-authored-by: Anil Tuncel <anil.tuncel@epfl.ch> Co-authored-by: Anil Tuncel <tuncel.manil@gmail.com> Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
- Loading branch information
1 parent
98f24f4
commit 2c152e6
Showing
10 changed files
with
98 additions
and
13 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
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
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
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,60 @@ | ||
|
||
[case testUnboundTypeVar] | ||
from typing import TypeVar | ||
|
||
T = TypeVar('T') | ||
|
||
def f() -> T: # E: A function returning TypeVar should receive at least one argument containing the same Typevar | ||
... | ||
|
||
f() | ||
|
||
|
||
[case testInnerFunctionTypeVar] | ||
|
||
from typing import TypeVar | ||
|
||
T = TypeVar('T') | ||
|
||
def g(a: T) -> T: | ||
def f() -> T: | ||
... | ||
return f() | ||
|
||
|
||
[case testUnboundIterableOfTypeVars] | ||
from typing import Iterable, TypeVar | ||
|
||
T = TypeVar('T') | ||
|
||
def f() -> Iterable[T]: | ||
... | ||
|
||
f() | ||
|
||
[case testBoundTypeVar] | ||
from typing import TypeVar | ||
|
||
T = TypeVar('T') | ||
|
||
def f(a: T, b: T, c: int) -> T: | ||
... | ||
|
||
|
||
[case testNestedBoundTypeVar] | ||
from typing import Callable, List, Union, Tuple, TypeVar | ||
|
||
T = TypeVar('T') | ||
|
||
def f(a: Union[int, T], b: str) -> T: | ||
... | ||
|
||
def g(a: Callable[..., T], b: str) -> T: | ||
... | ||
|
||
def h(a: List[Union[Callable[..., T]]]) -> T: | ||
... | ||
|
||
def j(a: List[Union[Callable[..., Tuple[T, T]], int]]) -> T: | ||
... | ||
[builtins fixtures/tuple.pyi] |