-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
No error in function definition that returns unbound T
#13061
Comments
Hints:
|
Could you elaborate on why and when types like |
@odesenfans The only example that I can think of is creating an empty container. For example, something like this: def make_bucket() -> Bucket[T]: ...
b: Bucket[int] = make_bucket() # similar to x: list[int] = list()
b.add(1) Usually you would just call the type object, but having a factory function seems like a plausible use case (e.g. we could do some extra things like logging or collect statistics in the factory function). |
### 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>
I think this was already fixed by #13166, but accidentally was not closed. |
Actual Behavior
No error in
f
definition. Twounreachable
errors insideif f(): ... else: ...
Expected Behavior
Error in
f
definition. For example:No errors inside
if f(): ... else: ...
My Environment
mypy 0.961 (compiled: no)
CPython 3.10.4
The text was updated successfully, but these errors were encountered: