Skip to content
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

What if a function has argument types but no return type? #37

Closed
JukkaL opened this issue Jan 15, 2015 · 5 comments
Closed

What if a function has argument types but no return type? #37

JukkaL opened this issue Jan 15, 2015 · 5 comments

Comments

@JukkaL
Copy link
Contributor

JukkaL commented Jan 15, 2015

The PEP draft has an example where there are argument type annotations but no return type annotation. What does it mean? Mypy currently treats the return type in a case like this as Any, but this is probably confusing, and it's easy to forget to give a return type. Maybe this should be disallowed?

For example, should this code be valid:

def f(x: int):
    return x + 1

There at least 4 potential approaches:

  1. Treat missing return type as Any.
  2. Treat missing return type as None.
  3. Treat missing return type as an error (if any argument has an annotation).
  4. If return type is missing but an argument has a type, infer return type automatically.

I'm inclined to support 3. I'm against 2 (because of inconsistency for functions which take no arguments, which must have an explicit None return type as otherwise they don't have an annotation at all) and 4 (explicit is better than implicit, implementation complexity).

@vlasovskikh
Copy link
Member

I think 1 sounds good. What if some of the arguments are not annotated?

@gvanrossum
Copy link
Member

I always assumed this would be (1), because "unannotated" means "Any" in
general.

On Thu, Jan 15, 2015 at 7:16 AM, Andrey Vlasovskikh <
notifications@github.com> wrote:

I think 1 sounds good. What if some of the arguments are not annotated?


Reply to this email directly or view it on GitHub
#37 (comment).

--Guido van Rossum (python.org/~guido)

@JukkaL
Copy link
Contributor Author

JukkaL commented Jan 17, 2015

Okay, 1 is reasonable and how it currently works in mypy. Maybe the examples in the PEP should be modified to include the return value type, even if it's None? Otherwise, it may give the impression that leaving out the return type is generally the right thing to do, even though statically typed code generally should explicitly define return types, as the implicit Any types limit the type checker's effectiveness.

@gvanrossum
Copy link
Member

Agreed, the PEP should only show fully-annotated examples, except where explaining what leaving out annotations means.

@gvanrossum
Copy link
Member

Looks like a duplicate of #65

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants