Add no-any-return error code#3922
Conversation
|
Thank you for the time and effort you put into submitting this! Regarding error subcategories, our existing convention is to use suffixes for more granular error codes. For example, we could use As for the default severity, I am personally a bit hesitant to enable Emitting non-actionable diagnostics like this generally creates a very poor user experience. Because of this, I would prefer that this kind of error remains strictly opt-in, rather than being bundled into the default strict preset. (cc. @yangdanny97 for more opinions) |
|
@grievejia Thanks for the review. Those changes are fine to me. Mypy enables no-any-return with strict, so this would be the only deviation. I would plan to make the following changes:
How does this plan sound? Do you prefer rebasing into one commit or shall I create a second commit with these changes? |
|
Sounds good! Perhaps we could enable it as well in our One minor clarification on step 3: you actually don't need to create any new dedicated configuration flags for these. Since we already have a generic mechanism for configuring individual error code severities, Either having a second commit or doing single commit works for us. |
Add an umbrella `no-any-return` error code that enables `no-any-return-implicit` and `no-any-return-explicit`. These error codes catch returning `Any` on a function that has a concrete type. The first, `no-any-return-explicit`, will present itself in cases where an explicitly typed `Any` is returned on a function that has a concrete return type annotation. The latter, `no-any-return-implicit`, appears in the case of an implicit `Any`, often coming from an another untyped function. Returning `Any` through a concrete return annotation weakens the guarantee provided by the annotation. The caller sees a precise type while the implementation has not demonstrated that the value satisfies that type. Enabling these error codes will improve bug finding ability. The strategy is to hook into where explicit `return` statements and their expressions are processed and compare the return type to the declared hint. This will be done for `async` and `Generator` functions as well. `yield` from generators is explicitly not included in this error code. `no-any-return` is completely opt-in. The `strict` preset will not include it by default. Migration from `mypy`'s `warn_return_any` is included in this change. Fixes facebook#3806
|
@grievejia I've made the changes:
|
Summary
Add an umbrella
no-any-returnerror code that enablesno-any-return-implicitandno-any-return-explicit. These error codescatch returning
Anyon a function that has a concrete type. The first,no-any-return-explicit, will present itself in cases where anexplicitly typed
Anyis returned on a function that has a concretereturn type annotation. The latter,
no-any-return-implicit, appears inthe case of an implicit
Any, often coming from an another untypedfunction.
Returning
Anythrough a concrete return annotation weakens the guaranteeprovided by the annotation. The caller sees a precise type while the
implementation has not demonstrated that the value satisfies that type.
Enabling these error codes will improve bug finding ability.
The strategy is to hook into where explicit
returnstatements andtheir expressions are processed and compare the return type to the
declared hint. This will be done for
asyncandGeneratorfunctionsas well.
yieldfrom generators is explicitly not included in thiserror code.
no-any-returnis completely opt-in. Thestrictpreset will notinclude it by default. Migration from
mypy'swarn_return_anyisincluded in this change.
Fixes #3806
Test Plan
New unit tests added to cover this functionality.
No changes from
test.pyto commit.