-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
(🎁) Flag to make default return type None
instead of Any
#9413
Comments
Already exists: ‘ --check-untyped-defs’. |
@gvanrossum I don't think you understand my feature request, in Kotlin fun foo() {
return
} is a valid function, that returns an implicit Swift and Rust(and others I assume) also have this feature and I think it's a good one. The Thanks for the quick response. |
Go also supports this. |
Currently returns def foo():
return |
Whoops. |
I think that this could be a useful feature, but perhaps only when There is a problem when using Unit tests, in particular, could benefit from this, since there tend to be a lot of functions like Additionally, maybe an explicit |
Maybe name the flag I would think an explicit none return type in this mode should be a warning. |
Or it would rather be an alternative to these 2 options.
This case should be considered an error. |
I feel this feature request is misguided. It creates an incompatible variant language. Just write ‘-> None’ already! |
I don't know. Maybe I'm just too spoiled by the fact that |
This feels like a special case of #4409. |
I implemented this in my fork, haven't made a release yet though. |
It feels nonsensical that mypy is incapable of inferring this itself. I've seen far too many developers come from Typescript (where its inference capabilities are incredible) to Python and straight up complain about why they have to manually annotate the obvious. |
Basedmypy 1.3 supports this functionality with the |
I would not want it to be inferred. I would like this to give me an error: def foo():
return 3 If there's no return type annotation, that means return |
I don't understand this. Can you explain? |
but why? |
There is a perfect explanation of that in the post you replied to. |
Because it may not be obvious to a human reader what the expected return type is. It's one thing to have mypy infer the return type by looking at what's being returned, it's a completely different thing for a human to do the same. In the example of returning a single integer that's easy enough of course, but chained function calls or objects are less obvious. By being explicit with what you want to return you are also reducing risk for bugs and it makes refactoring and team work much easier. And yes I do consider not writing a return type to be just as explicit as writing I am very much in favor of having a flag that enables this behavior as I find myself writing |
None
instead of Any
I'll bring a counterproposal from my (closed as duplicate) issue #14180, which I'll copy here for convenience. I seem to understand that all arguments against the proposal have to do with it potentially being unexpected as a default behaviour (and it would definitely be something of a breaking change for anyone who already adopts mypy and doesn't enable
def my_func(i_am_annotated: str): # Note: no "-> int" annotation
return 5
|
@AlexWaygood This is not about inference. def foo():
return 3 With this option turned on, this would be considered an annotated function, and it would report an error because I'm not allowed to return |
Strongly in favor of this! The major blocker for out team in adopting |
Note you could use a complementary tool like https://github.com/JelleZijlstra/autotyping to automatically add |
@hauntsaninja Yes, but |
As a workaround,
|
None
instead of Any
None
instead of Any
Came here looking for this feature - to default the return type to None if one wasn't specified, and give me an error if the function is returning something. |
🚀 Feature
Most other languages(Kotlin, Rust, Swift etc) have a
void
(or equivalent) return type by default. Can an option be added to have an implicitNone
return type.🆕 Update
Basedmypy 1.3 supports this functionality with the
--default-return
flag.The text was updated successfully, but these errors were encountered: